rework zmcontrol.pl. If command is given and server is not up, use zmdc.pl to start it. Give up after 10 seconds.
This commit is contained in:
parent
e1873b1693
commit
aa83239069
|
@ -30,7 +30,7 @@ use autouse 'Pod::Usage'=>qw(pod2usage);
|
|||
use POSIX qw/strftime EPIPE/;
|
||||
use Socket;
|
||||
#use Data::Dumper;
|
||||
use Module::Load::Conditional qw{can_load};;
|
||||
use Module::Load::Conditional qw{can_load};
|
||||
|
||||
use constant MAX_CONNECT_DELAY => 15;
|
||||
use constant MAX_COMMAND_WAIT => 1800;
|
||||
|
@ -43,7 +43,7 @@ delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
|
|||
|
||||
logInit();
|
||||
|
||||
my $arg_string = join( " ", @ARGV );
|
||||
my $arg_string = join(' ', @ARGV);
|
||||
|
||||
my $id;
|
||||
my %options;
|
||||
|
@ -70,16 +70,40 @@ if ( !$id ) {
|
|||
|
||||
( $id ) = $id =~ /^(\w+)$/;
|
||||
|
||||
Debug("zmcontrol: arg string: $arg_string");
|
||||
|
||||
my $sock_file = $Config{ZM_PATH_SOCKS}.'/zmcontrol-'.$id.'.sock';
|
||||
Debug("zmcontrol: arg string: $arg_string sock file $sock_file");
|
||||
|
||||
socket(CLIENT, PF_UNIX, SOCK_STREAM, 0)
|
||||
or Fatal("Can't open socket: $!");
|
||||
socket(CLIENT, PF_UNIX, SOCK_STREAM, 0) or Fatal("Can't open socket: $!");
|
||||
|
||||
my $saddr = sockaddr_un($sock_file);
|
||||
my $server_up = connect(CLIENT, $saddr);
|
||||
if ( !$server_up ) {
|
||||
|
||||
if ( $options{command} ) {
|
||||
# Have a command, so we are the client, connect to the server and send it.
|
||||
|
||||
my $tries = 10;
|
||||
my $server_up;
|
||||
while ( $tries and ! ( $server_up = connect(CLIENT, $saddr) ) ) {
|
||||
Debug("Failed to connect to $server_up at $sock_file");
|
||||
runCommand("zmdc.pl start zmcontrol.pl --id=$id");
|
||||
sleep 1;
|
||||
$tries -= 1;
|
||||
}
|
||||
if ( $server_up ) {
|
||||
# The server is there, connect to it
|
||||
#print( "Writing commands\n" );
|
||||
CLIENT->autoflush();
|
||||
|
||||
if ( $options{command} ) {
|
||||
my $message = jsonEncode(\%options);
|
||||
print(CLIENT $message);
|
||||
}
|
||||
shutdown(CLIENT, 1);
|
||||
} else {
|
||||
Error("Unable to connect to zmcontrol server at $sock_file");
|
||||
}
|
||||
} else {
|
||||
|
||||
# The server isn't there
|
||||
my $monitor = zmDbGetMonitorAndControl($id);
|
||||
if ( !$monitor ) {
|
||||
|
@ -113,31 +137,11 @@ if ( !$server_up ) {
|
|||
Fatal("Can't load ZoneMinder::Control::$protocol\n$Module::Load::Conditional::ERROR");
|
||||
}
|
||||
|
||||
if ( my $cpid = fork() ) {
|
||||
logReinit();
|
||||
|
||||
# Parent process just sleep and fall through
|
||||
socket(CLIENT, PF_UNIX, SOCK_STREAM, 0)
|
||||
or die("Can't open socket: $!");
|
||||
my $attempts = 0;
|
||||
while ( !connect(CLIENT, $saddr) ) {
|
||||
$attempts++;
|
||||
Fatal("Can't connect: $! after $attempts attempts to $sock_file") if $attempts > MAX_CONNECT_DELAY;
|
||||
sleep(1);
|
||||
}
|
||||
} elsif ( defined($cpid) ) {
|
||||
close(STDOUT);
|
||||
close(STDERR);
|
||||
|
||||
setpgrp();
|
||||
|
||||
logReinit();
|
||||
|
||||
Info("Control server $id/$protocol starting at "
|
||||
.strftime('%y/%m/%d %H:%M:%S', localtime())
|
||||
);
|
||||
|
||||
$0 = $0." --id $id";
|
||||
$0 = $0." --id=$id";
|
||||
|
||||
my $control = "ZoneMinder::Control::$protocol"->new($id);
|
||||
my $control_key = $control->getKey();
|
||||
|
@ -145,8 +149,13 @@ if ( !$server_up ) {
|
|||
|
||||
$control->open();
|
||||
|
||||
socket(SERVER, PF_UNIX, SOCK_STREAM, 0)
|
||||
or Fatal("Can't open socket: $!");
|
||||
# If we have a command when starting up, then do it.
|
||||
if ( $options{command} ) {
|
||||
my $command = $options{command};
|
||||
$control->$command(\%options);
|
||||
}
|
||||
|
||||
socket(SERVER, PF_UNIX, SOCK_STREAM, 0) or Fatal("Can't open socket: $!");
|
||||
unlink($sock_file);
|
||||
bind(SERVER, $saddr) or Fatal("Can't bind: $!");
|
||||
listen(SERVER, SOMAXCONN) or Fatal("Can't listen: $!");
|
||||
|
@ -192,20 +201,8 @@ if ( !$server_up ) {
|
|||
unlink($sock_file);
|
||||
$control->close();
|
||||
exit(0);
|
||||
} else {
|
||||
Fatal("Can't fork: $!");
|
||||
}
|
||||
} # end if !server up
|
||||
|
||||
# The server is there, connect to it
|
||||
#print( "Writing commands\n" );
|
||||
CLIENT->autoflush();
|
||||
|
||||
if ( $options{command} ) {
|
||||
my $message = jsonEncode(\%options);
|
||||
print(CLIENT $message);
|
||||
}
|
||||
shutdown(CLIENT, 1);
|
||||
|
||||
exit(0);
|
||||
|
||||
|
|
Loading…
Reference in New Issue