Browse Source

added parent/children ids to processes, modified delete process to be recursive, modules now have access to self:call_module

Luke Miller 2 years ago
parent
commit
5788ce199a
1 changed files with 19 additions and 25 deletions
  1. 19 25
      modpol_core/orgs/process.lua

+ 19 - 25
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)