Created
December 5, 2014 10:13
-
-
Save ywatase/0493fbae41295dd51329 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Resque; | |
use String::CamelCase qw(decamelize); | |
use Proclet; | |
my $cmd = shift @ARGV; | |
my $job = shift @ARGV; | |
my $worker = shift @ARGV || 1; | |
my $pidfile = sprintf "%s.pid", decamelize $job; | |
start() if $cmd eq 'start'; | |
stop() if $cmd eq 'stop'; | |
stop_force () if $cmd eq 'stop_force'; | |
status() if $cmd eq 'status'; | |
sub get_pid { | |
my $pid = `cat $pidfile`; | |
chomp $pid; | |
return $pid; | |
} | |
sub get_children { | |
my $pid = get_pid(); | |
return split /\s+/, `pgrep -P $pid perl`; | |
} | |
sub stop { | |
kill 'QUIT', get_children(); | |
} | |
sub stop_force { | |
kill 'TERM', get_children(); | |
} | |
$SIG{INT} = sub {stop_force()}; | |
$SIG{TERM} = sub {stop_force()}; | |
$SIG{QUIT} = sub {stop()}; | |
sub start { | |
my $pid = fork(); | |
if (not defined $pid) { | |
die "Can't fork" | |
} | |
if($pid) { | |
system sprintf "echo $pid > %s.pid", decamelize $job; | |
return; | |
} | |
my %children; | |
for (my $iter = 0 ; $iter < $worker ; $iter++) { | |
my $child = fork(); | |
if (not defined $child) { | |
die "Can't fork" | |
} | |
if($child){ | |
printf "pid($child) start\n"; | |
$children{$child} = $child; | |
next; | |
} | |
exec "./resque_runner.pl $job"; | |
exit; | |
} | |
while (keys %children) { | |
my $fin = wait; | |
delete $children{$fin}; | |
printf "pid($pid) stop\n"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Resque; | |
my $job = shift @ARGV; | |
my $w = Resque->new( redis => '127.0.0.1:6379' )->worker; | |
$w->add_queue($job); | |
$w->work; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment