File Coverage

blib/lib/Sque/Job.pm
Criterion Covered Total %
statement 15 29 51.7
branch 0 6 0.0
condition n/a
subroutine 5 9 55.5
pod 4 4 100.0
total 24 48 50.0


line stmt bran cond sub pod time code
1             package Sque::Job;
2             $Sque::Job::VERSION = '0.010';
3 3     3   19 use Any::Moose;
  3         5  
  3         26  
4 3     3   1885 use Any::Moose '::Util::TypeConstraints';
  3         5  
  3         13  
5 3     3   3822 use UNIVERSAL::require;
  3         5189  
  3         33  
6             with 'Sque::Encoder';
7              
8             # ABSTRACT: Sque job container
9              
10             has sque => (
11             is => 'rw',
12             handles => [qw/ stomp /],
13             default => sub { confess "This Sque::Job isn't associated to any Sque system yet!" }
14             );
15              
16             has worker => (
17             is => 'rw',
18             lazy => 1,
19             default => sub { $_[0]->sque->worker },
20             predicate => 'has_worker'
21             );
22              
23             has class => (
24             is => 'rw',
25             lazy => 1,
26             default => sub { confess "This job needs a class to do some work." },
27             );
28              
29             has queue => (
30             is => 'rw',
31             lazy => 1,
32             default => \&queue_from_class,
33             predicate => 'queued'
34             );
35              
36             has args => ( is => 'rw', isa => 'ArrayRef', default => sub {[]} );
37              
38             coerce 'HashRef'
39             => from 'Str'
40             => via { JSON->new->utf8->decode($_) };
41              
42             has payload => (
43             is => 'rw',
44             isa => 'HashRef',
45             coerce => 1,
46             lazy => 1,
47             default => sub {{
48             class => $_[0]->class,
49             args => $_[0]->args,
50             }},
51             trigger => sub {
52             my ( $self, $hr ) = @_;
53             $self->class( $hr->{class} );
54             $self->args( $hr->{args} ) if $hr->{args};
55             }
56             );
57              
58             has headers => (
59             is => 'rw',
60             isa => 'HashRef',
61             default => sub{ {} },
62             );
63              
64             has frame => (
65             is => 'ro',
66             lazy => 1,
67             default => sub { {} },
68             trigger => sub {
69             my ( $self, $frame ) = @_;
70             $self->payload( $frame->body );
71             }
72             );
73              
74             sub encode {
75 0     0 1   my $self = shift;
76 0           $self->encoder->encode( $self->payload );
77             }
78              
79             sub stringify {
80 0     0 1   my $self = shift;
81 0           sprintf( "(Job{%s} | %s | %s)",
82             $self->queue,
83             $self->class,
84             $self->encoder->encode( $self->args )
85             );
86             }
87             sub queue_from_class {
88 0     0 1   my $class = shift->class;
89 0           $class =~ s/://g;
90 0           $class;
91             }
92              
93             sub perform {
94 0     0 1   my $self = shift;
95 0 0         $self->class->require || confess $@;
96              
97             # First test if its OO
98 0 0         if($self->class->can('new')){
99 3     3   2437 no strict 'refs';
  3         7  
  3         763  
100 0           $self->class->new->perform( $self );
101             }else{
102             # If it's not OO, just call perform
103 0 0         $self->class->can('perform')
104             || confess $self->class . " doesn't know how to perform";
105              
106 3     3   16 no strict 'refs';
  3         7  
  3         265  
107 0           &{$self->class . '::perform'}($self);
  0            
108             }
109             }
110              
111             __PACKAGE__->meta->make_immutable();
112              
113             1;
114              
115             __END__
116              
117             =pod
118              
119             =encoding UTF-8
120              
121             =head1 NAME
122              
123             Sque::Job - Sque job container
124              
125             =head1 VERSION
126              
127             version 0.010
128              
129             =head1 ATTRIBUTES
130              
131             =head2 sque
132              
133             =head2 worker
134              
135             Worker running this job.
136             A new worker will be popped up from sque by default.
137              
138             =head2 class
139              
140             Class to be performed by this job.
141              
142             =head2 queue
143              
144             Name of the queue this job is or should be.
145              
146             =head2 args
147              
148             Array of arguments for this job.
149              
150             =head2 payload
151              
152             HashRef representation of the job.
153             When passed to constructor, this will restore the job from encoded state.
154             When passed as a string this will be coerced using JSON decoder.
155             This is read-only.
156              
157             =head2 frame
158              
159             Raw stomp frame representing the job.
160             This is read-only.
161              
162             =head1 METHODS
163              
164             =head2 encode
165              
166             String representation(JSON) to be used on the backend.
167              
168             =head2 stringify
169              
170             =head2 queue_from_class
171              
172             Normalize class name to be used as queue name.
173             NOTE: future versions will try to get the
174             queue name from the real class attr
175             or $class::queue global variable.
176              
177             =head2 perform
178              
179             Load job class and call perform() on it.
180             This job objet will be passed as the only argument.
181              
182             =head1 AUTHOR
183              
184             William Wolf <throughnothing@gmail.com>
185              
186             =head1 COPYRIGHT AND LICENSE
187              
188              
189             William Wolf has dedicated the work to the Commons by waiving all of his
190             or her rights to the work worldwide under copyright law and all related or
191             neighboring legal rights he or she had in the work, to the extent allowable by
192             law.
193              
194             Works under CC0 do not require attribution. When citing the work, you should
195             not imply endorsement by the author.
196              
197             =cut