process.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. --- Process functions for orgs
  2. function modpol.orgs:call_module(module_slug, initiator, config, result)
  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 empty_index = nil
  8. -- linear search for empty process slots (lazy deletion)
  9. for k, v in ipairs(self.processes) do
  10. if v == 'deleted' then
  11. empty_index = k
  12. break
  13. end
  14. end
  15. local index
  16. -- attempts to fill empty spots in list, otherwise appends to end
  17. if empty_index then
  18. index = empty_index
  19. else
  20. index = #self.processes + 1
  21. end
  22. local module = modpol.modules[module_slug]
  23. -- sets default values for undeclared config variables
  24. if modpol.util.num_pairs(module.config) > 0 and config then
  25. for k, v in pairs(module.config) do
  26. if config[k] == nil then
  27. config[k] = v
  28. end
  29. end
  30. end
  31. -- setting default params
  32. local new_process = {
  33. metatable = {__index = module},
  34. initiator = initiator,
  35. org = self,
  36. id = index,
  37. config = config,
  38. data = module.data,
  39. slug = module_slug
  40. }
  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:delete_process(id)
  49. local msg = "Deleting "..self.processes[id].slug..
  50. " process id "..id.." in org "..self.name
  51. self:record(msg, self.processes[id].slug)
  52. self:wipe_pending_actions(id)
  53. self.processes[id] = 'deleted'
  54. end
  55. function modpol.orgs:add_pending_action(process_id, user, callback)
  56. self.pending[user] = self.pending[user] or {}
  57. self.pending[user][process_id] = callback
  58. modpol.interactions.message(
  59. user, "New pending action in org "..self.name)
  60. end
  61. function modpol.orgs:remove_pending_action(process_id, user)
  62. if self.pending[user] then
  63. self.pending[user][process_id] = nil
  64. end
  65. end
  66. function modpol.orgs:wipe_pending_actions(process_id)
  67. for user in pairs(self.pending) do
  68. self.pending[user][process_id] = nil
  69. end
  70. end
  71. function modpol.orgs:has_pending_actions(user)
  72. -- next() will return the next pair in a table
  73. -- if next() returns nil, the table is empty
  74. if not self.pending[user] then
  75. return false
  76. else
  77. if not next(self.pending[user]) then
  78. return false
  79. else
  80. return true
  81. end
  82. end
  83. end
  84. function modpol.orgs:interact(process_id, user)
  85. local process = self.processes[process_id]
  86. if self.pending[user] then
  87. local callback = self.pending[user][process_id]
  88. if callback then
  89. -- get data in case callback ends process
  90. local slug = self.processes[process_id].slug
  91. -- run callback
  92. process[callback](process, user)
  93. -- record org data
  94. local msg = "Updating "..slug..
  95. " process id "..process_id.." in org "..self.name
  96. self:record(msg, slug)
  97. end
  98. end
  99. end