Add --stop suboption for --coordinator mode to gracefully take down a running coordinator
authorMike Rylander <mrylander@gmail.com>
Thu, 6 Oct 2022 19:24:11 +0000 (15:24 -0400)
committerMike Rylander <mrylander@gmail.com>
Thu, 6 Oct 2022 19:24:11 +0000 (15:24 -0400)
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Open-ILS/src/support-scripts/ingest_ctl

index 2a38fe8..4df5891 100755 (executable)
@@ -30,6 +30,7 @@ my $raise_db_error = 1;
 my $opt_lockfile      = '/tmp/queued-ingest-coordinator-LOCK';
 my $opt_logfile       = '/tmp/queued-ingest-coordinator-LOG';
 my $daemon     = 0;     # do we go into the background?
+my $stop       = 0;     # stop a running coordinator, if the lock file is there
 my $chatty     = 0;     # do we yell into the void?
 my $opt_max_child;      # max number of parallel worker processes
 my $max_child  = 20;    # max number of parallel worker processes
@@ -87,6 +88,7 @@ GetOptions(
     'end-id=i'          => \$end_id,
     'pipe'              => \$opt_pipe,
     'coordinator'       => \$daemon,
+    'stop'              => \$stop,
     'chatty'            => \$chatty,
     'help'              => \$help
 );
@@ -105,6 +107,9 @@ sub help {
     # Start the background worker
     $0 --coordinator --max-child $max_child
 
+    # Stop the background worker
+    $0 --coordinator --stop
+
     # Process whatever you can Right Now
     $0 --max-child $max_child
 
@@ -248,12 +253,17 @@ if ($opt_pipe && ($start_id || $end_id)) {
 }
 
 if ($daemon && ($start_id || $end_id || $opt_pipe)) {
-    warn('Mutually exclusive options: cannot start the Coordinator in Enqueuing mode');
+    warn('Mutually exclusive options: cannot start or stop the Coordinator in Enqueuing mode');
+    help();
+}
+
+if (!$daemon && $stop) {
+    warn('Option --stop can only be used with the --coordinator option');
     help();
 }
 
 if ($daemon && $queue) {
-    warn('Mutually exclusive options: cannot start the Coordinator in one-shot processing mode');
+    warn('Mutually exclusive options: cannot start or stop the Coordinator in one-shot processing mode');
     help();
 }
 
@@ -275,6 +285,34 @@ if ($queue && ($queue_owner || $queue_why || $queue_threads || $queue_run_at)) {
 
 if ($daemon) { # background mode, we need a lockfile;
 
+    if ($stop) {
+        die "Lockfile $opt_lockfile does not exist, is the coordinator running?\n" unless (-e $opt_lockfile);
+
+        open(F, "<$opt_lockfile") or die "Unable to open lockfile $opt_lockfile for reading, wrong user?\n";
+        my $old_pid = <F>;
+        close F;
+
+        if ($old_pid) {
+            if (kill(0,$old_pid)) {
+                my $dead_count = kill(9,$old_pid);
+                if ($dead_count) {
+                    warn "Coordinator process terminated, removing lock file $opt_lockfile\n";
+                    unlink($opt_lockfile) or die "Could not remove lock file $opt_lockfile\n";
+                } else {
+                    die "Could not kill coordinator process $old_pid\n";
+                }
+            } else {
+                warn "Coordinator process not running, removing stale lock file\n";
+                unlink($opt_lockfile) or die "Could not remove lock file $opt_lockfile\n";
+            }
+        } else {
+            warn "Coordinator lock file empty, removing lock file $opt_lockfile\n";
+            unlink($opt_lockfile) or die "Could not remove lock file $opt_lockfile\n";
+        }
+
+        exit;
+    }
+
     # check the lockfile
     die "I'm already running with lock-file $opt_lockfile\n" if (-e $opt_lockfile);