-
Notifications
You must be signed in to change notification settings - Fork 22
/
cf_devstarter
executable file
·152 lines (112 loc) · 3.49 KB
/
cf_devstarter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/lib";
use lib "$FindBin::Bin/extlib/lib/perl5";
use local::lib "$FindBin::Bin/extlib";
use lib "$FindBin::Bin/site-lib";
use Carp;
use Proc::Daemon;
use Getopt::Long;
use POSIX ":sys_wait_h";
sub usage {
print <<"EOT";
Usage: $0 [options] server-prog server-arg1 server-arg2 ...
Example:
$0 -- ./cloudforecast_radar -r -c cloudforecast_dev.yaml -l server_list_dev.yaml
$0 --daemon -- /path/to/cloudforecast_web -r -p 5001 -c /path/to/cloudforecast_dev.yaml -l /path/to/server_list_dev.yaml
Options:
--daemon run server-prog as daemon process
--interval=seconds minimum interval to respawn the server process
(default: 1)
EOT
exit 0;
}
my ($opt_daemon, $opt_interval, $opt_help, $opt_version);
GetOptions(
'daemon' => \$opt_daemon,
'interval=i' => \$opt_interval,
help => \$opt_help,
) or exit 1;
usage()
if $opt_help;
# validate options
die "server program not specified\n"
unless @ARGV;
start_server(
exec => \@ARGV,
daemon => $opt_daemon,
interval => $opt_interval,
);
sub start_server {
my $opts = @_ == 1 ? shift : { @_ };
$opts->{interval} ||= 1;
croak "mandatory option ``exec'' is missing or is not an arrayref\n"
unless $opts->{exec} && ref $opts->{exec} eq 'ARRAY';
Proc::Daemon::Init if $opts->{daemon};
my @signals_received;
$SIG{$_} = sub {
push @signals_received, $_[0];
} for (qw/INT TERM HUP/);
$SIG{PIPE} = 'IGNORE';
my $pid;
my $initial=1;
while (1) {
if ( $pid ) {
my $kid = waitpid( $pid, 0 );
if ( $kid == -1 ) {
print STDERR "no server process\n";
$pid = undef;
}
elsif ( $kid ) {
my $status = $? >> 8;
print STDERR "server $pid died with status:$status\n";
$pid = undef;
}
}
if ( grep { $_ ne 'HUP' } @signals_received ) {
print STDERR "signals_received: " . join(",", @signals_received) . "\n";
last;
}
while ( my $signals_received = shift @signals_received ) {
if ( $pid && $signals_received eq 'HUP' ) {
print STDERR "HUP signal received, send TERM server $pid\n";
kill 'TERM', $pid;
waitpid( $pid, 0 );
$pid = undef;
}
}
select( undef, undef, undef, $opts->{interval} ) if ! $initial;
$initial=0;
if ( ! defined $pid ) {
$pid = fork();
die "failed fork: $!" unless defined $pid;
next if $pid; #main process
# child process
print STDERR "start server $opts->{exec}->[0]\n";
{ exec(@{$opts->{exec}}) };
print STDERR "failed to exec $opts->{exec}->[0]:$!\n";
exit(255);
}
}
if ( $pid ) {
kill 'TERM', $pid;
waitpid( $pid, 0 );
}
}
__END__
=head1 NAME
cf_devstarter - a superdaemon for cloudforecast servers and workers
=head1 SYNOPSIS
cf_devstarter [options] server-prog server-arg1 server-arg2 ...
=head1 DESCRIPTION
Use --help to find out how to use the script.
=head1 COPYRIGHT
Copyright 2010 Mixi, Inc.
=head1 AUTHOR
Masahiro Nagano E<lt>kazeburo {at} gmail.comE<gt>
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut