Browse Source

Use fork() again for watcher.

scossu 4 days ago
parent
commit
7dce6cb3f5
1 changed files with 40 additions and 11 deletions
  1. 40 11
      src/util/watcher.lua

+ 40 - 11
src/util/watcher.lua

@@ -63,7 +63,6 @@ cli.program {
         local wd = watchdog.init()
         logger:setlevel(args.loglevel or logger:getlevel())
 
-        logger:info("")
         logger:info("* * * * * * * * * *")
         logger:info("Starting Pocket Archive watchdog on ", args.path)
         logger:info("Config file: ", pkar.config_path)
@@ -72,22 +71,52 @@ cli.program {
         if args.gen_site then
             logger:info("Generating website at: ", pkar.config.pres_gen.out_dir)
         end
-        logger:info("* * * * * * * * * *")
+        logger:info("* * * * * * * * * *\n")
 
         wd:add(args.path, watchdog.IN_CLOSE_WRITE, function(ev)
-            logger:info("Created new file: ", ev.name, " mask: ", ev.mask)
+            logger:info("Received new file: ", ev.name, " mask: ", ev.mask)
             logger:info("\n")
             if ev.name:find("pkar_submission.*%.csv") then
                 logger:info("Detected new submission file: ", ev.name)
-                posix.spawn(function()
-                    local cpid = unistd.getpid()
-                    logger:info("Starting deposit process with pid: " .. cpid)
-                    sub.deposit(plpath.join(args.path, ev.name), args.cleanup)
-                    if args.gen_site then pres.generate_site() end
-                    logger:info(cpid .. ": sleeping...")
+                -- FIXME this raises EAGAIN in Volksdata / LMDB if more than
+                -- one submission is run in parallel.
+                local cpid, err = unistd.fork()
+                if cpid == nil then
+                    logger:error("Failed to fork submission process: " .. err)
+                elseif cpid == 0 then
+                    local mypid = unistd.getpid()
+                    logger:info("Starting submission with pid: " .. mypid)
+                    local sub_rc, sub_ret = pcall(
+                        sub.deposit,
+                        plpath.join(args.path, ev.name),
+                        args.cleanup
+                    )
+                    if sub_rc then
+                        local report = sub_ret
+
+                    else
+                        logger:error("Submission process failed: " .. sub_rc)
+                        goto finally
+                    end
+
+                    if args.gen_site then
+                        local gen_rc, gen_ret = pcall(pres.generate_site)
+                        if not gen_rc then
+                            logger:error(
+                                "Site generation process failed: " .. gen_rc)
+                            goto finally
+                        end
+                    end
+                    --[[
+                    logger:info(mypid .. ": sleeping...")
                     require "socket".sleep(3)
-                    logger:info(cpid .. ": complete.")
-                end, "r")
+                    logger:info(mypid .. ": complete.")
+                    --]]
+
+                    ::finally::
+                    os.exit(exit_code)
+                end
+                -- Else: main process keeps on running.
             end
         end)