| 1 | package Hopkins::Worker; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Hopkins::Worker - hopkins worker session | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Hopkins::Worker encapsulates a POE session created by | ||
| 12 | Hopkins::Queue->spawn_worker via POE::Component::JobQueue. | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | use base 'Class::Accessor::Fast'; | ||
| 17 | |||
| 18 | use POE; | ||
| 19 | use POE::Filter::Reference; | ||
| 20 | use POE::Wheel::Run; | ||
| 21 | use Class::Accessor::Fast; | ||
| 22 | |||
| 23 | use YAML; | ||
| 24 | |||
| 25 | use Hopkins::Worker::Native; | ||
| 26 | |||
| 27 | __PACKAGE__->mk_accessors(qw(alias postback work child status timer)); | ||
| 28 | |||
| 29 | use constant HOPKINS_WORKER_MAX_STATUS_WAIT => 10; | ||
| 30 | |||
| 31 | =head1 STATES | ||
| 32 | |||
| 33 | =over 4 | ||
| 34 | |||
| 35 | =item spawn | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | sub new | ||
| 40 | { | ||
| 41 | my $self = shift->SUPER::new(@_); | ||
| 42 | |||
| 43 | my $method = $self->work->task->class ? 'perl' : 'exec'; | ||
| 44 | my $source = $self->work->task->class || $self->work->task->cmd; | ||
| 45 | |||
| 46 | Hopkins->log_debug("spawning worker: type=$method; source=$source"); | ||
| 47 | |||
| 48 | POE::Session->create | ||
| 49 | ( | ||
| 50 | object_states => | ||
| 51 | [ | ||
| 52 | $self => | ||
| 53 | { | ||
| 54 | _start => 'start', | ||
| 55 | _stop => 'stop', | ||
| 56 | |||
| 57 | execute => 'execute', | ||
| 58 | terminate => 'terminate', | ||
| 59 | stdout => 'stdout', | ||
| 60 | stderr => 'stderr', | ||
| 61 | done => 'done', | ||
| 62 | statuswait => 'statuswait', | ||
| 63 | reap => 'reap', | ||
| 64 | shutdown => 'shutdown' | ||
| 65 | } | ||
| 66 | ] | ||
| 67 | ); | ||
| 68 | |||
| 69 | return $self; | ||
| 70 | } | ||
| 71 | |||
| 72 | =item start | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | sub start | ||
| 77 | { | ||
| 78 | my $self = $_[OBJECT]; | ||
| 79 | my $kernel = $_[KERNEL]; | ||
| 80 | |||
| 81 | $self->work->worker($self); | ||
| 82 | $self->work->date_started(DateTime->now(time_zone => 'local')); | ||
| 83 | |||
| 84 | $kernel->post(store => notify => task_started => $self->work->serialize); | ||
| 85 | |||
| 86 | # set the name of this session's alias based on the queue and session ID | ||
| 87 | my $session = $kernel->get_active_session; | ||
| 88 | |||
| 89 | $self->alias(join '.', $self->work->queue->alias, 'worker', $session->ID); | ||
| 90 | $kernel->alias_set($self->alias); | ||
| 91 | |||
| 92 | Hopkins->log_debug('worker session created'); | ||
| 93 | |||
| 94 | $kernel->post($self->alias => 'execute'); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub execute | ||
| 98 | { | ||
| 99 | my $self = $_[OBJECT]; | ||
| 100 | my $kernel = $_[KERNEL]; | ||
| 101 | |||
| 102 | # determine the Program argument based upon what method | ||
| 103 | # we're using. POE::Wheel::Run will execute both native | ||
| 104 | # perl code as well as external binaries depending upon | ||
| 105 | # whether the argument is a coderef or a simple scalar. | ||
| 106 | |||
| 107 | my $program = $self->work->task->class | ||
| 108 | ? new Hopkins::Worker::Native { work => $self->work } | ||
| 109 | : $self->work->task->cmd; | ||
| 110 | |||
| 111 | # construct the arguments neccessary for POE::Wheel::Run | ||
| 112 | |||
| 113 | my %args = | ||
| 114 | ( | ||
| 115 | Program => $program, | ||
| 116 | StdoutEvent => 'stdout', | ||
| 117 | StderrEvent => 'stderr', | ||
| 118 | StdoutFilter => new POE::Filter::Reference 'YAML' | ||
| 119 | ); | ||
| 120 | |||
| 121 | # after making sure to setup appropriate signal handlers | ||
| 122 | # beforehand, spawn the actual worker in a child process | ||
| 123 | # via POE::Wheel::Run. this protects us from code that | ||
| 124 | # may potentially block POE for a very long time. it | ||
| 125 | # also isolates hopkins from code that would otherwise | ||
| 126 | # be able to alter the running environment. | ||
| 127 | |||
| 128 | $kernel->sig(CHLD => 'done'); | ||
| 129 | $self->child(new POE::Wheel::Run %args); | ||
| 130 | } | ||
| 131 | |||
| 132 | sub stop | ||
| 133 | { | ||
| 134 | my $self = $_[OBJECT]; | ||
| 135 | my $kernel = $_[KERNEL]; | ||
| 136 | my $heap = $_[HEAP]; | ||
| 137 | |||
| 138 | Hopkins->log_debug('session destroyed'); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub done | ||
| 142 | { | ||
| 143 | my $self = $_[OBJECT]; | ||
| 144 | my $kernel = $_[KERNEL]; | ||
| 145 | my $signal = $_[ARG0]; | ||
| 146 | my $pid = $_[ARG1]; | ||
| 147 | my $status = $_[ARG2]; | ||
| 148 | |||
| 149 | return if $pid != $self->child->PID; | ||
| 150 | |||
| 151 | Hopkins->log_debug("child process $pid exited with status $status"); | ||
| 152 | |||
| 153 | $self->timer(time); | ||
| 154 | $self->child(undef); | ||
| 155 | |||
| 156 | $kernel->sig('CHLD'); | ||
| 157 | $kernel->post($self->alias => 'statuswait'); | ||
| 158 | |||
| 159 | if ($self->work->task->cmd) { | ||
| 160 | $self->status({}) unless $self->status; | ||
| 161 | |||
| 162 | $self->status->{error} = "command exited with status $status" | ||
| 163 | if $status != 0; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | sub statuswait | ||
| 168 | { | ||
| 169 | my $self = $_[OBJECT]; | ||
| 170 | my $kernel = $_[KERNEL]; | ||
| 171 | |||
| 172 | return $kernel->post($self->alias => 'reap') if $self->status; | ||
| 173 | |||
| 174 | if (time > $self->timer + HOPKINS_WORKER_MAX_STATUS_WAIT) { | ||
| 175 | use Data::Dumper; | ||
| 176 | $Data::Dumper::Indent = 1; | ||
| 177 | Hopkins->log_error('worker did not report a status!'); | ||
| 178 | Hopkins->log_error(Dumper $self); | ||
| 179 | |||
| 180 | $kernel->yield('shutdown'); | ||
| 181 | } else { | ||
| 182 | $kernel->alarm(statuswait => time + 1); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | sub reap | ||
| 187 | { | ||
| 188 | my $self = $_[OBJECT]; | ||
| 189 | my $kernel = $_[KERNEL]; | ||
| 190 | |||
| 191 | my $task = $self->work->task->name; | ||
| 192 | |||
| 193 | if ($self->status->{error}) { | ||
| 194 | Hopkins->log_error("worker failure executing $task"); | ||
| 195 | $kernel->call(manager => queue_failure => $self->work->queue, $self->status->{error}); | ||
| 196 | $self->work->succeeded(0); | ||
| 197 | } else { | ||
| 198 | if ($self->status->{terminated}) { | ||
| 199 | Hopkins->log_info("worker terminated executing $task"); | ||
| 200 | $self->work->aborted(1); | ||
| 201 | } else { | ||
| 202 | Hopkins->log_info("worker successfully executed $task"); | ||
| 203 | $self->work->succeeded(1); | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | $kernel->yield('shutdown'); | ||
| 208 | } | ||
| 209 | |||
| 210 | sub terminate | ||
| 211 | { | ||
| 212 | my $self = $_[OBJECT]; | ||
| 213 | my $kernel = $_[KERNEL]; | ||
| 214 | |||
| 215 | $self->child->kill; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub shutdown | ||
| 219 | { | ||
| 220 | my $self = $_[OBJECT]; | ||
| 221 | my $kernel = $_[KERNEL]; | ||
| 222 | |||
| 223 | # use the postback to inform the JobQueue component that | ||
| 224 | # we have finished executing. | ||
| 225 | |||
| 226 | $self->postback->(); # ($pid, $status); | ||
| 227 | |||
| 228 | # we have to remove the session alias from the kernel, | ||
| 229 | # else POE will never destroy the session. | ||
| 230 | |||
| 231 | $kernel->alias_remove($self->alias); | ||
| 232 | } | ||
| 233 | |||
| 234 | sub stdout | ||
| 235 | { | ||
| 236 | my $self = $_[OBJECT]; | ||
| 237 | |||
| 238 | $self->work->task->class | ||
| 239 | ? $self->status($_[ARG0]) | ||
| 240 | : Hopkins->log_worker_stdout($self->work->task->name, $_[ARG0]); | ||
| 241 | } | ||
| 242 | |||
| 243 | sub stderr | ||
| 244 | { | ||
| 245 | my $self = $_[OBJECT]; | ||
| 246 | |||
| 247 | Hopkins->log_worker_stderr($self->work->task->name, $_[ARG0]); | ||
| 248 | |||
| 249 | $self->work->output(($self->work->output || '') . $_[ARG0]); | ||
| 250 | } | ||
| 251 | |||
| 252 | =back | ||
| 253 | |||
| 254 | =head1 AUTHOR | ||
| 255 | |||
| 256 | Mike Eldridge <diz@cpan.org> | ||
| 257 | |||
| 258 | =head1 LICENSE | ||
| 259 | |||
| 260 | This program is free software; you may redistribute it | ||
| 261 | and/or modify it under the same terms as Perl itself. | ||
| 262 | |||
| 263 | =cut | ||
| 264 | |||
| 265 | 1; |