diff --git a/modpol_core/orgs/process.lua b/modpol_core/orgs/process.lua index 476447d..25c7ba6 100644 --- a/modpol_core/orgs/process.lua +++ b/modpol_core/orgs/process.lua @@ -1,27 +1,12 @@ --- Process functions for orgs -function modpol.orgs:call_module(module_slug, initiator, config, result) +function modpol.orgs:call_module(module_slug, initiator, config, result, parent_id) if not modpol.modules[module_slug] then modpol.ocutil.log('Error in ' .. self.name .. ':call_module -> module "' .. module_slug .. '" not found') return end - local empty_index = nil - -- linear search for empty process slots (lazy deletion) - for k, v in ipairs(self.processes) do - if v == 'deleted' then - empty_index = k - break - end - end - - local index - -- attempts to fill empty spots in list, otherwise appends to end - if empty_index then - index = empty_index - else - index = #self.processes + 1 - end + local index = #self.processes + 1 local module = modpol.modules[module_slug] @@ -34,18 +19,25 @@ function modpol.orgs:call_module(module_slug, initiator, config, result) end end - -- setting default params local new_process = { metatable = {__index = module}, initiator = initiator, org = self, id = index, + parent_id = parent_id, + children = {}, config = config, data = modpol.util.copy_table(module.data), slug = module_slug } + -- call module wrapper for modules, passes its id to child process when called + function new_process:call_module(module_slug, initiator, config, result) + local child_id = self.org:call_module(module_slug, initiator, config, result, self.id) + table.insert(self.children, child_id) + end + setmetatable(new_process, new_process.metatable) self.processes[index] = new_process @@ -57,14 +49,21 @@ function modpol.orgs:call_module(module_slug, initiator, config, result) end function modpol.orgs:delete_process(id) - if self.processes[id] - and self.processes[id] ~= "deleted" then - local msg = "Deleting "..self.processes[id].slug.. - " process id "..id.." in org "..self.name - self:record(msg, self.processes[id].slug) - self:wipe_pending_actions(id) - self.processes[id] = 'deleted' - end + local process = self.processes[id] + if process and process ~= "deleted" then + -- recursively deletes any children + if #process.children > 0 then + for i, child_id in pairs(process.children) do + self:delete_process(child_id) + end + end + local msg = "Deleting " .. self.processes[id].slug .. " process id "..id.." in org "..self.name + modpol.ocutil.log(msg) + self:record(msg, self.processes[id].slug) + self:wipe_pending_actions(id) + -- sets process to 'deleted' in process table + self.processes[id] = 'deleted' + end end function modpol.orgs:add_pending_action(process_id, user, callback)