Job::Machine::Worker
version 0.26
my $worker = My::Worker->new(dbh => $dbh, queue => 'job.task'); $worker->receive;
Job::Machine::Worker inherits from Job::Machine::Base. All you have to do is package My::Worker; use base 'Job::Machine::Worker'; sub process { my ($self, $task) = @_; $queuename = $task->{name}; ... do stuff };
Push the internal chores onto the chores list.
By pushing, we make sure that we don't destroy any existing chores.
NOTE. This is an internal method, meant to make sure that important Job::Machine tasks get done.
Takes a coderef and push it onto the chores list.
The supplied coderef can do anything, but is supposed to perform some kind of housekeeping.
It takes turns with the other chores, including the internal ones.
Job::Machine::Worker - Base class for Job Workers
A worker process always needs to subclass the process method with the real functionality.
startup will be called before any tasks are fetched and any processing is done. Call this method for one-time initializing.
Subclassable process method. E.g. sub process { my ($self, $data) = @_; ... process $data $self->reply({answer => 'Something'}); };
If the default of 30 minutes isn't suitable, return the number of seconds a process is expected to run.
A task will not be killed if it runs for longer than max_runtime. This setting is only used when reviving tasks that are suspected to be dead.
If the default of 5 minutes isn't suitable, return the number of seconds the worker should wait for notifications before doing housekeeping chores.
If you don't want the worker to perform any housekeeping tasks, return undef
If the default of 3 times isn't suitable, return the number of times a task is retried before failing.
If the default of 30 days isn't suitable, return the number of days a task will remain in the database before being removed.
Return 0 if you never want tasks to be removed.
Worker will wait for next message if this method returns true.
$worker->reply($some_structure); Reply to a message. Use from within a Worker's process method. Marks the task as done,
Use from within a Worker's process method. $worker->result($result_data); Save the result of the task. Marks the task as done,
Use from within a Worker's process method. $worker->error_result($result_data); Save the result of the task. Does NOT change the job status,
Get the DB class. From this it's possible to get the database handle my $dbh = $self->db->dbh; If you use the same database for Job::Machine as for your other data, this handle can be used by your worker module.
Get the current task id.
$worker->receive; Starts the Worker's receive loop. receive subscribes the worker to the queue and waits for a message to be passed along. It will first see if there are any messages to be processed.
Job::Machine::Base.
Kaare Rasmussen <kaare@cpan.org>.
Copyright (C) 2009,2014, Kaare Rasmussen
This module is free software; you can redistribute it or modify it under the same terms as Perl itself.
Kaare Rasmussen <kaare at cpan dot net>
This software is copyright (c) 2015 by Kaare Rasmussen.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Job::Machine, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Job::Machine
CPAN shell
perl -MCPAN -e shell install Job::Machine
For more information on module installation, please visit the detailed CPAN module installation guide.