X-Git-Url: http://gb7djk.dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=perl%2Fcluster.pl;h=1e10eb518aae676ddbe0d8b85df3825951e52455;hb=36e0c1ffda9295a4090eab75360f1b59d964ada3;hp=25b84a9cec3ade5663a27a4fe7ca02eb62a8556f;hpb=c1540ccd7990ec4bd151604dd63583d19fe4d0f6;p=spider.git diff --git a/perl/cluster.pl b/perl/cluster.pl index 25b84a9c..1e10eb51 100755 --- a/perl/cluster.pl +++ b/perl/cluster.pl @@ -10,7 +10,7 @@ # # -require 5.004; +require 5.10.1; # make sure that modules are searched in the order local then perl BEGIN { @@ -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 = ; - 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 + $can_encode $maxconnect_user $maxconnect_node $idle_interval $log_flush_interval ); @inqueue = (); # the main input queue, an array of hashes @@ -129,6 +134,14 @@ $starttime = 0; # the starting time of the cluster $reqreg = 0; # 1 = registration required, 2 = deregister people $bumpexisting = 1; # 1 = allow new connection to disconnect old, 0 - don't allow it $allowdxby = 0; # 1 = allow "dx by ", 0 - don't allow it +$maxconnect_user = 3; # the maximum no of concurrent connections a user can have at a time +$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.500; # the wait between invocations of the main idle loop processing. +$log_flush_interval = 2; # interval to wait between log flushes + +our $ending; # signal that we are ending; # send a message to call on conn and disconnect @@ -143,13 +156,6 @@ sub already_conn $conn->disconnect; } -sub error_handler -{ - my $dxchan = shift; - $dxchan->{conn}->set_error(undef) if exists $dxchan->{conn}; - $dxchan->disconnect(1); -} - # handle incoming messages sub new_channel { @@ -168,11 +174,11 @@ sub new_channel my $dxchan = DXChannel::get($call); if ($dxchan) { if ($user && $user->is_node) { - already_conn($conn, $call, DXM::msg($lang, 'concluster', $call, $main::mycall)); + already_conn($conn, $call, DXM::msg($lang, 'conother', $call, $main::mycall)); 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; @@ -182,6 +188,22 @@ sub new_channel } } + # (fairly) politely disconnect people that are connected to too many other places at once + my $r = Route::get($call); + if ($conn->{sort} && $conn->{sort} =~ /^I/ && $r && $user) { + my @n = $r->parents; + my $m = $r->isa('Route::Node') ? $maxconnect_node : $maxconnect_user; + my $c = $user->maxconnect; + my $v; + $v = defined $c ? $c : $m; + if ($v && @n >= $v) { + my $nodes = join ',', @n; + LogDbg('DXCommand', "$call has too many connections ($v) at $nodes - disconnected"); + already_conn($conn, $call, DXM::msg($lang, 'contomany', $call, $v, $nodes)); + return; + } + } + # is he locked out ? my $basecall = $call; $basecall =~ s/-\d+$//; @@ -189,7 +211,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; @@ -217,7 +239,8 @@ sub new_channel $conn->conns($call) if $conn->isa('IntMsg'); # set callbacks - $conn->set_error(sub {error_handler($dxchan)}); + $conn->set_error(sub {my $err = shift; LogDbg('DXCommand', "Comms error '$err' received for call $dxchan->{call}"); $dxchan->disconnect(1);}); + $conn->set_on_eof(sub {$dxchan->disconnect}); $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg);}); $dxchan->rec($msg); } @@ -228,11 +251,17 @@ sub login return \&new_channel; } +our $ceasing; + # cease running this program, close down all the connections nicely sub cease { my $dxchan; + cluck("ceasing") if $ceasing; + + return if $ceasing++; + unless ($is_win) { $SIG{'TERM'} = 'IGNORE'; $SIG{'INT'} = 'IGNORE'; @@ -247,16 +276,6 @@ sub cease dbg("Local::finish error $@") if $@; } - # disconnect nodes - 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) { - $dxchan->disconnect; - } # disconnect AGW AGWMsg::finish(); @@ -266,7 +285,6 @@ sub cease UDPMsg::finish(); # end everything else - Msg->event_loop(100, 0.01); DXUser::finish(); DXDupe::finish(); @@ -278,15 +296,14 @@ sub cease $l->close_server; } - LogDbg('cluster', "DXSpider V$version, build $subversion.$build ended"); + LogDbg('cluster', "DXSpider V$version, build $build (git: $gitversion) ended"); + dbg("bye bye everyone - bye bye"); dbgclose(); Logclose(); $dbh->finish if $dbh; unlink $lockfn; -# $SIG{__WARN__} = $SIG{__DIE__} = sub {my $a = shift; cluck($a); }; - exit(0); } # the reaper of children @@ -320,6 +337,79 @@ sub AGWrestart AGWMsg::init(\&new_channel); } +our $io_disconnected; + +sub idle_loop +{ + my $timenow = time; + + BPQMsg::process(); +# 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(); + DXCron::process(); # do cron jobs + IsoTime::update($systime); + DXProt::process(); # process ongoing ak1a pcxx stuff + DXConnect::process(); + DXUser::process(); + AGWMsg::process(); + + Timer::handler(); + DXLog::flushall(); + } + + if (defined &Local::process) { + eval { + Local::process(); # do any localised processing + }; + dbg("Local::process error $@") if $@; + } + + while ($ending) { + my $dxchan; + + dbg("DXSpider Ending $ending"); + + unless ($io_disconnected++) { + + # disconnect users + foreach $dxchan (DXChannel::get_all_users) { + $dxchan->disconnect; + } + + # disconnect nodes + foreach $dxchan (DXChannel::get_all_nodes) { + next if $dxchan == $main::me; + $dxchan->disconnect(2); + } + $main::me->disconnect; + } + + Mojo::IOLoop->stop if --$ending <= 0; + } +} + + ############################################################# # # The start of the main line of code @@ -348,7 +438,7 @@ if (DXSql::init($dsn)) { $dbh = $dbh->connect($dsn, $dbuser, $dbpass) if $dbh; } -# try to load Encode +# try to load Encode and Git { local $^W = 0; my $w = $SIG{__DIE__}; @@ -358,6 +448,22 @@ if (DXSql::init($dsn)) { import Encode; $can_encode = 1; } + eval { require Git; }; + unless ($@) { + import Git; + + # determine the real version number + my $repo = Git->repository(Directory => "$root/.git"); + if ($repo) { + my $desc = $repo->command_oneline(['describe', '--long'], STDERR => 0); + if ($desc) { + my ($v, $s, $b, $g) = $desc =~ /^([\d.]+)(?:\.(\d+))?-(\d+)-g([0-9a-f]+)/; + $version = $v; + $build = $b || 0; + $gitversion = "$g\[r]"; + } + } + } $SIG{__DIE__} = $w; } @@ -367,7 +473,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 $build (git: $gitversion) started"); dbg("Copyright (c) 1998-$year Dirk Koopman G1TLH"); # load Prefixes @@ -386,6 +492,7 @@ DXUser->init($userfn, 1); # look for the sysop and the alias user and complain if they aren't there { + die "\$myalias \& \$mycall are the same ($mycall)!, they must be different (hint: make \$mycall = '${mycall}-2';). Oh and don't forget to rerun create_sysop.pl!" if $mycall eq $myalias; my $ref = DXUser::get($mycall); die "$mycall missing, run the create_sysop.pl script and please RTFM" unless $ref && $ref->priv == 9; $ref = DXUser::get($myalias); @@ -423,7 +530,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 { $ending = 10; }; } unless ($is_win) { @@ -512,50 +619,12 @@ $script->run($main::me) if $script; #open(DB::OUT, "|tee /tmp/aa"); -for (;;) { -# $DB::trace = 1; +my $main_loop = Mojo::IOLoop->recurring($idle_interval => \&idle_loop); +my $log_flush_loop = Mojo::IOLoop->recurring($log_flush_interval => \&DXLog::flushall); - Msg->event_loop(10, 0.010); - 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(); - AGWMsg::process(); - BPQMsg::process(); +Mojo::IOLoop->start unless Mojo::IOLoop->is_running; - if (defined &Local::process) { - eval { - Local::process(); # do any localised processing - }; - dbg("Local::process error $@") if $@; - } - } - if ($decease) { - last if --$decease <= 0; - } -} +dbg("After Mojo::IOLoop"); cease(0); exit(0); -