process.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. function modpol.orgs:get_root_process(id)
  49. local process = self.processes[id]
  50. while (process.parent_id) do
  51. process = self.processes[process.parent_id]
  52. end
  53. return process
  54. end
  55. function modpol.orgs:delete_process(id)
  56. local process = self.processes[id]
  57. if process and process ~= "deleted" then
  58. -- recursively deletes any children
  59. if #process.children > 0 then
  60. for i, child_id in pairs(process.children) do
  61. self:delete_process(child_id)
  62. end
  63. end
  64. local msg = "Deleting " .. self.processes[id].slug .. " process id "..id.." in org "..self.name
  65. modpol.ocutil.log(msg)
  66. self:record(msg, self.processes[id].slug)
  67. self:wipe_pending_actions(id)
  68. -- sets process to 'deleted' in process table
  69. self.processes[id] = 'deleted'
  70. end
  71. end
  72. --- Delete process tree by id
  73. -- @function modpol.orgs:delete_process_tree
  74. -- @param id Id of process tree
  75. function modpol.orgs:delete_process_tree(id)
  76. self:delete_process(self:get_root_process(id).id)
  77. end
  78. --- Add a new pending action
  79. -- @function modpol.orgs:add_pending_action
  80. -- @param process_id Process id
  81. -- @param user User adding the action
  82. -- @param callback
  83. function modpol.orgs:add_pending_action(process_id, user, callback)
  84. self.pending[user] = self.pending[user] or {}
  85. self.pending[user][process_id] = callback
  86. modpol.interactions.message(
  87. user, "New pending action in org "..self.name)
  88. end
  89. --- Remove a pending action
  90. -- @function modpol.orgs:remove_pending_action
  91. -- @param process_id Process id to be removed
  92. -- @param user
  93. function modpol.orgs:remove_pending_action(process_id, user)
  94. if self.pending[user] then
  95. self.pending[user][process_id] = nil
  96. end
  97. end
  98. --- Wipe all pending actions for process
  99. -- @function modpol.orgs:wipe_pending_actions
  100. -- @param process_id
  101. function modpol.orgs:wipe_pending_actions(process_id)
  102. for user in pairs(self.pending) do
  103. self.pending[user][process_id] = nil
  104. end
  105. end
  106. --- Check if there are pending actions for user
  107. -- @function modpol.orgs:has_pending_actions
  108. -- @param user User
  109. -- @return True if there are pending actions for a user, false if not
  110. function modpol.orgs:has_pending_actions(user)
  111. -- next() will return the next pair in a table
  112. -- if next() returns nil, the table is empty
  113. if not self.pending[user] then
  114. return false
  115. else
  116. if not next(self.pending[user]) then
  117. return false
  118. else
  119. return true
  120. end
  121. end
  122. end
  123. --- Interact a user with given process
  124. -- @function modpol.orgs:interact
  125. -- @param process_id
  126. -- @param user
  127. function modpol.orgs:interact(process_id, user)
  128. local process = self.processes[process_id]
  129. if self.pending[user] then
  130. local callback = self.pending[user][process_id]
  131. if callback and process ~= "deleted" then
  132. -- get data in case callback ends process
  133. local slug = self.processes[process_id].slug
  134. -- run callback
  135. process[callback](process, user)
  136. -- record org data
  137. local msg = "Updating "..slug..
  138. " process id "..process_id.." in org "..self.name
  139. self:record(msg, slug)
  140. end
  141. end
  142. end