start of mojo conversion
[spider.git] / perl / cluster.pl
index bec097821a7fd6dcf3046e29594f6e451c9d1284..a3e915a614ebb45e41686a7ef7162f1d4aff6738 100755 (executable)
@@ -34,11 +34,14 @@ BEGIN {
        # try to create and lock a lockfile (this isn't atomic but
        # should do for now
        $lockfn = "$root/local/cluster.lck";       # lock file name
-       if (-e $lockfn) {
+       if (-w $lockfn) {
                open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
                my $pid = <CLLOCK>;
-               chomp $pid;
-               die "Lockfile ($lockfn) and process $pid exist, another cluster running?" if kill 0, $pid;
+               if ($pid) {
+                       chomp $pid;
+                       die "Lockfile ($lockfn) and process $pid exist, another cluster running?" if kill 0, $pid;
+               }
+               unlink $lockfn;
                close CLLOCK;
        }
        open(CLLOCK, ">$lockfn") or die "Can't open Lockfile ($lockfn) $!";
@@ -49,6 +52,8 @@ BEGIN {
        $systime = time;
 }
 
+use Mojo::IOLoop;
+
 use DXVars;
 use Msg;
 use IntMsg;
@@ -118,7 +123,7 @@ use vars qw(@inqueue $systime $starttime $lockfn @outstanding_connects
                        $zombies $root @listeners $lang $myalias @debug $userfn $clusteraddr
                        $clusterport $mycall $decease $is_win $routeroot $me $reqreg $bumpexisting
                        $allowdxby $dbh $dsn $dbuser $dbpass $do_xml $systime_days $systime_daystart
-                       $can_encode $maxconnect_user $maxconnect_node
+                       $can_encode $maxconnect_user $maxconnect_node $idle_interval
                   );
 
 @inqueue = ();                                 # the main input queue, an array of hashes
@@ -133,6 +138,7 @@ $maxconnect_user = 3;                       # the maximum no of concurrent connections a user can ha
 $maxconnect_node = 0;                  # Ditto but for nodes. In either case if a new incoming connection
                                                                # takes the no of references in the routing table above these numbers
                                                                # then the connection is refused. This only affects INCOMING connections.
+$idle_interval = 0.100;                        # the wait between invocations of the main idle loop processing.
 
 # send a message to call on conn and disconnect
 sub already_conn
@@ -175,7 +181,7 @@ sub new_channel
                        return;
                }
                if ($bumpexisting) {
-                       my $ip = $conn->{peerhost} || 'unknown';
+                       my $ip = $conn->peerhost || 'unknown';
                        $dxchan->send_now('D', DXM::msg($lang, 'conbump', $call, $ip));
                        LogDbg('DXCommand', "$call bumped off by $ip, disconnected");
                        $dxchan->disconnect;
@@ -208,7 +214,7 @@ sub new_channel
        my $lock = $user->lockout if $user;
        if ($baseuser && $baseuser->lockout || $lock) {
                if (!$user || !defined $lock || $lock) {
-                       my $host = $conn->{peerhost} || "unknown";
+                       my $host = $conn->peerhost || "unknown";
                        LogDbg('DXCommand', "$call on $host is locked out, disconnected");
                        $conn->disconnect;
                        return;
@@ -270,7 +276,6 @@ sub cease
        foreach $dxchan (DXChannel::get_all_nodes) {
            $dxchan->disconnect(2) unless $dxchan == $main::me;
        }
-       Msg->event_loop(100, 0.01);
 
        # disconnect users
        foreach $dxchan (DXChannel::get_all_users) {
@@ -285,7 +290,6 @@ sub cease
        UDPMsg::finish();
 
        # end everything else
-       Msg->event_loop(100, 0.01);
        DXUser::finish();
        DXDupe::finish();
 
@@ -297,7 +301,8 @@ sub cease
                $l->close_server;
        }
 
-       LogDbg('cluster', "DXSpider V$version, build $subversion.$build ended");
+       LogDbg('cluster', "DXSpider V$version, build $subversion.$build (git: $gitversion) ended");
+       dbg("bye bye everyone - bye bye");
        dbgclose();
        Logclose();
 
@@ -339,6 +344,60 @@ sub AGWrestart
        AGWMsg::init(\&new_channel);
 }
 
+sub idle_loop
+{
+       my $timenow = time;
+
+       DXChannel::process();
+
+       #      $DB::trace = 0;
+
+       # do timed stuff, ongoing processing happens one a second
+       if ($timenow != $systime) {
+               reap() if $zombies;
+               $systime = $timenow;
+               my $days = int ($systime / 86400);
+               if ($systime_days != $days) {
+                       $systime_days = $days;
+                       $systime_daystart = $days * 86400;
+               }
+               IsoTime::update($systime);
+               DXCron::process();      # do cron jobs
+               DXCommandmode::process(); # process ongoing command mode stuff
+               DXXml::process();
+               DXProt::process();              # process ongoing ak1a pcxx stuff
+               DXConnect::process();
+               DXMsg::process();
+               DXDb::process();
+               DXUser::process();
+               DXDupe::process();
+               $systime_days = $days;
+               $systime_daystart = $days * 86400;
+       }
+       IsoTime::update($systime);
+       DXCron::process();                      # do cron jobs
+       DXCommandmode::process();       # process ongoing command mode stuff
+       DXXml::process();
+       DXProt::process();                      # process ongoing ak1a pcxx stuff
+       DXConnect::process();
+       DXMsg::process();
+       DXDb::process();
+       DXUser::process();
+       DXDupe::process();
+       AGWMsg::process();
+       BPQMsg::process();
+
+       Timer::handler();
+
+       if (defined &Local::process) {
+               eval {
+                       Local::process();       # do any localised processing
+               };
+               dbg("Local::process error $@") if $@;
+       }
+}
+
+
 #############################################################
 #
 # The start of the main line of code
@@ -386,7 +445,7 @@ DXXml::init();
 # banner
 my ($year) = (gmtime)[5];
 $year += 1900;
-LogDbg('cluster', "DXSpider V$version, build $subversion.$build started");
+LogDbg('cluster', "DXSpider V$version, build $subversion.$build (git: $gitversion) started");
 dbg("Copyright (c) 1998-$year Dirk Koopman G1TLH");
 
 # load Prefixes
@@ -443,7 +502,7 @@ dbg("load badwords: " . (BadWords::load or "Ok"));
 
 # prime some signals
 unless ($DB::VERSION) {
-       $SIG{INT} = $SIG{TERM} = sub { $decease = 1 };
+       $SIG{INT} = $SIG{TERM} = sub { Mojo::IOLoop->stop; };
 }
 
 unless ($is_win) {
@@ -532,49 +591,8 @@ $script->run($main::me) if $script;
 
 #open(DB::OUT, "|tee /tmp/aa");
 
-for (;;) {
-#      $DB::trace = 1;
-
-       Msg->event_loop(10, 0.010);
-       my $timenow = time;
-
-       DXChannel::process();
-
-#      $DB::trace = 0;
+Mojo::IOLoop->start;
 
-       # do timed stuff, ongoing processing happens one a second
-       if ($timenow != $systime) {
-               reap() if $zombies;
-               $systime = $timenow;
-               my $days = int ($systime / 86400);
-               if ($systime_days != $days) {
-                       $systime_days = $days;
-                       $systime_daystart = $days * 86400;
-               }
-               IsoTime::update($systime);
-               DXCron::process();      # do cron jobs
-               DXCommandmode::process(); # process ongoing command mode stuff
-               DXXml::process();
-               DXProt::process();              # process ongoing ak1a pcxx stuff
-               DXConnect::process();
-               DXMsg::process();
-               DXDb::process();
-               DXUser::process();
-               DXDupe::process();
-               AGWMsg::process();
-               BPQMsg::process();
-
-               if (defined &Local::process) {
-                       eval {
-                               Local::process();       # do any localised processing
-                       };
-                       dbg("Local::process error $@") if $@;
-               }
-       }
-       if ($decease) {
-               last if --$decease <= 0;
-       }
-}
 cease(0);
 exit(0);