Przeglądaj źródła

upstream monitor code into tPSGI

George Baugh 5 miesięcy temu
rodzic
commit
7f76fb9cc5
3 zmienionych plików z 0 dodań i 145 usunięć
  1. 0 75
      bin/tcms_monitor
  2. 0 63
      lib/Trog/Autoreload.pm
  3. 0 7
      lib/Trog/Utils.pm

+ 0 - 75
bin/tcms_monitor

@@ -1,75 +0,0 @@
-#!/usr/bin/env perl
-
-use FindBin::libs;
-use FindBin;
-use Proc::Daemon;
-
-use strict;
-use warnings;
-
-use Trog::Autoreload;
-
-# Note that we do not reload ourselves.
-
-sub cleanup {
-    unlink 'run/monitor.pid';
-    return 0;
-};
-
-$SIG{HUP} = sub {
-    print "Recieved signal from tcms master process, restarting.\n";
-    cleanup();
-    exec $0;
-};
-
-sub get_master_pid {
-    # Get the PID of the current master process
-    my $ppf = 'run/tcms.pid';
-    my $ppid;
-    if (-f $ppf) {
-        open(my $pidfile, '<', $ppf);
-        read($pidfile, $ppid, 10000);
-        close $pidfile;
-        chomp $ppid;
-        if (!-d "/proc/$ppid") {
-            print "tCMS not running, but left a PIDFile behind. Exiting monitor.\n";
-            exit 0 if -f "/run/$ppid";
-        }
-    } else {
-        print "tCMS not running, exiting monitor.\n";
-        exit 0;
-    }
-    return $ppid;
-}
-
-sub main {
-    my $pf = 'run/monitor.pid';
-    my $monitor_pid = Trog::Autoreload::monitor_pid();
-    if ($monitor_pid) {
-        print "Change monitor already running, killing and re-starting.\n";
-        kill 'TERM', $monitor_pid;
-    }
-
-    # daemonize immediately.  We are handling the pidfile ourselves, see below
-    my $daemon = Proc::Daemon->new(
-        work_dir => "$FindBin::Bin/..",
-        pid_file => $pf,
-        child_STDOUT => 'logs/monitor.log',
-        child_STDERR => 'logs/monitor.err',
-    );
-    my $child_pid = $daemon->Init;
-    return 0 if $child_pid;
-
-    my $ppid = get_master_pid();
-    # If we go here, we (probably) have a PID, so watch for the changes.
-    my $status = Trog::Autoreload::watch_for_changes('lib', $ppid);
-    if ($status) {
-        # tCMS process went away.  Bye bye!
-        print "tCMS process went away, shutting down monitor\n";
-        return cleanup();
-    }
-    # By now we should have recieved SIGHUP and are already dead, so exit anyways.
-    return cleanup();
-}
-
-exit main(@ARGV) unless caller;

+ 0 - 63
lib/Trog/Autoreload.pm

@@ -1,63 +0,0 @@
-package Trog::Autoreload;
-
-use strict;
-use warnings;
-
-no warnings qw{experimental};
-use feature qw{signatures};
-
-use Cwd qw{abs_path};
-use Time::HiRes qw{usleep};
-use Linux::Perl::inotify;
-
-use Trog::Utils;
-
-sub watch_for_changes ( $dir, $pid=undef ) {
-    my @dirs = (abs_path($dir));
-    my @wds;
-
-    my $inf = Linux::Perl::inotify->new(flags => [qw{NONBLOCK}]);
-    foreach my $directory (@dirs) {
-        # Recursive scan for directories and setting up inotifies
-        push(@dirs, _readdir( $directory ));
-        print "Watching $directory for changes\n";
-        push(@wds, $inf->add( path => $directory, events => [qw{CREATE MODIFY}] ));
-    }
-    my @result;
-    CHECK: while (1) {
-        @result = $inf->read();
-        foreach my $res (@result) {
-            last CHECK if $res->{name} =~ m/\.pm$/;
-        }
-        usleep 100000; #100ms oughtta be enough
-        # If we know the pid, stop watching when it dies.
-        if ($pid && !-d "/proc/$pid") {
-            foreach my $wd (@wds) { $inf->remove($wd) }
-            return 1;
-        }
-    }
-    print "Relevant Change in $dir detected, reloading\n";
-    Trog::Utils::restart_parent($pid);
-    foreach my $wd (@wds) { $inf->remove($wd) }
-    return 0;
-}
-
-sub _readdir ( $dir ) {
-    opendir(my $dh, $dir);
-    my @dirs = map { "$dir/$_" } grep { -d "$dir/$_" && !m/^\.+$/ } readdir($dh);
-    closedir($dh);
-    return @dirs;
-}
-
-sub monitor_pid {
-    my $pf = 'run/monitor.pid';
-    if (-f $pf) {
-        open(my $pidfile, '<', $pf);
-        read($pidfile, my $pid, 10000);
-        close $pidfile;
-        return $pid if -d "/proc/$pid";
-    }
-    return 0;
-}
-
-1;

+ 0 - 7
lib/Trog/Utils.pm

@@ -13,7 +13,6 @@ use Mojo::File;
 use File::LibMagic;
 use Ref::Util qw{is_hashref};
 
-use Trog::Autoreload;
 use Trog::Log qw{WARN};
 use Trog::Config();
 
@@ -45,12 +44,6 @@ sub restart_parent ( $parent=undef ) {
     }
     $parent //= getppid;
     kill 'HUP', $parent;
-
-    # Restart the monitor if it is lurking
-    my $monitor_pid = Trog::Autoreload::monitor_pid();
-    if ($monitor_pid) {
-        kill 'HUP', $monitor_pid;
-    }
 }
 
 sub uuid {