sub pm_wait { my ($this) = self_or_default(@_); # wait for the next server to die. next if (my $pid = wait()) < 0; # notify when one of our servers have died. delete $this->{PIDS}->{$pid} and $this->pm_notify("server (pid $pid) exited with status $?"); return $pid; }
use FCGI; use FCGI::ProcManager; use IO::Select; use IO::Socket; use POSIX ":sys_wait_h"; #use Module::Refresh; use feature ':5.14'; use strict; # For fastcgi app my $socket = FCGI::OpenSocket( ":9019", 5 ); my $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%ENV, $socket, FCGI::FAIL_ACCEPT_ON_INTR ); # To communicate with the manager my $server = IO::Socket::INET->new(LocalPort => '9034', Type => SOCK_STREAM, Reuse => 1, Listen => 1) or die "Couldn't start messange server $@\n"; my $select = IO::Select->new($server); # Patch my $pm_wait = sub { my ($pm) = @_; my $pid; while (1) { last if ($pid = waitpid(-1, WNOHANG)); foreach my $client ($select->can_read(1)) { if ($client == $server) { $client = $server->accept(); $select->add($client); } else { my $msg = <$client>; chomp $msg; if ($msg eq 'reload') { #my $refresher = Module::Refresh->new(); #my @module_list = qw( MyLib1.pm My/Lib2.pm ); #foreach my $module_name (@module_list){ # $refresher->refresh_module($module_name); #} $pm->sig_manager('HUP'); } print $client "done!\n"; $select->remove($client); close $client; } } } # notify when one of our servers have died. delete $pm->{PIDS}->{$pid} and $pm->pm_notify("server (pid $pid) exited with status $?"); return $pid; }; no strict 'refs'; *{'FCGI::ProcManager::pm_wait'} = $pm_wait; use strict 'refs'; my $pm = FCGI::ProcManager->new( ); $pm->pm_manage( n_processes => 3, pm_title => 'perl-fcgi-pm', die_timeout => 10 ); while ( $request->Accept() >= 0 ) { $pm->pm_pre_dispatch(); print "Content-Type: text/plain\n\n"; print "server works"; $pm->pm_post_dispatch(); } FCGI::CloseSocket($socket); close $server;
use IO::Socket; use feature ':5.14'; use strict; my $socket = new IO::Socket::INET(Proto => "tcp", Type => SOCK_STREAM, PeerPort => 9034, PeerAddr => "127.0.0.1" ) or die "Can't connect: $!"; #my $msg = $ARGV[0]; my $msg = 'reload'; if ($msg) { print $socket "$msg\n"; my $answer = <$socket>; say "Answer: ".$answer; }
$pm->{n_processes} = 10; $pm->sig_manager('HUP');
Source: https://habr.com/ru/post/124691/
All Articles