App/MultiModule/Task.pm untested

filter: Statementstmt 0.0% 0 / 29 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 47 CC 5 SCAR 26.9
CC
5
Cov
0%
emit
30.0
is_stateful
2.0
set_config
2.0
CRAP 14.7
1 package App::MultiModule::Task;
2 $App::MultiModule::Task::VERSION = '1.171870';
3 use strict;use warnings;
4 use IPC::Transit;
5 use parent 'App::MultiModule::Core';
6  
7 =head1 METHODS
8  
9 =head2 emit($message)
10  
11 A task uses this to send a message to the Router.
12  
13 The idea is to never address messages directly to tasks; each task
14 should only 'blindly' emit messages, and leave the decisions about
15 delivery and transformations to the Router.
16  
17 You can over-ride this method in a task; just be sure to call the
18 emit method in the Task class in order to send messages.
19  
20 This method sets the following fields on the message, automatically:
21  
22 =over 4
23  
24 =item source
25  
26 This field contains the name of the task that called emit
27  
28 =item previous_source
29  
30 This field contains the previous value of the source field
31  
32 =back
33  
34 Example:
35  
36 $self->emit({something => 'important'});
37  
38 =cut
39 sub emit {
40 my $self = shift;
41 my $message = shift;
42 $message->{previous_source} = $message->{source}
43 if $message->{source};
44 { my @info = caller(0);
45 $message->{source} = $info[0];
46 $message->{source} =~ s/.*:://;
47 }
48 $self->debug('Task: emit: sending to router', message => $message)
49 if $self->{debug} > 5;
50 $App::MultiModule::Task::emit_counts->{$message->{source}} = 0
51 unless $App::MultiModule::Task::emit_counts->{$message->{source}};
52 $App::MultiModule::Task::emit_counts->{$message->{source}}++;
53 IPC::Transit::send(qname => 'Router', message => $message);
54 $self->debug('emit: local transit queue info', local_queues => $IPC::Transit::local_queues)
55 if $self->{debug} > 5;
56 }
57  
58 =head2 is_stateful
59  
60 This method is called by the MultiModule daemon to determine if your task
61 is stateful. is_stateful() returns 'false' in this class. Override
62 in your task to return some true value if you want MultiModule to maintain
63 your state.
64  
65 Example:
66  
67 sub is_stateful {
68 return 'yes';
69 }
70  
71 =cut
72 sub is_stateful {
73 return undef;
74 }
75  
76 =head2 set_config($config)
77  
78 This method is called by the MultiModule daemon when it has updated
79 config for your task. The default behaviour (as implemented in the
80 Task class) is to simply take the passed config and assign it to
81 the 'config' field on the $self reference.
82  
83 The other important purpose of this method is to give your task a chance
84 to setup various POE events.
85  
86 If your task needs to, for instance, follow logfiles, that setup should
87 happen in this method.
88  
89 This method is re-called every time the underlying config changes. So
90 whatever setup that was done in an initial process-space invocation of
91 this should probably be re-considered in future calls, since the setup
92 was likely controlled by the config, and future calls have differences
93 in the config.
94  
95 It is important to be mindful of this flow; it would be easy to leak
96 descriptors and/or resources over various calls to set_config().
97  
98 Concretely, if an initial process-space call to this method setup
99 a POE event to watch a file /var/log/some_file.log, as defined in
100 the passed config, and a later call did not have any reference to
101 following /var/log/some_file.log, it is expected that the POE
102 event associated with /var/log/some_file.log would be deallocated.
103  
104 Example: (copied from lib/MultiModuleTest/Example1.pm in this distribution)
105  
106 sub set_config {
107 my $self = shift;
108 my $config = shift;
109 $self->{config} = $config;
110 $self->{state} = { ct => 0 } unless $self->{state};
111 $self->named_recur( #See perldoc App::MultiModule::Core
112 recur_name => 'Example1',
113 repeat_interval => 1, #runs every second
114 work => sub {
115 my $message = {
116 ct => $self->{state}->{ct}++,
117 outstr => $config->{outstr},
118 };
119 $self->emit($message);
120 },
121 }
122 }
123  
124 =cut
125 sub set_config {
126 my $self = shift;
127 my $config = shift;
128 $self->{config} = $config;
129 }
130  
131 1;