process.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. --- Process functions for orgs
  2. -- @module modpol.orgs.process
  3. --- Call modules
  4. -- @function modpol.orgs.call_module
  5. -- @param module_slug Same as module name
  6. -- @param intiator Initiator for module
  7. -- @param config Config for module
  8. -- @param result
  9. function modpol.orgs:call_module(module_slug, initiator, config, result, parent_id)
  10. if not modpol.modules[module_slug] then
  11. modpol.ocutil.log('Error in ' .. self.name .. ':call_module -> module "' .. module_slug .. '" not found')
  12. return
  13. end
  14. local index = #self.processes + 1
  15. local module = modpol.modules[module_slug]
  16. -- sets default values for undeclared config variables
  17. -- first applies any relevant org policies
  18. -- then overrides with the config values given on input
  19. local new_config = {}
  20. if modpol.util.num_pairs(module.config) > 0 then
  21. for k, v in pairs(module.config) do
  22. new_config[k] = v
  23. -- org policies
  24. if self.policies[module_slug]
  25. and self.policies[module_slug][k] then
  26. new_config[k] = self.policies[module_slug][k]
  27. end
  28. -- input settings
  29. if config and config[k] then
  30. new_config[k] = config[k]
  31. end
  32. end
  33. end
  34. -- setting default params
  35. local new_process = {
  36. metatable = {__index = module},
  37. initiator = initiator,
  38. org = self,
  39. id = index,
  40. parent_id = parent_id,
  41. children = {},
  42. config = modpol.util.copy_table(new_config),
  43. data = modpol.util.copy_table(module.data),
  44. slug = module_slug
  45. }
  46. -- call module wrapper for modules, passes its id to child process when called
  47. function new_process:call_module(module_slug, initiator, config, result)
  48. local child_id = self.org:call_module(module_slug, initiator, config, result, self.id)
  49. table.insert(self.children, child_id)
  50. end
  51. setmetatable(new_process, new_process.metatable)
  52. self.processes[index] = new_process
  53. self.processes[index]:initiate(result)
  54. local msg = "Initiating "..module_slug..
  55. " process id "..index.." in org "..self.name
  56. return index
  57. end
  58. --- Get the root process of the given id
  59. -- @function modpol.orgs.get_root_process
  60. -- @param id
  61. -- @return root process
  62. function modpol.orgs:get_root_process(id)
  63. local process = self.processes[id]
  64. while (process.parent_id) do
  65. process = self.processes[process.parent_id]
  66. end
  67. return process
  68. end
  69. --- Delete the process given id
  70. -- @function modpol.orgs.delete_process
  71. -- @param id
  72. function modpol.orgs:delete_process(id)
  73. local process = self.processes[id]
  74. if process and process ~= "deleted" then
  75. -- recursively deletes any children
  76. if #process.children > 0 then
  77. for i, child_id in pairs(process.children) do
  78. self:delete_process(child_id)
  79. end
  80. end
  81. local msg = "Deleting " .. self.processes[id].slug .. " process id "..id.." in org "..self.name
  82. modpol.ocutil.log(msg)
  83. self:record(msg, self.processes[id].slug)
  84. self:wipe_pending_actions(id)
  85. -- sets process to 'deleted' in process table
  86. self.processes[id] = 'deleted'
  87. end
  88. end
  89. --- Delete process tree by id
  90. -- @function modpol.orgs:delete_process_tree
  91. -- @param id Id of process tree
  92. function modpol.orgs:delete_process_tree(id)
  93. self:delete_process(self:get_root_process(id).id)
  94. end
  95. --- Add a new pending action
  96. -- @function modpol.orgs:add_pending_action
  97. -- @param process_id Process id
  98. -- @param user User adding the action
  99. -- @param callback
  100. function modpol.orgs:add_pending_action(process_id, user, callback)
  101. self.pending[user] = self.pending[user] or {}
  102. self.pending[user][process_id] = callback
  103. modpol.interactions.message(
  104. user, "New pending action in org "..self.name)
  105. end
  106. --- Remove a pending action
  107. -- @function modpol.orgs:remove_pending_action
  108. -- @param process_id Process id to be removed
  109. -- @param user
  110. function modpol.orgs:remove_pending_action(process_id, user)
  111. if self.pending[user] then
  112. self.pending[user][process_id] = nil
  113. end
  114. end
  115. --- Wipe all pending actions for process
  116. -- @function modpol.orgs:wipe_pending_actions
  117. -- @param process_id
  118. function modpol.orgs:wipe_pending_actions(process_id)
  119. for user in pairs(self.pending) do
  120. self.pending[user][process_id] = nil
  121. end
  122. end
  123. --- Check if there are pending actions for user
  124. -- @function modpol.orgs:has_pending_actions
  125. -- @param user User
  126. -- @return True if there are pending actions for a user, false if not
  127. function modpol.orgs:has_pending_actions(user)
  128. -- next() will return the next pair in a table
  129. -- if next() returns nil, the table is empty
  130. if not self.pending[user] then
  131. return false
  132. else
  133. if not next(self.pending[user]) then
  134. return false
  135. else
  136. return true
  137. end
  138. end
  139. end
  140. --- Interact a user with given process
  141. -- @function modpol.orgs:interact
  142. -- @param process_id
  143. -- @param user
  144. function modpol.orgs:interact(process_id, user)
  145. local process = self.processes[process_id]
  146. if self.pending[user] then
  147. local callback = self.pending[user][process_id]
  148. if callback and process ~= "deleted" then
  149. -- get data in case callback ends process
  150. local slug = self.processes[process_id].slug
  151. -- run callback
  152. process[callback](process, user)
  153. -- record org data
  154. local msg = "Updating "..slug..
  155. " process id "..process_id.." in org "..self.name
  156. self:record(msg, slug)
  157. end
  158. end
  159. end