From 5a2e6277e5177547b655e3aadc99b48f572fef22 Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Wed, 29 Dec 2021 21:10:00 -0500 Subject: [PATCH 1/2] added a module version of call_module which automatically handles parent ids in the backend --- modpol_core/orgs/process.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/modpol_core/orgs/process.lua b/modpol_core/orgs/process.lua index 9ebb03b..5f9d170 100644 --- a/modpol_core/orgs/process.lua +++ b/modpol_core/orgs/process.lua @@ -1,6 +1,6 @@ --- 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 @@ -34,18 +34,23 @@ 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, config = config, data = 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) + self.org:call_module(module_slug, initiator, config, result, self.id) + end + setmetatable(new_process, new_process.metatable) self.processes[index] = new_process From 5788ce199a2e84ef0023a80707a17cf387ef9725 Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Wed, 5 Jan 2022 18:10:38 -0500 Subject: [PATCH 2/2] added parent/children ids to processes, modified delete process to be recursive, modules now have access to self:call_module --- modpol_core/orgs/process.lua | 44 ++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/modpol_core/orgs/process.lua b/modpol_core/orgs/process.lua index 5f9d170..7a0976c 100644 --- a/modpol_core/orgs/process.lua +++ b/modpol_core/orgs/process.lua @@ -6,22 +6,7 @@ function modpol.orgs:call_module(module_slug, initiator, config, result, parent_ 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] @@ -41,6 +26,7 @@ function modpol.orgs:call_module(module_slug, initiator, config, result, parent_ org = self, id = index, parent_id = parent_id, + children = {}, config = config, data = module.data, slug = module_slug @@ -48,7 +34,8 @@ function modpol.orgs:call_module(module_slug, initiator, config, result, parent_ -- call module wrapper for modules, passes its id to child process when called function new_process:call_module(module_slug, initiator, config, result) - self.org:call_module(module_slug, initiator, config, result, self.id) + 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) @@ -62,14 +49,21 @@ function modpol.orgs:call_module(module_slug, initiator, config, result, parent_ 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)