Created
February 17, 2010 21:41
-
-
Save jhannah/307036 to your computer and use it in GitHub Desktop.
MooseX::Workers in action
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
Here's my most recent stack... | |
View/System/ows/cache/runner.pl | |
View/Runner.pm | |
MooseX::Workers | |
Moose, POE | |
========================================================== | |
View/System/ows/cache/runner.pl | |
========================================================== | |
#!/usr/bin/perl | |
package MyRunner; | |
use Getopt::Long; | |
use Omni2::Base; | |
use Omni2::Model::DB2; | |
use Omni2::Control::DateTime; | |
use Moose; | |
extends "Omni2::View::Runner"; | |
Log::Log4perl::init($Omni2::Base::home . '/Omni2/log4perl.datamining.cfg'); | |
my $logger = Log::Log4perl->get_logger('Omni2.View.System.ows.cache'); | |
my $dbh = Omni2::Model::DB2::connect(system => 'dal'); | |
my ($start_offset, $end_offset, $max_workers, $ttl); | |
GetOptions( | |
"start_offset=i" => \$start_offset, | |
"end_offset=i" => \$end_offset, | |
"max_workers=i" => \$max_workers, | |
"ttl=i" => \$ttl, | |
); | |
usage() unless (defined $start_offset && defined $end_offset && defined $max_workers && defined $ttl); | |
sub run { | |
my ($self) = @_; | |
my $today = Omni2::Control::DateTime->new('today'); | |
my $seek = $today + "${start_offset}D"; | |
my $end = $today + "${end_offset}D"; | |
while ($seek <= $end) { | |
my $date = $seek->format('%Y-%m-%d'); | |
my $cmd = $Omni2::Base::home . "/Omni2/View/System/ows/cache/FetchCalendar.pl --run_date=$date --ttl=$ttl"; | |
my $job = MooseX::Workers::Job->new( | |
name => $date, | |
command => sub { system($cmd) }, | |
timeout => 60 * 60, # 1 hour | |
); | |
$logger->debug("queuing '$cmd'"); | |
$_[0]->enqueue($job); | |
$seek = $seek + '1D'; | |
} | |
POE::Kernel->run(); | |
print "POE::Kernel->run() exiting\n"; | |
} | |
my $MyRunner = MyRunner->new( | |
logger => $logger, | |
); | |
$MyRunner->max_workers($max_workers); | |
#while (1) { | |
$MyRunner->run; | |
#} | |
sub usage { | |
print <<EOT; | |
$0 --start_offset=0 --end_offset=7 --max_workers=5 --ttl=6 | |
Launch up to 5 children at a time, refreshing all hotels for all dates between today and | |
one week from now where the cache is more than 6 hours old. | |
Once all of that is complete the program exits. | |
EOT | |
exit; | |
} | |
========================================================== | |
View/Runner.pm: | |
========================================================== | |
package Omni2::View::Runner; | |
use strict; | |
use Omni2::Control::Hotels; | |
use Log::Log4perl; | |
use Moose; | |
with 'MooseX::Workers'; | |
has target => (is => 'rw', isa => 'Str' ); | |
has Hotels => (is => 'rw', isa => 'Object', default => sub { Omni2::Control::Hotels->new() }); | |
has logger => (is => 'rw', isa => 'Object' ); | |
no Moose; | |
sub worker_manager_start { | |
my ($self) = @_; | |
$self->logger->info("LAUNCH!"); | |
} | |
sub worker_manager_stop { | |
my ($self) = @_; | |
$self->logger->info("EXIT!"); | |
} | |
sub worker_stdout { | |
my ($self, $output, $job) = @_; | |
$self->logger->info(sprintf("worker_stdout %s(%s,%s): %s", $job->name, $job->ID, $job->PID, $output)); | |
} | |
sub worker_stderr { | |
my ($self, $output, $job) = @_; | |
$self->logger->info(sprintf("worker_stderr %s(%s,%s): %s", $job->name, $job->ID, $job->PID, $output)); | |
} | |
sub worker_error { | |
my ($self) = @_; | |
$self->logger->error("worker_error: @_\n"); | |
} | |
sub worker_done { | |
my ($self, $job) = @_; | |
$self->logger->info(sprintf("worker_done %s(%s,%s)", $job->name, $job->ID, $job->PID)); | |
} | |
sub worker_started { | |
my ($self, $job) = @_; | |
$self->logger->info(sprintf("worker_started %s(%s,%s)", $job->name, $job->ID, $job->PID)); | |
} | |
sub run { | |
my ($self) = @_; | |
Log::Log4perl::init($Omni2::Base::home . '/Omni2/log4perl.datamining.cfg'); | |
$self->logger(Log::Log4perl->get_logger('Omni2.View.Runner')) | |
unless $self->logger; | |
unless ($self->target) { | |
$self->logger->fatal("FATAL: target() must be set"); | |
exit; | |
} | |
foreach my $hotelcode (sort $self->Hotels->epitome_pms_list) { | |
my $cmd = $self->target . " --prop $hotelcode"; | |
my $job = MooseX::Workers::Job->new( | |
name => $hotelcode, | |
command => sub { system($cmd) }, | |
timeout => 60 * 60, # 1 hour | |
); | |
$self->logger->debug("queuing '$cmd'"); | |
$_[0]->enqueue($job); | |
} | |
POE::Kernel->run(); | |
print "POE::Kernel->run() exiting\n"; | |
} | |
'j + POE + Moose r0ck5'; | |
# Make log4perl.datamining.cfg happy... | |
package main; | |
sub get_filename { '/var/log/omni/datamining/blank.log' } | |
========================================================= | |
MooseX::Workers handles Moose and POE for me. :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment