Qless/Worker.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 147 CC 12 SCAR 31.4
CC
12
Cov
0%
new
42.0
run
42.0
debug
6.0
CRAP 23.2
1 package Qless::Worker;
2 =head1 NAME
3  
4 Qless::Worker
5  
6 =cut
7  
8 use strict; use warnings;
9 use Redis;
10 use Qless::Client;
11 use POSIX qw(setsid :sys_wait_h);
12  
13 sub new {
14 my $class = shift;
15  
16 my %opt = @_;
17  
18 $class = ref $class if ref $class;
19 my $self = bless {}, $class;
20  
21 $self->{'debug'} = $opt{'debug'} || 0;
22  
23  
24 #Redis server
25 $opt{'host'} ||= '127.0.0.1:6379' if !$opt{'socket'};
26 $self->debug('Connecting to Redis server at '.($opt{'socket'} || $opt{'host'}));
27 my %redis_params = (encoding => undef);
28 $redis_params{'server'} = $opt{'host'} if $opt{'host'};
29 $redis_params{'sock'} = $opt{'socket'} if $opt{'socket'};
30 $self->{'redis'} = Redis->new(%redis_params);
31  
32  
33 # Qless client and queues
34 $self->{'client'} = Qless::Client->new($self->{'redis'});
35 $self->{'client'}->worker_name($opt{'name'}) if $opt{'name'};
36 my @queues = map { $self->{'client'}->queues($_) } @{ $opt{'queue'} };
37 $self->debug('Listening for jobs in queues: '.join(', ', map { $_->name } @queues));
38 $self->{'queues'} = \@queues;
39  
40 $self->{'interval'} = $opt{'interval'};
41 $self->{'workers_max'} = $opt{'workers'} || 5;
42 $self->{'workers'} = {};
43  
44 $self;
45 }
46  
47 sub redis { $_[0]->{'redis'} }
48 sub client { $_[0]->{'client'} }
49  
50 #TBD
51 sub resume {
52 my $self = shift;
53 my $jids = $self->client->workers( $self->client->worker_name )->{'jobs'};
54 }
55  
56 sub _reaper {
57 my $self = shift;
58 while (( my $pid = waitpid(-1, WNOHANG)) > 0) {
59 $self->debug("reaping worker PID $pid\n");
60 delete $self->{'workers'}->{ $pid };
61 }
62 $SIG{'CHLD'} = sub { $self->_reaper };
63 }
64  
65 sub run {
66 my $self = shift;
67  
68 $SIG{'CHLD'} = sub { $self->_reaper };
69  
70 my $working = 1;
71 my $queue_index = 0;
72 my $queue_count = scalar @{ $self->{'queues'} };
73 $self->debug('Starting loop');
74 while ($working) {
75  
76 if (scalar keys %{ $self->{'workers'} } >= $self->{'workers_max'}) {
77 sleep(1);
78 next;
79 }
80  
81 my $queue = $self->{'queues'}->[$queue_index];
82 $self->debug('Queuing '.$queue->name);
83 if (my $job = $queue->pop) {
84  
85 $self->debug('Got job class '.$job->klass);
86 my $pid = fork();
87 if (!defined $pid) {
88 die "Could not fork: $!";
89 }
90  
91 if (!$pid) {
92 $job->process;
93 exit;
94 }
95  
96 $self->debug('Starting worker, PID: '.$pid);
97 $self->{'workers'}->{ $pid } = 1;
98 }
99 else {
100 $self->debug('Sleeping');
101 sleep($self->{'interval'} || 1+int(rand(10)));
102 }
103  
104 $queue_index++;
105 if ($queue_index >= $queue_count) {
106 $queue_index = 0;
107 }
108 }
109  
110 while(scalar keys %{ $self->{'workers'} }) {
111 $self->debug('waiting for worker PIDS: '.join(', ', keys %{ $self->{'workers'} }));
112 sleep(1);
113 }
114  
115 $self->debug('done');
116 }
117  
118  
119 sub debug {
120 return if !$_[0]->{'debug'};
121 print $_[1],"\n";
122 }
123  
124 1;