process.lua 5.6 KB

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