process.lua 5.3 KB

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