| 1 | package Hopkins::Worker::Native; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Hopkins::Worker::Native - perl worker | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Hopkins::Worker::Native represents the special case of a | ||
| 12 | worker that will be executing a perl task. it wraps the | ||
| 13 | execution in order to return results to hopkins. it also | ||
| 14 | does pre-execution cleansing of the log4perl environment. | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | use base 'Class::Accessor::Fast'; | ||
| 19 | |||
| 20 | __PACKAGE__->mk_accessors(qw(work status filter)); | ||
| 21 | |||
| 22 | =head1 METHODS | ||
| 23 | |||
| 24 | =over 4 | ||
| 25 | |||
| 26 | =item new | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | sub new | ||
| 31 | { | ||
| 32 | my $self = shift->SUPER::new(@_); | ||
| 33 | |||
| 34 | # create a POE::Filter object; status information will | ||
| 35 | # be reported back to the controlling worker object via | ||
| 36 | # this filter. | ||
| 37 | |||
| 38 | $self->filter(new POE::Filter::Reference 'YAML'); | ||
| 39 | $self->status({}); | ||
| 40 | |||
| 41 | return sub { $self->execute }; | ||
| 42 | } | ||
| 43 | |||
| 44 | =item execute | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | sub execute | ||
| 49 | { | ||
| 50 | my $self = shift; | ||
| 51 | |||
| 52 | # begin processing | ||
| 53 | |||
| 54 | eval { $self->_execute }; | ||
| 55 | |||
| 56 | if (my $err = $@) { | ||
| 57 | Hopkins->log_worker_stderr($self->work->task->name, "execution failed: $err"); | ||
| 58 | $self->status->{error} = $err unless $self->status->{terminated}; | ||
| 59 | } | ||
| 60 | |||
| 61 | $self->report; | ||
| 62 | |||
| 63 | # POE::Wheel::Run doesn't execute DESTROY and END blocks | ||
| 64 | # since it exits the child process by using POSIX::_exit | ||
| 65 | # instead of exit(). this prevents log4perl appenders | ||
| 66 | # such as Log::Dispatch::Email from flushing buffers. | ||
| 67 | # although the task may not be using log4perl, there's | ||
| 68 | # no harm in calling it explicitly. it will probably | ||
| 69 | # save the user some headache. | ||
| 70 | |||
| 71 | Log::Log4perl::Logger::cleanup(); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _execute | ||
| 75 | { | ||
| 76 | my $self = shift; | ||
| 77 | |||
| 78 | $SIG{TERM} = sub { $self->status->{terminated} = 1; die 'terminated' }; | ||
| 79 | |||
| 80 | my $class = $self->work->task->class; | ||
| 81 | my $file = "$class.pm"; | ||
| 82 | |||
| 83 | $file =~ s{::}{/}g; | ||
| 84 | |||
| 85 | # immediately undefine the log4perl config watcher. | ||
| 86 | # the logic in Log::Log4perl->init_and_watch will | ||
| 87 | # use the existing configuration if it is called at | ||
| 88 | # a later time. this will cause problems if any of | ||
| 89 | # the perl workers use init_and_watch. | ||
| 90 | # | ||
| 91 | # this should probably be considered a bug. it's | ||
| 92 | # not init()ing and watching. just more watching. | ||
| 93 | |||
| 94 | $Log::Log4perl::Config::WATCHER = undef; | ||
| 95 | |||
| 96 | # redirect STDOUT to STDERR so that we can use | ||
| 97 | # the original STDOUT pipe to report status | ||
| 98 | # information back to hopkins via YAML | ||
| 99 | |||
| 100 | open STATUS, '>&STDOUT'; | ||
| 101 | open STDOUT, '>&STDERR'; | ||
| 102 | |||
| 103 | eval { require $file; $class->new({ options => $self->work->options })->run }; | ||
| 104 | |||
| 105 | my $err = $@; | ||
| 106 | |||
| 107 | $SIG{TERM} = 'IGNORE'; | ||
| 108 | |||
| 109 | if ($err) { | ||
| 110 | print STDERR $err; | ||
| 111 | $self->status->{error} = $err unless $self->status->{terminated}; | ||
| 112 | Hopkins->log_worker_stderr($self->work->task->name, $err); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | sub report | ||
| 117 | { | ||
| 118 | my $self = shift; | ||
| 119 | |||
| 120 | # make sure to close the handle so that hopkins will | ||
| 121 | # receive the information before the child exits. | ||
| 122 | |||
| 123 | print STATUS $self->filter->put([ $self->status ])->[0]; | ||
| 124 | close STATUS; | ||
| 125 | } | ||
| 126 | |||
| 127 | =back | ||
| 128 | |||
| 129 | =head1 AUTHOR | ||
| 130 | |||
| 131 | Mike Eldridge <diz@cpan.org> | ||
| 132 | |||
| 133 | =head1 LICENSE | ||
| 134 | |||
| 135 | This program is free software; you may redistribute it | ||
| 136 | and/or modify it under the same terms as Perl itself. | ||
| 137 | |||
| 138 | =cut | ||
| 139 | |||
| 140 | 1; |