process.lua 5.5 KB

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