| 1 |
package Analizo::Batch::Runner; |
| 2 |
|
| 3 |
use parent qw(Class::Accessor::Fast); |
| 4 |
|
| 5 |
sub new { |
| 6 |
my ($class, @args) = @_; |
| 7 |
return bless { @args }, $class; |
| 8 |
} |
| 9 |
|
| 10 |
sub run($$$) { |
| 11 |
my ($self, $batch, $output) = @_; |
| 12 |
$output->initialize(); |
| 13 |
$self->actually_run($batch, $output); |
| 14 |
$output->flush(); |
| 15 |
} |
| 16 |
|
| 17 |
# must be implemented by subclasses. Will receive as argument: |
| 18 |
# |
| 19 |
# * the batch to be run |
| 20 |
# * the output object |
| 21 |
# |
| 22 |
# This method must iterate over the jobs in some way, depending on the strategy |
| 23 |
# employed by the subclass, call I<execute> on each job, and feed the job |
| 24 |
# object to the $output object by calling I<$output->push($job)>. In the end, |
| 25 |
# the runner must call flush() on the output object. |
| 26 |
sub actually_run { |
| 27 |
} |
| 28 |
|
| 29 |
__PACKAGE__->mk_accessors(qw( progress )); |
| 30 |
|
| 31 |
sub report_progress { |
| 32 |
my ($self, $job, $i, $n) = @_; |
| 33 |
my $progress = $self->progress; |
| 34 |
if ($progress) { |
| 35 |
&$progress($job, $i, $n); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
1; |