process.lua 5.1 KB

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