process.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. --- Process functions for orgs
  2. function modpol.orgs:call_module(module_slug, initiator, config, result, parent_id)
  3. if not modpol.modules[module_slug] then
  4. modpol.ocutil.log('Error in ' .. self.name .. ':call_module -> module "' .. module_slug .. '" not found')
  5. return
  6. end
  7. local index = #self.processes + 1
  8. local module = modpol.modules[module_slug]
  9. -- sets default values for undeclared config variables
  10. if modpol.util.num_pairs(module.config) > 0 and config then
  11. for k, v in pairs(module.config) do
  12. if config[k] == nil then
  13. config[k] = v
  14. end
  15. end
  16. end
  17. -- setting default params
  18. local new_process = {
  19. metatable = {__index = module},
  20. initiator = initiator,
  21. org = self,
  22. id = index,
  23. parent_id = parent_id,
  24. children = {},
  25. config = config,
  26. data = modpol.util.copy_table(module.data),
  27. slug = module_slug
  28. }
  29. -- call module wrapper for modules, passes its id to child process when called
  30. function new_process:call_module(module_slug, initiator, config, result)
  31. local child_id = self.org:call_module(module_slug, initiator, config, result, self.id)
  32. table.insert(self.children, child_id)
  33. end
  34. setmetatable(new_process, new_process.metatable)
  35. self.processes[index] = new_process
  36. self.processes[index]:initiate(result)
  37. local msg = "Initiating "..module_slug..
  38. " process id "..index.." in org "..self.name
  39. return index
  40. end
  41. function modpol.orgs:get_root_process(id)
  42. local process = self.processes[id]
  43. while (process.parent_id) do
  44. process = self.processes[process.parent_id]
  45. end
  46. return process
  47. end
  48. function modpol.orgs:delete_process(id)
  49. local process = self.processes[id]
  50. if process and process ~= "deleted" then
  51. -- recursively deletes any children
  52. if #process.children > 0 then
  53. for i, child_id in pairs(process.children) do
  54. self:delete_process(child_id)
  55. end
  56. end
  57. local msg = "Deleting " .. self.processes[id].slug .. " process id "..id.." in org "..self.name
  58. modpol.ocutil.log(msg)
  59. self:record(msg, self.processes[id].slug)
  60. self:wipe_pending_actions(id)
  61. -- sets process to 'deleted' in process table
  62. self.processes[id] = 'deleted'
  63. end
  64. end
  65. function modpol.orgs:delete_process_tree(id)
  66. self:delete_process(self:get_root_process(id).id)
  67. end
  68. function modpol.orgs:add_pending_action(process_id, user, callback)
  69. self.pending[user] = self.pending[user] or {}
  70. self.pending[user][process_id] = callback
  71. modpol.interactions.message(
  72. user, "New pending action in org "..self.name)
  73. end
  74. function modpol.orgs:remove_pending_action(process_id, user)
  75. if self.pending[user] then
  76. self.pending[user][process_id] = nil
  77. end
  78. end
  79. function modpol.orgs:wipe_pending_actions(process_id)
  80. for user in pairs(self.pending) do
  81. self.pending[user][process_id] = nil
  82. end
  83. end
  84. function modpol.orgs:has_pending_actions(user)
  85. -- next() will return the next pair in a table
  86. -- if next() returns nil, the table is empty
  87. if not self.pending[user] then
  88. return false
  89. else
  90. if not next(self.pending[user]) then
  91. return false
  92. else
  93. return true
  94. end
  95. end
  96. end
  97. function modpol.orgs:interact(process_id, user)
  98. local process = self.processes[process_id]
  99. if self.pending[user] then
  100. local callback = self.pending[user][process_id]
  101. if callback and process ~= "deleted" then
  102. -- get data in case callback ends process
  103. local slug = self.processes[process_id].slug
  104. -- run callback
  105. process[callback](process, user)
  106. -- record org data
  107. local msg = "Updating "..slug..
  108. " process id "..process_id.." in org "..self.name
  109. self:record(msg, slug)
  110. end
  111. end
  112. end