Skip to content

Commit

Permalink
startup script
Browse files Browse the repository at this point in the history
  • Loading branch information
kazeburo committed Nov 21, 2011
1 parent 981f63a commit 6971c23
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Makefile.PL
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ WriteMakefile(
'HTTP::Date' => 0, 'HTTP::Date' => 0,
'File::Zglob' => '0.09', 'File::Zglob' => '0.09',
'Log::Minimal' => '0.09', 'Log::Minimal' => '0.09',
'Starlet' => '0.14',
'Parallel::Scoreboard' => '0.03',
'Plack::Builder::Conditionals' => '0.03',
}, },
MIN_PERL_VERSION => '5.010000', MIN_PERL_VERSION => '5.008001',
); );


File renamed without changes.
File renamed without changes.
87 changes: 87 additions & 0 deletions growthforecast.pl
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/perl

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/extlib/lib/perl5";
use lib "$FindBin::Bin/lib";
use File::Basename;
use Getopt::Long;
use File::Temp qw/tempdir/;
use Parallel::Prefork;
use Parallel::Scoreboard;
use Plack::Loader;
use Plack::Builder;
use Plack::Builder::Conditionals;
use GrowthForecast::Web;
use GrowthForecast::Worker;

my $port = 5125;
my $host = 0;
my @front_proxy;
Getopt::Long::Configure ("no_ignore_case");
GetOptions(
'port=s' => \$port,
'host=s' => \$host,
'front-proxy=s' => \@front_proxy,
"h|help" => \my $help,
);

if ( $help ) {
print "usage: $0 --port 5005 --host 127.0.0.1 --front-proxy 127.0.0.1\n";
exit(1);
}

my $root_dir = File::Basename::dirname(__FILE__);
my $sc_board_dir = tempdir( CLEANUP => 1 );
my $scoreboard = Parallel::Scoreboard->new( base_dir => $sc_board_dir );

my $pm = Parallel::Prefork->new({
max_workers => 2,
spawn_interval => 1,
trap_signals => {
map { ($_ => 'TERM') } qw(TERM HUP)
}
});

while ($pm->signal_received ne 'TERM' ) {
$pm->start(sub{
my $stats = $scoreboard->read_all;
my %running;
for my $pid ( keys %{$stats} ) {
my $val = $stats->{$pid};
$running{$val}++;
}
if ( $running{worker} ) {
local $0 = "$0 (GrowthForecast::Web)";
$scoreboard->update('web');
my $app = GrowthForecast::Web->psgi($root_dir);
$app = builder {
enable 'Lint';
enable 'StackTrace';
if ( @front_proxy ) {
enable match_if addr(\@front_proxy), 'ReverseProxy';
}
enable 'Static',
path => qr!^/(?:(?:css|js|images)/|favicon\.ico$)!,
root => $root_dir . '/public';
$app;
};
my $loader = Plack::Loader->load(
'Starlet',
port => $port,
host => $host || 0,
max_workers => 4,
);
$loader->run($app);
}
else {
local $0 = "$0 (GrowthForecast::Worker)";
$scoreboard->update('worker');
my $worker = GrowthForecast::Worker->new($root_dir);
$worker->run;
}
});
}


0 comments on commit 6971c23

Please sign in to comment.