| 1 | package RWDE::Scheduler::SchedulerWorker; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Error qw(:try); | ||
| 7 | |||
| 8 | use RWDE::Exceptions; | ||
| 9 | |||
| 10 | use base qw(RWDE::DB::Deletable RWDE::DB::Record); | ||
| 11 | |||
| 12 | our ($db, $table, $index, $id, @fieldnames, $ccrcontext, %fields, %static_fields, %modifiable_fields, @static_fieldnames, @modifiable_fieldnames); | ||
| 13 | |||
| 14 | use vars qw($VERSION); | ||
| 15 | $VERSION = sprintf "%d", q$Revision: 534 $ =~ /(\d+)/; | ||
| 16 | |||
| 17 | BEGIN { | ||
| 18 | $table = 'workers'; | ||
| 19 | $id = 'worker_id'; | ||
| 20 | $index = 'workers_worker_id_seq'; | ||
| 21 | |||
| 22 | #all of the static fields present in the worker table | ||
| 23 | %static_fields = ( | ||
| 24 | |||
| 25 | # Field => [Type, Descr] | ||
| 26 | worker_id => [ 'int', 'Worker ID' ], | ||
| 27 | ); | ||
| 28 | |||
| 29 | #all of the fields allowed to be modified in the worker table | ||
| 30 | %modifiable_fields = ( | ||
| 31 | |||
| 32 | # Field => [Type, Descr] | ||
| 33 | action_id => [ 'int', 'Scheduler ID' ], | ||
| 34 | scheduler_id => [ 'int', 'Scheduler ID' ], | ||
| 35 | worker_pid => [ 'int', 'Worker process id' ], | ||
| 36 | |||
| 37 | ); | ||
| 38 | |||
| 39 | %fields = (%static_fields, %modifiable_fields); | ||
| 40 | |||
| 41 | @static_fieldnames = sort keys %static_fields; | ||
| 42 | @modifiable_fieldnames = sort keys %modifiable_fields; | ||
| 43 | @fieldnames = sort keys %fields; | ||
| 44 | |||
| 45 | } | ||
| 46 | |||
| 47 | ## @method object get_db() | ||
| 48 | # (Enter get_db info here) | ||
| 49 | # @return | ||
| 50 | sub get_db { | ||
| 51 | my ($self, $params) = @_; | ||
| 52 | |||
| 53 | return 'default'; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub cleanup { | ||
| 57 | my ($self, $params) = @_; | ||
| 58 | |||
| 59 | $self->delete_record; | ||
| 60 | |||
| 61 | return; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub run { | ||
| 65 | my ($self, $params) = @_; | ||
| 66 | |||
| 67 | my @required = qw( job scheduler_id ); | ||
| 68 | $self->check_params({ required => \@required, supplied => $params }); | ||
| 69 | |||
| 70 | # to avoid contention: release the reference to parent's db handle | ||
| 71 | # the handles are shared between parents and children | ||
| 72 | RWDE::DB::DbRegistry->destroy_dbh({ db => $self->get_db() }); | ||
| 73 | |||
| 74 | my $worker = $self->new(); | ||
| 75 | my $job = $$params{job}; | ||
| 76 | |||
| 77 | $worker->scheduler_id($$params{scheduler_id}); | ||
| 78 | $worker->action_id($job->action_id); | ||
| 79 | $worker->worker_pid($$); | ||
| 80 | $worker->create_record; | ||
| 81 | |||
| 82 | try { | ||
| 83 | $job->process; | ||
| 84 | } | ||
| 85 | catch Error with { | ||
| 86 | my $ex = shift; | ||
| 87 | $self->syslog_msg('info', "Worker::run caught an unhandled exception $ex "); | ||
| 88 | }; | ||
| 89 | |||
| 90 | $worker->delete_record; | ||
| 91 | |||
| 92 | return (0); | ||
| 93 | } | ||
| 94 | |||
| 95 | 1; |