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
'end-id=i' => \$end_id,
'pipe' => \$opt_pipe,
'coordinator' => \$daemon,
+ 'stop' => \$stop,
'chatty' => \$chatty,
'help' => \$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
}
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();
}
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);