Engine/Threads.pm untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 2 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 167 CC 7 SCAR 17.0
CC
7
Cov
0%
new
12.0
add_thread
12.0
queue
6.0
CRAP 5.5
1 package Service::Engine::Threads;
2 use strict;
3  
4 use 5.010;
5 use strict;
6 use warnings;
7 use Carp;
8 use Service::Engine;
9 use Data::Dumper;
10  
11 use threads;
12 use threads::shared;
13 use Thread::Queue;
14  
15 our $Config;
16 our $Log;
17 our $EngineName;
18 our $EngineConfig;
19 our $Modules;
20 our $ModuleMethods;
21 our $ThreadCount = 0;
22 our $ThreadQueue= [];
23 our $Throughput;
24 our $Alert;
25 share($Throughput);
26 share($ThreadCount);
27 share($ThreadQueue);
28  
29 sub new {
30  
31 my ($class,$options) = @_;
32
33 # set some defaults
34 my $attributes = {'threads'=>[]};
35
36 # load options
37 if (ref($options) eq 'HASH') {
38 foreach my $option (keys %{$options}) {
39 $attributes->{$option} = $options->{$option};
40 }
41 }
42
43 # pull in some Service::Engine globals
44 $Config = $Service::Engine::Config;
45 $Log = $Service::Engine::Log;
46 $EngineName = $Service::Engine::EngineName;
47 $EngineConfig = $Service::Engine::EngineConfig;
48 $Modules = $Service::Engine::Modules;
49 $ModuleMethods = $Service::Engine::ModuleMethods;
50 $Throughput = $Service::Engine::Throughput;
51 $Alert = $Service::Engine::Alert;
52
53 my $ThreadQueue_to_start = $Config->get_config('engine')->{'threads'};
54 $ThreadQueue_to_start ||= 5;
55
56 $Log->log({msg=>"loading $ThreadQueue_to_start threads",level=>2});
57  
58 my @carriers :shared = ();
59 while (scalar(@carriers) < $ThreadQueue_to_start) {
60 my $queue :shared = Thread::Queue->new();
61 my $thread = threads->new( 'queue', $queue);
62 my $thread_id :shared = $thread->tid;
63 my %carrier_obj :shared = ('queue'=>$queue,'thread_id'=>$thread_id);
64 push @carriers, \%carrier_obj;
65 }
66
67 $ThreadCount = scalar(@carriers);
68 $Log->log({msg=>"LOADED $ThreadCount threads",level=>3});
69  
70 if ( @carriers < 1 ) {
71 croak('failed to load threads');
72 }
73
74 $ThreadQueue= \@carriers;
75  
76 my $self = bless $attributes, $class;
77
78 return $self;
79
80 }
81  
82 sub queue {
83 my ($thread) = @_;
84 my $method = $ModuleMethods->{'worker'}->{'method'};
85 # Each thread needs it own $Data
86 my $data = Service::Engine::Data->new();
87 $Log->log({msg=>"setting up data",level=>2});
88 while ( (my $item = $thread->dequeue) ) {
89 my $queue_item = {'data'=>$data, 'item'=>$item, 'id'=> threads->tid()};
90 $Modules->{'worker'}->$method($queue_item);
91 if ($Throughput) {
92 $Throughput->finish();
93 }
94 }
95 }
96  
97 sub add_thread {
98 my ($self, $fh, $queue_id) = @_;
99
100 my $queue :shared = Thread::Queue->new();
101 my $thread = threads->new( 'queue', $queue);
102 my $thread_id :shared = $thread->tid;
103 my %carrier_obj :shared = ('queue'=>$queue,'thread_id'=>$thread_id);
104 if ($queue_id) {
105 $ThreadQueue->[$queue_id] = \%carrier_obj;
106 } else {
107 push @{$ThreadQueue}, \%carrier_obj;
108 }
109
110 $ThreadCount += 1;
111 $Log->log({msg=>"adding 1 thread",level=>3});
112 if ($fh) {
113 warn(Dumper($fh));
114 $fh->write("Added 1 Thread");
115 }
116 return '';
117 }
118  
119 sub get_thread {
120 my ($self) = @_;
121 # Get a random number between 0 and $self->{thread_count}
122 my $rand = int(rand(scalar(@{$ThreadQueue})));
123 warn(Dumper($ThreadQueue->[$rand]));
124 my $carrier = $ThreadQueue->[$rand];
125 my $queue = $carrier->{'queue'};
126 my $thread_id = $carrier->{'thread_id'};
127 my $thr = threads->object($thread_id);
128 if (my $err = $thr->error()) {
129 $Log->log({msg=>"Thread $thread_id queue:$rand error: $err\n",level=>0});
130 # need to support alerting here
131 # $Alert->send({'msg'=>"Thread $thread_id queue:$rand error: $err",'module'=>'Twilio', 'handle'=>'sms', 'options'=>$handle_config});
132 # create a new thread in this slot
133 $self->add_thread(undef,$rand);
134 return $ThreadQueue->[$rand]->{'queue'};
135 }
136
137 return $queue;
138 }
139  
140 sub get_thread_count {
141 my ($self) = @_;
142 return scalar(@{$ThreadQueue});
143 }
144  
145 sub get_queue_count {
146 my ($self) = @_;
147 my $count = 0;
148 foreach my $carrier (@{$ThreadQueue}) {
149 my $queue = $carrier->{'queue'};
150 $count += $queue->pending();
151 }
152 return $count;
153 }
154  
155 1;