Skip to content
This repository
Browse code

startup script

  • Loading branch information...
commit 6971c23fa4e8e73c008a4bb2e4e9f7e4af58de2c 1 parent 981f63a
Masahiro Nagano authored November 21, 2011
5  Makefile.PL
@@ -10,7 +10,10 @@ WriteMakefile(
10 10
         'HTTP::Date' => 0,
11 11
         'File::Zglob' => '0.09',
12 12
         'Log::Minimal' => '0.09',
  13
+        'Starlet' => '0.14',
  14
+        'Parallel::Scoreboard' => '0.03',
  15
+        'Plack::Builder::Conditionals' => '0.03',
13 16
     },
14  
-    MIN_PERL_VERSION => '5.010000',
  17
+    MIN_PERL_VERSION => '5.008001',
15 18
 );
16 19
 
0  app.psgi → eg/app.psgi
File renamed without changes
0  worker.pl → eg/worker.pl
File renamed without changes
87  growthforecast.pl
... ...
@@ -0,0 +1,87 @@
  1
+#!/usr/bin/perl
  2
+
  3
+use strict;
  4
+use warnings;
  5
+use FindBin;
  6
+use lib "$FindBin::Bin/extlib/lib/perl5";
  7
+use lib "$FindBin::Bin/lib";
  8
+use File::Basename;
  9
+use Getopt::Long;
  10
+use File::Temp qw/tempdir/;
  11
+use Parallel::Prefork;
  12
+use Parallel::Scoreboard;
  13
+use Plack::Loader;
  14
+use Plack::Builder;
  15
+use Plack::Builder::Conditionals;
  16
+use GrowthForecast::Web;
  17
+use GrowthForecast::Worker;
  18
+
  19
+my $port = 5125;
  20
+my $host = 0;
  21
+my @front_proxy;
  22
+Getopt::Long::Configure ("no_ignore_case");
  23
+GetOptions(
  24
+    'port=s' => \$port,
  25
+    'host=s' => \$host,
  26
+    'front-proxy=s' => \@front_proxy,
  27
+    "h|help" => \my $help,
  28
+);
  29
+
  30
+if ( $help ) {
  31
+    print "usage: $0 --port 5005 --host 127.0.0.1 --front-proxy 127.0.0.1\n";
  32
+    exit(1);
  33
+}
  34
+
  35
+my $root_dir = File::Basename::dirname(__FILE__);
  36
+my $sc_board_dir = tempdir( CLEANUP => 1 );
  37
+my $scoreboard = Parallel::Scoreboard->new( base_dir => $sc_board_dir );
  38
+
  39
+my $pm = Parallel::Prefork->new({
  40
+    max_workers => 2,
  41
+    spawn_interval  => 1,
  42
+    trap_signals    => {
  43
+        map { ($_ => 'TERM') } qw(TERM HUP)
  44
+    }
  45
+});
  46
+
  47
+while ($pm->signal_received ne 'TERM' ) {
  48
+    $pm->start(sub{
  49
+        my $stats = $scoreboard->read_all;
  50
+        my %running;
  51
+        for my $pid ( keys %{$stats} ) {
  52
+            my $val = $stats->{$pid};
  53
+            $running{$val}++;
  54
+        }
  55
+        if ( $running{worker} ) {
  56
+            local $0 = "$0 (GrowthForecast::Web)";
  57
+            $scoreboard->update('web');
  58
+            my $app = GrowthForecast::Web->psgi($root_dir);
  59
+            $app = builder {
  60
+                enable 'Lint';
  61
+                enable 'StackTrace';
  62
+                if ( @front_proxy ) {
  63
+                    enable match_if addr(\@front_proxy), 'ReverseProxy';
  64
+                }
  65
+                enable 'Static',
  66
+                    path => qr!^/(?:(?:css|js|images)/|favicon\.ico$)!,
  67
+                    root => $root_dir . '/public';
  68
+                $app;
  69
+            };
  70
+             my $loader = Plack::Loader->load(
  71
+                 'Starlet',
  72
+                 port => $port,
  73
+                 host => $host || 0,
  74
+                 max_workers => 4,
  75
+             );
  76
+             $loader->run($app);
  77
+        }
  78
+        else {
  79
+            local $0 = "$0 (GrowthForecast::Worker)";
  80
+            $scoreboard->update('worker');
  81
+            my $worker = GrowthForecast::Worker->new($root_dir);
  82
+            $worker->run;
  83
+        }
  84
+    });
  85
+}
  86
+
  87
+

0 notes on commit 6971c23

Please sign in to comment.
Something went wrong with that request. Please try again.