blib/lib/Beekeeper/Worker.pm untested

filter: Statementstmt 0.0% 0 / 472 Branchbran 0.0% 0 / 130 Conditioncond 0.0% 0 / 105 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 47 Podpod 0.0% 0 / 32 totaltotal 0.0% 0 / 786 SCAR 83.9
CC
66
Cov
0%
__drain_task_queue
600.0
stop_accepting_calls
72.0
accept_remote_calls
42.0
CRAP 4422.0
1 package Beekeeper::Worker;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.09';
7  
8 use Beekeeper::Client ':worker';
9 use Beekeeper::Logger ':log_levels';
10 use Beekeeper::JSONRPC;
11  
12 use JSON::XS;
13 use Time::HiRes;
14 use Sys::Hostname;
15 use Digest::MD5 'md5_base64';
16 use Scalar::Util 'blessed';
17 use Carp;
18  
19 use constant COMPILE_ERROR_EXIT_CODE => 99;
20 use constant BKPR_REQUEST_AUTHORIZED => int(rand(90000000)+10000000);
21  
22 use Exporter 'import';
23  
24 our @EXPORT = qw( BKPR_REQUEST_AUTHORIZED );
25  
26 our @EXPORT_OK = qw(
27 log_fatal
28 log_alert
29 log_critical
30 log_error
31 log_warn
32 log_warning
33 log_notice
34 log_info
35 log_debug
36 log_trace
37 log_level
38 );
39  
40 our %EXPORT_TAGS = ('log' => [ @EXPORT_OK, @EXPORT ]);
41  
42 our $Logger = sub { warn(@_) }; # redefined later by __init_logger
43 our $LogLevel = LOG_INFO;
44  
45 sub log_fatal (@) { $LogLevel >= LOG_FATAL && $Logger->( LOG_FATAL, @_ ) }
46 sub log_alert (@) { $LogLevel >= LOG_ALERT && $Logger->( LOG_ALERT, @_ ) }
47 sub log_critical (@) { $LogLevel >= LOG_CRIT && $Logger->( LOG_CRIT, @_ ) }
48 sub log_error (@) { $LogLevel >= LOG_ERROR && $Logger->( LOG_ERROR, @_ ) }
49 sub log_warn (@) { $LogLevel >= LOG_WARN && $Logger->( LOG_WARN, @_ ) }
50 sub log_warning (@) { $LogLevel >= LOG_WARN && $Logger->( LOG_WARN, @_ ) }
51 sub log_notice (@) { $LogLevel >= LOG_NOTICE && $Logger->( LOG_NOTICE, @_ ) }
52 sub log_info (@) { $LogLevel >= LOG_INFO && $Logger->( LOG_INFO, @_ ) }
53 sub log_debug (@) { $LogLevel >= LOG_DEBUG && $Logger->( LOG_DEBUG, @_ ) }
54 sub log_trace (@) { $LogLevel >= LOG_TRACE && $Logger->( LOG_TRACE, @_ ) }
55 sub log_level (;$) { $LogLevel = shift if scalar @_; return $LogLevel }
56  
57 our $BUSY_SINCE; *BUSY_SINCE = \$Beekeeper::MQTT::BUSY_SINCE;
58 our $BUSY_TIME; *BUSY_TIME = \$Beekeeper::MQTT::BUSY_TIME;
59  
60 our $REPORT_STATUS_PERIOD = 5;
61 our $UNSUBSCRIBE_LINGER = 2;
62  
63 my %AUTH_TOKENS;
64 my $JSON;
65  
66  
67 sub new {
68 my ($class, %args) = @_;
69  
70 # Parameters passed by WorkerPool->spawn_worker
71
72 my $self = {
73 _WORKER => undef,
74 _CLIENT => undef,
75 _BUS => undef,
76 _LOGGER => undef,
77 };
78  
79 bless $self, $class;
80  
81 $self->{_WORKER} = {
82 parent_pid => $args{'parent_pid'},
83 foreground => $args{'foreground'}, # --foreground option
84 debug => $args{'debug'}, # --debug option
85 bus_config => $args{'bus_config'}, # content of bus.config.json
86 pool_config => $args{'pool_config'}, # content of pool.config.json
87 pool_id => $args{'pool_id'},
88 bus_id => $args{'bus_id'},
89 config => $args{'config'},
90 hostname => hostname(),
91 stop_cv => undef,
92 callbacks => {},
93 task_queue_high => [],
94 task_queue_low => [],
95 queued_tasks => 0,
96 in_progress => 0,
97 last_report => 0,
98 call_count => 0,
99 notif_count => 0,
100 error_count => 0,
101 busy_time => 0,
102 };
103  
104 $JSON = JSON::XS->new;
105 $JSON->utf8; # encode result as utf8
106 $JSON->allow_blessed; # encode blessed references as null
107 $JSON->convert_blessed; # use TO_JSON methods to serialize objects
108  
109 if (defined $SIG{TERM} && $SIG{TERM} eq 'DEFAULT') {
110 # Stop working gracefully when TERM signal is received
111 $SIG{TERM} = sub { $self->stop_working };
112 }
113  
114 if (defined $SIG{INT} && $SIG{INT} eq 'DEFAULT' && $args{'foreground'}) {
115 # In foreground mode also stop working gracefully when INT signal is received
116 $SIG{INT} = sub { $self->stop_working };
117 }
118  
119 eval {
120  
121 # Init logger as soon as possible
122 $self->__init_logger;
123  
124 # Connect to broker
125 $self->__init_client;
126  
127 # Pass broker connection to logger
128 $self->{_LOGGER}->{_BUS} = $self->{_BUS} if (exists $self->{_LOGGER}->{_BUS});
129  
130 $self->__init_auth_tokens;
131  
132 $self->__init_worker;
133 };
134  
135 if ($@) {
136 log_fatal "Worker died while initialization: $@";
137 log_fatal "$class could not be started";
138 CORE::exit( COMPILE_ERROR_EXIT_CODE );
139 }
140  
141 return $self;
142 }
143  
144 sub __init_auth_tokens {
145 my ($self) = @_;
146  
147 # Using a hashing function makes harder to access the wrong worker pool by mistake,
148 # but it is not an effective access restriction: anyone with access to the backend
149 # bus credentials can easily inspect and clone auth data tokens
150  
151 my $salt = $self->{_CLIENT}->{auth_salt};
152  
153 $AUTH_TOKENS{'BKPR_SYSTEM'} = md5_base64('BKPR_SYSTEM'. $salt);
154 $AUTH_TOKENS{'BKPR_ADMIN'} = md5_base64('BKPR_ADMIN' . $salt);
155 $AUTH_TOKENS{'BKPR_ROUTER'} = md5_base64('BKPR_ROUTER'. $salt);
156 }
157  
158 sub __has_authorization_token {
159 my ($self, $auth_level) = @_;
160  
161 my $auth_data = $self->{_CLIENT}->{auth_data};
162  
163 return 0 unless $auth_data && $auth_level;
164 return 0 unless exists $AUTH_TOKENS{$auth_level};
165 return 0 unless $AUTH_TOKENS{$auth_level} eq $auth_data;
166  
167 return 1;
168 }
169  
170 sub __init_logger {
171 my $self = shift;
172  
173 # Honor --debug command line option and 'debug' config option from pool.config.json
174 $LogLevel = LOG_DEBUG if $self->{_WORKER}->{debug} || $self->{_WORKER}->{config}->{debug};
175  
176 my $log_handler = $self->log_handler;
177 $self->{_LOGGER} = $log_handler;
178  
179 $Logger = sub {
180 # ($level, @messages) = @_
181 $log_handler->log(@_);
182 };
183  
184 $SIG{__WARN__} = sub { $Logger->( LOG_WARN, @_ ) };
185 }
186  
187 sub log_handler {
188 my $self = shift;
189  
190 Beekeeper::Logger->new(
191 worker_class => ref $self,
192 foreground => $self->{_WORKER}->{foreground},
193 log_file => $self->{_WORKER}->{config}->{log_file},
194 host => $self->{_WORKER}->{hostname},
195 pool => $self->{_WORKER}->{pool_id},
196 _BUS => $self->{_BUS},
197 @_
198 );
199 }
200  
201 sub __init_client {
202 my $self = shift;
203  
204 my $bus_id = $self->{_WORKER}->{bus_id};
205 my $config = $self->{_WORKER}->{bus_config}->{$bus_id};
206  
207 my $client = Beekeeper::Client->new(
208 %$config,
209 timeout => 0, # retry forever
210 on_error => sub {
211 my $errmsg = $_[0] || ""; $errmsg =~ s/\s+/ /sg;
212 log_fatal "Connection to $bus_id failed: $errmsg";
213 $self->stop_working;
214 },
215 );
216  
217 $self->{_CLIENT} = $client->{_CLIENT};
218 $self->{_BUS} = $client->{_BUS};
219  
220 $Beekeeper::Client::singleton = $self;
221 }
222  
223 sub __init_worker {
224 my $self = shift;
225  
226 $self->on_startup;
227  
228 $self->__report_status;
229  
230 AnyEvent->now_update;
231  
232 $self->{_WORKER}->{report_status_timer} = AnyEvent->timer(
233 after => rand( $REPORT_STATUS_PERIOD ),
234 interval => $REPORT_STATUS_PERIOD,
235 cb => sub { $self->__report_status },
236 );
237 }
238  
239  
240 sub on_startup {
241 # Placeholder, intended to be overrided
242 my $class = ref $_[0];
243 log_fatal "Worker class $class doesn't define on_startup() method";
244 }
245  
246 sub on_shutdown {
247 # Placeholder, can be overrided
248 }
249  
250 sub authorize_request {
251 # Placeholder, must to be overrided
252 my $class = ref $_[0];
253 log_fatal "Worker class $class doesn't define authorize_request() method";
254 return undef; # do not authorize
255 }
256  
257  
258 sub accept_notifications {
259 my ($self, %args) = @_;
260  
261 my $worker = $self->{_WORKER};
262 my $callbacks = $worker->{callbacks};
263  
264 my ($file, $line) = (caller)[1,2];
265 my $at = "at $file line $line\n";
266  
267 foreach my $fq_meth (keys %args) {
268  
269 $fq_meth =~ m/^ ( [\w-]+ (?: \.[\w-]+ )* )
270 \. ( [\w-]+ | \* ) $/x or die "Invalid notification method '$fq_meth' $at";
271  
272 my ($service, $method) = ($1, $2);
273  
274 my $callback = $self->__get_cb_coderef($fq_meth, $args{$fq_meth});
275  
276 die "Already accepting notifications '$fq_meth' $at" if exists $callbacks->{"msg.$fq_meth"};
277 $callbacks->{"msg.$fq_meth"} = $callback;
278  
279 my $local_bus = $self->{_BUS}->{bus_role};
280  
281 my $topic = "msg/$local_bus/$service/$method";
282 $topic =~ tr|.*|/#|;
283  
284 $self->{_BUS}->subscribe(
285 topic => $topic,
286 on_publish => sub {
287 # ($payload_ref, $properties) = @_;
288  
289 # Enqueue notification
290 push @{$worker->{task_queue_high}}, [ @_ ];
291  
292 unless ($worker->{queued_tasks}) {
293 $worker->{queued_tasks} = 1;
294 AnyEvent::postpone { $self->__drain_task_queue };
295 }
296 },
297 on_suback => sub {
298 my ($success, $prop) = @_;
299 die "Could not subscribe to topic '$topic' $at" unless $success;
300 }
301 );
302 }
303 }
304  
305 sub __get_cb_coderef {
306 my ($self, $method, $callback) = @_;
307  
308 if (ref $callback eq 'CODE') {
309 # Already a coderef
310 return $callback;
311 }
312 elsif (!ref($callback) && $self->can($callback)) {
313 # Return a reference to given method
314 no strict 'refs';
315 my $class = ref $self;
316 return \&{"${class}::${callback}"};
317 }
318 else {
319 my ($file, $line) = (caller(1))[1,2];
320 my $at = "at $file line $line\n";
321 die "Invalid handler '$callback' for '$method' $at";
322 }
323 }
324  
325  
326 sub accept_remote_calls {
327 my ($self, %args) = @_;
328  
329 my $worker = $self->{_WORKER};
330 my $callbacks = $worker->{callbacks};
331 my %subscribed_to;
332  
333 my ($file, $line) = (caller)[1,2];
334 my $at = "at $file line $line\n";
335  
336 foreach my $fq_meth (keys %args) {
337  
338 $fq_meth =~ m/^ ( [\w-]+ (?: \.[\w-]+ )* )
339 \. ( [\w-]+ | \* ) $/x or die "Invalid remote call method '$fq_meth' $at";
340  
341 my ($service, $method) = ($1, $2);
342  
343 my $callback = $self->__get_cb_coderef($fq_meth, $args{$fq_meth});
344  
345 die "Already accepting remote calls '$fq_meth' $at" if exists $callbacks->{"req.$fq_meth"};
346 $callbacks->{"req.$fq_meth"} = $callback;
347  
348 next if $subscribed_to{$service};
349 $subscribed_to{$service} = 1;
350  
351 if (keys %subscribed_to == 2) {
352 log_warn "Running multiple services within a single worker hurts load balancing $at";
353 }
354  
355 my $local_bus = $self->{_BUS}->{bus_role};
356  
357 my $topic = "\$share/BKPR/req/$local_bus/$service";
358 $topic =~ tr|.*|/#|;
359  
360 $self->{_BUS}->subscribe(
361 topic => $topic,
362 maximum_qos => 1,
363 on_publish => sub {
364 # ($payload_ref, $mqtt_properties) = @_;
365  
366 # Enqueue request
367 push @{$worker->{task_queue_low}}, [ @_ ];
368  
369 unless ($worker->{queued_tasks}) {
370 $worker->{queued_tasks} = 1;
371 AnyEvent::postpone { $self->__drain_task_queue };
372 }
373 },
374 on_suback => sub {
375 my ($success, $prop) = @_;
376 die "Could not subscribe to topic '$topic' $at" unless $success;
377 }
378 );
379 }
380 }
381  
382 my $_TASK_QUEUE_DEPTH = 0;
383  
384 sub __drain_task_queue {
385 my $self = shift;
386  
387 # Ensure that draining does not recurse
388 Carp::confess "Unexpected task queue processing recursion" if $_TASK_QUEUE_DEPTH;
389 $_TASK_QUEUE_DEPTH++;
390  
391 my $timing_tasks;
392  
393 unless (defined $BUSY_SINCE) {
394 # Measure time elapsed while processing requests
395 $BUSY_SINCE = Time::HiRes::time;
396 $timing_tasks = 1;
397 }
398  
399 my $worker = $self->{_WORKER};
400 my $client = $self->{_CLIENT};
401 my $task;
402  
403 # When requests or notifications are received these are not executed immediately
404 # because that could happen in the middle of the process of another request,
405 # so these tasks get queued until the worker is ready to process the next one.
406 #
407 # Callbacks are executed here, exception handling is done here, responses are
408 # sent back here. This is one of the most important methods of the framework.
409 #
410 # Notifications have higher priority and are processed first.
411  
412 DRAIN: {
413  
414 while ($task = shift @{$worker->{task_queue_high}}) {
415  
416 ## Notification
417  
418 my ($payload_ref, $mqtt_properties) = @$task;
419  
420 $worker->{notif_count}++;
421  
422 eval {
423  
424 my $request = decode_json($$payload_ref);
425  
426 unless (ref $request eq 'HASH' && $request->{jsonrpc} eq '2.0') {
427 log_error "Received invalid JSON-RPC 2.0 notification";
428 return;
429 }
430  
431 bless $request, 'Beekeeper::JSONRPC::Notification';
432 $request->{_mqtt_properties} = $mqtt_properties;
433  
434 my $method = $request->{method};
435  
436 unless (defined $method && $method =~ m/^([\.\w-]+)\.([\w-]+)$/) {
437 log_error "Received notification with invalid method '$method'";
438 return;
439 }
440  
441 my $cb = $worker->{callbacks}->{"msg.$1.$2"} ||
442 $worker->{callbacks}->{"msg.$1.*"};
443  
444 local $client->{caller_id} = $mqtt_properties->{'clid'};
445 local $client->{caller_addr} = $mqtt_properties->{'addr'};
446 local $client->{auth_data} = $mqtt_properties->{'auth'};
447  
448 unless (($self->authorize_request($request) || "") eq BKPR_REQUEST_AUTHORIZED) {
449 log_error "Notification '$method' was not authorized";
450 return;
451 }
452  
453 unless ($cb) {
454 log_error "No handler found for received notification '$method'";
455 return;
456 }
457  
458 $cb->($self, $request->{params}, $request);
459 };
460  
461 if ($@) {
462 # Got an exception while processing message
463 log_error $@;
464 $worker->{error_count}++;
465 }
466 }
467  
468 if ($task = shift @{$worker->{task_queue_low}}) {
469  
470 ## RPC Call
471  
472 my ($payload_ref, $mqtt_properties) = @$task;
473  
474 $worker->{call_count}++;
475 my ($request, $request_id, $result, $response);
476  
477 $result = eval {
478  
479 $request = decode_json($$payload_ref);
480  
481 unless (ref $request eq 'HASH' && $request->{jsonrpc} eq '2.0') {
482 log_error "Received invalid JSON-RPC 2.0 request";
483 die Beekeeper::JSONRPC::Error->invalid_request;
484 }
485  
486 $request_id = $request->{id};
487 my $method = $request->{method};
488  
489 bless $request, 'Beekeeper::JSONRPC::Request';
490 $request->{_mqtt_properties} = $mqtt_properties;
491  
492 unless (defined $method && $method =~ m/^([\.\w-]+)\.([\w-]+)$/) {
493 log_error "Received request with invalid method '$method'";
494 die Beekeeper::JSONRPC::Error->method_not_found;
495 }
496  
497 my $cb = $worker->{callbacks}->{"req.$1.$2"} ||
498 $worker->{callbacks}->{"req.$1.*"};
499  
500 local $client->{caller_id} = $mqtt_properties->{'clid'};
501 local $client->{caller_addr} = $mqtt_properties->{'addr'};
502 local $client->{auth_data} = $mqtt_properties->{'auth'};
503  
504 unless (($self->authorize_request($request) || "") eq BKPR_REQUEST_AUTHORIZED) {
505 log_error "Request '$method' was not authorized";
506 die Beekeeper::JSONRPC::Error->request_not_authorized;
507 }
508  
509 unless ($cb) {
510 log_error "No handler found for received request '$method'";
511 die Beekeeper::JSONRPC::Error->method_not_found;
512 }
513  
514 # Execute method handler
515 $cb->($self, $request->{params}, $request);
516 };
517  
518 if ($@) {
519 # Got an exception while executing method handler
520 if (blessed($@) && $@->isa('Beekeeper::JSONRPC::Error')) {
521 # Handled exception
522 $response = $@;
523 $worker->{error_count}++;
524 }
525 else {
526 # Unhandled exception
527 log_error $@;
528 $worker->{error_count}++;
529 $response = Beekeeper::JSONRPC::Error->server_error;
530 # Sending exact error to caller is very handy, but it is also a security risk
531 $response->{error}->{data} = $@ if $worker->{debug};
532 $worker->{error_count}++;
533 }
534 }
535 elsif (blessed($result) && $result->isa('Beekeeper::JSONRPC::Error')) {
536 # Explicit error response
537 $response = $result;
538 $worker->{error_count}++;
539 }
540 elsif ($request->{_async_response}) {
541 # Response was deferred and will be sent later
542 $worker->{in_progress}++;
543 $request->{_worker} = $self;
544 }
545 else {
546 # Build a success response
547 $response = {
548 jsonrpc => '2.0',
549 result => $result,
550 };
551 }
552  
553 if (defined $request_id && defined $response) {
554  
555 # Send back response to caller
556  
557 $response->{id} = $request_id;
558  
559 my $json = eval { $JSON->encode( $response ) };
560  
561 if ($@) {
562 # Probably response contains blessed references
563 log_error "Couldn't serialize response as JSON: $@";
564 $response = Beekeeper::JSONRPC::Error->server_error;
565 $response->{id} = $request_id;
566 $json = $JSON->encode( $response );
567 }
568  
569 # Request is acknowledged as received just after sending the response. So, if
570 # the process is abruptly interrupted here, the broker will send the request to
571 # another worker and it will be executed twice (acking the request just before
572 # processing it may cause unprocessed requests or undelivered responses)
573  
574 $self->{_BUS}->publish(
575 topic => $mqtt_properties->{'response_topic'},
576 addr => $mqtt_properties->{'addr'},
577 payload => \$json,
578 buffer_id => 'response',
579 );
580  
581 if (exists $mqtt_properties->{'packet_id'}) {
582  
583 $self->{_BUS}->puback(
584 packet_id => $mqtt_properties->{'packet_id'},
585 buffer_id => 'response',
586 );
587 }
588 else {
589 # Should not happen (clients must publish with QoS 1)
590 log_warn "Request published with QoS 0 to topic " . $mqtt_properties->{'topic'};
591 }
592  
593 $self->{_BUS}->flush_buffer( buffer_id => 'response' );
594 }
595 else {
596  
597 # Acknowledge requests that doesn't send a response right now (fire & forget calls
598 # and requests handled asynchronously), signaling the broker to send more requests
599  
600 $self->{_BUS}->puback(
601 packet_id => $mqtt_properties->{'packet_id'},
602 );
603 }
604 }
605  
606 redo DRAIN if (@{$worker->{task_queue_high}} || @{$worker->{task_queue_low}});
607  
608 # Execute tasks postponed until task queue is empty
609 if (exists $worker->{postponed}) {
610 $_->() foreach @{$worker->{postponed}};
611 delete $worker->{postponed};
612 }
613 }
614  
615 $_TASK_QUEUE_DEPTH--;
616  
617 if (defined $timing_tasks) {
618 $BUSY_TIME += Time::HiRes::time - $BUSY_SINCE;
619 undef $BUSY_SINCE;
620 }
621  
622 $worker->{queued_tasks} = 0;
623 }
624  
625 sub __send_response {
626 my ($self, $request, $result) = @_;
627  
628 # Send back async response to caller
629  
630 my ($timing_tasks, $response);
631  
632 $self->{_WORKER}->{in_progress}--;
633  
634 # fire & forget calls doesn't expect responses
635 return unless defined $request->{id};
636  
637 unless (defined $BUSY_SINCE) {
638 $BUSY_SINCE = Time::HiRes::time;
639 $timing_tasks = 1;
640 }
641  
642 if (blessed($result) && $result->isa('Beekeeper::JSONRPC::Error')) {
643 # Explicit error response
644 $response = $result;
645 $self->{_WORKER}->{error_count}++;
646 }
647 else {
648 # Build a success response
649 $response = {
650 jsonrpc => '2.0',
651 result => $result,
652 };
653 }
654  
655 $response->{id} = $request->{id};
656  
657 local $@;
658 my $json = eval { $JSON->encode( $response ) };
659  
660 if ($@) {
661 # Probably response contains blessed references
662 log_error "Couldn't serialize response as JSON: $@";
663 $response = Beekeeper::JSONRPC::Error->server_error;
664 $response->{id} = $request->{id};
665 $json = $JSON->encode( $response );
666 $self->{_WORKER}->{error_count}++;
667 }
668  
669 $self->{_BUS}->publish(
670 topic => $request->{_mqtt_properties}->{'response_topic'},
671 addr => $request->{_mqtt_properties}->{'addr'},
672 payload => \$json,
673 );
674  
675 if (defined $timing_tasks) {
676 $BUSY_TIME += Time::HiRes::time - $BUSY_SINCE;
677 undef $BUSY_SINCE;
678 }
679 }
680  
681  
682 sub stop_accepting_notifications {
683 my ($self, @methods) = @_;
684  
685 my ($file, $line) = (caller)[1,2];
686 my $at = "at $file line $line\n";
687  
688 die "No method specified $at" unless @methods;
689  
690 foreach my $fq_meth (@methods) {
691  
692 $fq_meth =~ m/^ ( [\w-]+ (?: \.[\w-]+ )* )
693 \. ( [\w-]+ | \* ) $/x or die "Invalid method '$fq_meth' $at";
694  
695 my ($service, $method) = ($1, $2);
696  
697 my $worker = $self->{_WORKER};
698  
699 unless (defined $worker->{callbacks}->{"msg.$fq_meth"}) {
700 log_warn "Not previously accepting notifications '$fq_meth' $at";
701 next;
702 }
703  
704 my $local_bus = $self->{_BUS}->{bus_role};
705  
706 my $topic = "msg/$local_bus/$service/$method";
707 $topic =~ tr|.*|/#|;
708  
709 # Cannot remove callbacks right now, as new notifications could be in flight or be
710 # already queued. We must wait for unsubscription completion, and then until the
711 # notification queue is empty to ensure that all received ones were processed. And
712 # even then wait a bit more, as some brokers may send messages *after* unsubscription.
713 my $postpone = sub {
714  
715 my $unsub_tmr; $unsub_tmr = AnyEvent->timer(
716 after => $UNSUBSCRIBE_LINGER, cb => sub {
717  
718 delete $worker->{callbacks}->{"msg.$fq_meth"};
719 undef $unsub_tmr;
720 }
721 );
722 };
723  
724 $self->{_BUS}->unsubscribe(
725 topic => $topic,
726 on_unsuback => sub {
727 my ($success, $prop) = @_;
728  
729 log_error "Could not unsubscribe from topic '$topic' $at" unless $success;
730  
731 my $postponed = $worker->{postponed} ||= [];
732 push @$postponed, $postpone;
733  
734 AnyEvent::postpone { $self->__drain_task_queue };
735 }
736 );
737 }
738 }
739  
740  
741 sub stop_accepting_calls {
742 my ($self, @methods) = @_;
743  
744 my ($file, $line) = (caller)[1,2];
745 my $at = "at $file line $line\n";
746  
747 die "No method specified $at" unless @methods;
748  
749 foreach my $fq_meth (@methods) {
750  
751 $fq_meth =~ m/^ ( [\w-]+ (?: \.[\w-]+ )* )
752 \. ( [\w-]+ | \* ) $/x or die "Invalid remote call method '$fq_meth' $at";
753  
754 my ($service, $method) = ($1, $2);
755  
756 unless ($method eq '*') {
757 # Known limitation. As all calls for an entire service class are received
758 # through a single MQTT subscription (in order to load balance them), it is
759 # not possible to reject a single method. A workaround is to use a different
760 # class for each method that need to be individually rejected.
761 die "Cannot stop accepting individual methods, only '$service.*' is allowed $at";
762 }
763  
764 my $worker = $self->{_WORKER};
765 my $callbacks = $worker->{callbacks};
766  
767 my @cb_keys = grep { $_ =~ m/^req.\Q$service\E\b/ } keys %$callbacks;
768  
769 unless (@cb_keys) {
770 log_warn "Not previously accepting remote calls '$fq_meth' $at";
771 next;
772 }
773  
774 my $local_bus = $self->{_BUS}->{bus_role};
775  
776 my $topic = "\$share/BKPR/req/$local_bus/$service";
777 $topic =~ tr|.*|/#|;
778  
779 # Cannot remove callbacks right now, as new requests could be in flight or be already
780 # queued. We must wait for unsubscription completion, and then until the task queue
781 # is empty to ensure that all received requests were processed. And even then wait a
782 # bit more, as some brokers may send requests *after* unsubscription.
783 my $postpone = sub {
784  
785 $worker->{stop_cv}->begin;
786  
787 my $unsub_tmr; $unsub_tmr = AnyEvent->timer(
788 after => $UNSUBSCRIBE_LINGER, cb => sub {
789  
790 delete $worker->{callbacks}->{$_} foreach @cb_keys;
791 delete $worker->{subscriptions}->{$service};
792 undef $unsub_tmr;
793  
794 return unless $worker->{shutting_down};
795  
796 if ($worker->{in_progress} > 0) {
797  
798 # The task queue is empty now, but an asynchronous method handler is
799 # still busy processing some requests received previously. Wait for
800 # these requests to be completed before telling _work_forever to stop
801  
802 my $wait_time = 60;
803 $worker->{stop_cv}->begin;
804  
805 my $busy_tmr; $busy_tmr = AnyEvent->timer( after => 1, interval => 1, cb => sub {
806 unless ($worker->{in_progress} > 0 && --$wait_time > 0) {
807 undef $busy_tmr;
808 $worker->{stop_cv}->end;
809 }
810 });
811 }
812  
813 # Tell _work_forever to stop
814 $worker->{stop_cv}->end;
815 }
816 );
817 };
818  
819 $self->{_BUS}->unsubscribe(
820 topic => $topic,
821 on_unsuback => sub {
822 my ($success, $prop) = @_;
823  
824 log_error "Could not unsubscribe from topic '$topic' $at" unless $success;
825  
826 my $postponed = $worker->{postponed} ||= [];
827 push @$postponed, $postpone;
828  
829 AnyEvent::postpone { $self->__drain_task_queue };
830 }
831 );
832 }
833 }
834  
835  
836 sub __work_forever {
837 my $self = shift;
838  
839 # Called by WorkerPool->spawn_worker
840  
841 eval {
842  
843 my $worker = $self->{_WORKER};
844  
845 $worker->{stop_cv} = AnyEvent->condvar;
846  
847 # Blocks here until stop_working is called
848 $worker->{stop_cv}->recv;
849  
850 $self->on_shutdown;
851  
852 $self->__report_exit;
853 };
854  
855 if ($@) {
856 log_fatal "Worker died: $@";
857 CORE::exit(255);
858 }
859  
860 if ($self->{_BUS}->{is_connected}) {
861 $self->{_BUS}->disconnect;
862 }
863 }
864  
865  
866 sub stop_working {
867 my ($self, %args) = @_;
868  
869 my $worker = $self->{_WORKER};
870  
871 # This is the default handler for TERM signal
872  
873 return if $worker->{shutting_down};
874 $worker->{shutting_down} = 1;
875  
876 unless (defined $worker->{stop_cv}) {
877 # Worker did not completed initialization yet
878 CORE::exit(0);
879 }
880  
881 my %services;
882 foreach my $fq_meth (keys %{$worker->{callbacks}}) {
883 next unless $fq_meth =~ m/^req\.(?!_sync)(.*)\./;
884 $services{$1} = 1;
885 }
886  
887 if (keys %services) {
888  
889 # Cannot exit right now, as some requests could be in flight or already queued.
890 # So tell the broker to stop sending requests, and exit after the task queue is empty
891 foreach my $service (keys %services) {
892  
893 $self->stop_accepting_calls( $service . '.*' );
894 }
895 }
896 else {
897 # Tell _work_forever to stop
898 $worker->{stop_cv}->send;
899 }
900 }
901  
902  
903 sub __report_status {
904 my $self = shift;
905  
906 my $worker = $self->{_WORKER};
907 my $client = $self->{_CLIENT};
908  
909 my $now = Time::HiRes::time;
910 my $period = $now - ($worker->{last_report} || ($now - 1));
911  
912 $worker->{last_report} = $now;
913  
914 # Average calls per second
915 my $cps = sprintf("%.2f", $worker->{call_count} / $period);
916 $worker->{call_count} = 0;
917  
918 # Average notifications per second
919 my $nps = sprintf("%.2f", $worker->{notif_count} / $period);
920 $worker->{notif_count} = 0;
921  
922 # Average errors per second
923 my $err = sprintf("%.2f", $worker->{error_count} / $period);
924 $worker->{error_count} = 0;
925  
926 # Average load as percentage of wall clock busy time (not cpu usage)
927 my $load = sprintf("%.2f", ($BUSY_TIME - $worker->{busy_time}) / $period * 100);
928 $worker->{busy_time} = $BUSY_TIME;
929  
930 # Queues
931 my %queues;
932 foreach my $queue (keys %{$worker->{callbacks}}) {
933 next unless $queue =~ m/^req\.(?!_sync)(.*)\./;
934 $queues{$1} = 1;
935 }
936  
937 local $client->{auth_data} = $AUTH_TOKENS{'BKPR_SYSTEM'};
938 local $client->{caller_id};
939  
940 # Tell any supervisor our stats
941 $self->fire_remote(
942 method => '_bkpr.supervisor.worker_status',
943 params => {
944 class => ref($self),
945 host => $worker->{hostname},
946 pool => $worker->{pool_id},
947 pid => $$,
948 cps => $cps,
949 nps => $nps,
950 err => $err,
951 load => $load,
952 queue => [ keys %queues ],
953 },
954 );
955 }
956  
957 sub __report_exit {
958 my $self = shift;
959  
960 return unless $self->{_BUS}->{is_connected};
961  
962 my $worker = $self->{_WORKER};
963 my $client = $self->{_CLIENT};
964  
965 local $client->{auth_data} = $AUTH_TOKENS{'BKPR_SYSTEM'};
966 local $client->{caller_id};
967  
968 $self->fire_remote(
969 method => '_bkpr.supervisor.worker_exit',
970 params => {
971 class => ref($self),
972 host => $worker->{hostname},
973 pool => $worker->{pool_id},
974 pid => $$,
975 },
976 );
977 }
978  
979 1;
980  
981 __END__