Permalink
Browse files

startup script

  • Loading branch information...
kazeburo committed Nov 21, 2011
1 parent 981f63a commit 6971c23fa4e8e73c008a4bb2e4e9f7e4af58de2c
Showing with 91 additions and 1 deletion.
  1. +4 −1 Makefile.PL
  2. 0 { → eg}/app.psgi
  3. 0 { → eg}/worker.pl
  4. +87 −0 growthforecast.pl
View
@@ -10,7 +10,10 @@ WriteMakefile(
'HTTP::Date' => 0,
'File::Zglob' => '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',
);
View
File renamed without changes.
File renamed without changes.
View
@@ -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.