File Coverage

blib/lib/WorkerManager/Client/TheSchwartz.pm
Criterion Covered Total %
statement 18 48 37.5
branch 0 10 0.0
condition 0 18 0.0
subroutine 6 8 75.0
pod 0 2 0.0
total 24 86 27.9


line stmt bran cond sub pod time code
1             package WorkerManager::Client::TheSchwartz;
2 1     1   1185 use strict;
  1         2  
  1         31  
3 1     1   5 use warnings;
  1         2  
  1         27  
4              
5 1     1   4 use DBI;
  1         2  
  1         33  
6 1     1   419 use TheSchwartz::Simple;
  1         2233  
  1         28  
7 1     1   7 use Module::Load ();
  1         1  
  1         15  
8 1     1   5 use Carp;
  1         2  
  1         368  
9              
10             sub new {
11 0     0 0   my ($class, $args) = @_;
12             # Old version had typo...
13 0   0       my $dsn = $args->{dsn} || $args->{dns} || croak 'not specified dsn for worker manager';
14 0   0       my $user = $args->{user} || 'nobody';
15 0   0       my $pass = $args->{pass} || 'nobody';
16 0   0       my $opts = $args->{opts} || {};
17              
18 0           my $client;
19 0 0         if ($ENV{DISABLE_WORKER}) {
20 0           Module::Load::load('TheSchwartz');
21 0           Module::Load::load('TheSchwartz::Job');
22             } else {
23 0           my $dbh = DBI->connect($dsn, $user, $pass, {RaiseError => 1, %$opts});
24 0           $client = TheSchwartz::Simple->new([$dbh]);
25             }
26 0           bless { client => $client }, $class;
27             }
28              
29             sub insert {
30 0     0 0   my $self = shift;
31 0           my $funcname = shift;
32 0           my $arg = shift;
33 0           my $options = shift;
34              
35 0 0         my $job = $ENV{DISABLE_WORKER} ? TheSchwartz::Job->new : TheSchwartz::Simple::Job->new;
36 0           $job->funcname($funcname);
37 0           $job->arg($arg);
38 0   0       $job->run_after($options->{run_after} || time);
39 0   0       $job->grabbed_until($options->{grabbed_until} || 0);
40 0   0       $job->uniqkey($options->{uniqkey} || undef);
41 0 0 0       $job->priority($options->{priority} || undef) if($job->can('priority'));
42              
43 0 0         if ($ENV{DISABLE_WORKER}) {
44 0           eval {
45 0           Module::Load::load($funcname);
46 0           $funcname->work($job);
47 0 0         warn $@ if $@;
48 0           return !$@;
49             }
50             } else {
51 0           return $self->{client}->insert($job)
52             }
53             }
54              
55             1;