RWDE/Gearman/Client.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 115 CC 6 SCAR 23.2
CC
6
Cov
0%
do_task
30.0
get_gear
6.0
new
2.0
CRAP 10.2
1 ## @file
2 # (Enter your file info here)
3 #
4 # $Id: Client.pm 462 2008-05-09 15:34:28Z damjan $
5  
6 ## @class RWDE::Gearman::Client
7 # (Enter RWDE::Gearman::Client info here)
8 package RWDE::Gearman::Client;
9  
10 use strict;
11  
12 use Error qw(:try);
13 use Gearman::Client;
14 use Storable qw( thaw nfreeze );
15  
16 use RWDE::Configuration;
17 use RWDE::Exceptions;
18  
19 use vars qw($VERSION);
20 $VERSION = sprintf "%d", q$Revision: 560 $ =~ /(\d+)/;
21  
22 ## @cmethod object new()
23 # Construct a new Gearman clients
24 # @return initialized object
25 sub new {
26 my ($proto, $params) = @_;
27  
28 my $class = ref($proto) || $proto;
29  
30 my $gear = Gearman::Client->new();
31  
32 my $self = { gear => $gear };
33  
34 bless $self, $class;
35  
36 $gear->job_servers(@{ RWDE::Configuration->GearHost });
37  
38 $self->initialize();
39  
40 return $self;
41 }
42  
43 ## @method object get_gear()
44 # (Enter get_gear info here)
45 # @return (Enter explanation for return value----here)
46 sub get_gear {
47 my ($self, $params) = @_;
48  
49 throw RWDE::DevelException({ info => 'No worker present' })
50 unless defined $self->{gear};
51  
52 return $self->{gear};
53 }
54  
55 ## @method void initialize()
56 # Use this method to define initial behavior of the class. It should be overriden in subclasses
57 sub initialize {
58 my ($self, $params) = @_;
59  
60 return;
61 }
62  
63 ## @cmethod object Do_task()
64 # Call the method do_task on the instance
65 # @return The value/reference produced by the RPC
66 sub Do_task {
67 my ($self, $params) = @_;
68  
69 my $client = $self->new;
70  
71 return $client->do_task($params);
72 }
73  
74 ## @method object do_task($method)
75 # (Enter do_task info here)
76 # @param method (Enter explanation for param here)
77 # @return The value/reference produced by the RPC
78 sub do_task {
79 my ($self, $params) = @_;
80  
81 my $client = $self->get_gear();
82  
83 my $result_ref;
84  
85 try {
86 local $SIG{ALRM} = sub { $self->alarm() };
87  
88 #waiting for longer than 5 seconds raises an exception
89 alarm 5;
90  
91 #issue job
92 $result_ref = $client->do_task($$params{method}, nfreeze($params));
93 alarm 0;
94  
95 throw RWDE::DevelException({ info => 'Undefined error trying to execute ' })
96 unless defined $result_ref;
97 }
98  
99 catch Error with {
100 my $ex = shift;
101  
102 if ($ex =~ m/Can't call method "syswrite" on an undefined value/) {
103 throw RWDE::DevelException({ info => "Couldn't connect to Gearman server/worker pair\n" });
104 }
105 else {
106 $ex->throw();
107 }
108 };
109  
110 my $result = thaw $$result_ref;
111 $result = $$result;
112  
113 #temporarily support multi-element returns as the default case
114 #special case the single element returns
115 if ((ref $result eq 'ARRAY') && scalar @{$result} == 1) {
116 $result = @{$result}[0];
117 }
118  
119 #check for error
120 if ((ref $result) =~ m/Exception/i || (ref $result) =~ m/Error::Simple/i) {
121  
122 #result is an exception :/
123 $result->throw();
124 }
125  
126 return $result;
127 }
128  
129 ## @method void alarm()
130 # Handle the timeout fromt the server by throwing an exception (devel)
131 sub alarm {
132 my ($self, $params) = @_;
133  
134 throw RWDE::DevelException({ info => 'Haven\'t received response within allotted time' });
135  
136 return ();
137 }
138  
139 1;