NAME
Job::Machine::Worker
VERSION
version 0.26
DESCRIPTION
Write a script to instantiate your class and start the receive loop:
my $worker = My::Worker->new(dbh => $dbh, queue => 'job.task');
$worker->receive;
Write the Worker Class
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
};
_init_chores
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.
add_chore
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.
NAME
Job::Machine::Worker - Base class for Job Workers
METHODS
Methods to be subclassed
A worker process always needs to subclass the process method with the real functionality.
startup
startup will be called before any tasks are fetched and any processing is done.
Call this method for one-time initializing.
process
Subclassable process method.
E.g.
sub process {
my ($self, $data) = @_;
... process $data
$self->reply({answer => 'Something'});
};
max_runtime
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.
timeout
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
retries
If the default of 3 times isn't suitable, return the number of times a task is retried before failing.
remove_after
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.
keep_running
Worker will wait for next message if this method returns true.
Methods to be used from within the process method
reply
$worker->reply($some_structure);
Reply to a message. Use from within a Worker's process method.
Marks the task as done,
result
Use from within a Worker's process method.
$worker->result($result_data);
Save the result of the task.
Marks the task as done,
error_result
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,
db
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.
id
Get the current task id.
methods not to be disturbed
receive
$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.
SEE ALSO
AUTHOR
Kaare Rasmussen <kaare@cpan.org>.
COPYRIGHT
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.
AUTHOR
Kaare Rasmussen <kaare at cpan dot net>
COPYRIGHT AND LICENSE
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.