-
Notifications
You must be signed in to change notification settings - Fork 1
/
Worker.pm
67 lines (44 loc) · 1.06 KB
/
Worker.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package Reflexive::WorkerPool::Worker;
use Moose;
extends 'Reflex::Base';
use Reflex::Collection;
has_many jobs => (
handles => {
remove_job => 'forget',
}
);
has max_jobs => (
is => 'ro',
isa => 'Int',
default => 5,
);
sub num_jobs {
my $self = shift;
return scalar keys %{$self->jobs->_objects};
}
sub available_job_slots {
my $self = shift;
return $self->max_jobs - $self->num_jobs;
}
sub add_job {
my ( $self, $job ) = @_;
Carp::croak "Not a valid job"
unless $job->does('Reflex::Role::Collectible');
Carp::croak "Job doesn't implement a work() method"
unless $job->can('work');
Carp::croak "Too many jobs!"
if $self->num_jobs == $self->max_jobs;
$self->jobs->remember($job);
$job->run();
}
1;
__END__
=head1 NAME
Reflexive::Worker - Manages a collection of jobs
=head1 DESCRIPTION
See L<Reflexive::WorkerPool> for details.
=head1 AUTHOR
Andy Gorman, agorman@cpan.org
=head1 COPYRIGHT AND LICENSE
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.