Gearman-Driver
view release on metacpan or search on metacpan
sub scale_image : Job {
my ( $self, $job, $workload ) = @_;
# do something
}
This method will be registered as job function with gearmand, verify it
by doing:
plu@mbp ~$ telnet localhost 4730
Trying ::1...
Connected to localhost.
Escape character is '^]'.
status
My::Workers::ONE::scale_image 0 0 1
.
^]
telnet> Connection closed.
If you dont like to use the full package name you can also specify a
custom prefix:
package My::Workers::ONE;
sub prefix { 'foo_bar_' }
sub scale_image : Job {
my ( $self, $job, $workload ) = @_;
# do something
}
This would register 'foo_bar_scale_image' with gearmand.
See also: prefix
ATTRIBUTES
See also "ATTRIBUTES" in Gearman::Driver::Loader.
server
A list of Gearman servers the workers should connect to. The format for
the server list is: "host[:port][,host[:port]]"
See also: Gearman::XS
* default: "localhost:4730"
* isa: "Str"
console_port
Gearman::Driver has a telnet management console, see also:
Gearman::Driver::Console
* default: 47300
* isa: "Int"
Set this to 0 to disable management console at all.
interval
Each n seconds Net::Telnet::Gearman is used in Gearman::Driver::Observer
to check status of free/running/busy workers on gearmand. This is used
to fork more workers depending on the queue size and the
MinProcesses/MaxProcesses attribute of the job method. See also:
Gearman::Driver::Worker
* default: 5
* isa: "Int"
max_idle_time
Whenever Gearman::Driver::Observer notices that there are more processes
running than actually necessary (depending on min_processes and
max_processes setting) it will kill them. By default this happens
immediately. If you change this value to 300, a process which is not
necessary is killed after 300 seconds.
Please remember that this also depends on what value you set "interval"
to. The max_idle_time is only checked each n seconds where n is
"interval". Besides that it makes only sense when you have workers where
"MinProcesses" in Gearman::Driver::Worker is set to 0.
* default: 0
* isa: "Int"
logfile
Path to logfile.
* isa: "Str"
* default: "gearman_driver.log"
loglayout
See also Log::Log4perl.
* isa: "Str"
* default: "[%d] %p %m%n"
loglevel
See also Log::Log4perl.
* isa: "Str"
* default: "INFO"
unknown_job_callback
Whenever Gearman::Driver::Observer sees a job that isnt handled it will
call this CodeRef, passing following arguments:
* $driver
* $status
my $driver = Gearman::Driver->new(
namespaces => [qw(My::Workers)],
unknown_job_callback => sub {
my ( $driver, $status ) = @_;
# notify nagios here for example
}
);
$status might look like:
$VAR1 = {
'busy' => 0,
'free' => 0,
'name' => 'GDExamples::Convert::unknown_job',
'queue' => 6,
'running' => 0
};
worker_options
You can pass runtime options to the worker module, these will merged
with 'GLOBAL' and pass to the worker constructor. ( worker options
override globals )
* default: "{}"
* isa: "HashRef"
Example:
my $driver = Gearman::Driver->new(
namespaces => [qw(My::Workers)],
worker_options => {
'GLOBAL' => {
'config' => $config,
},
'My::Workers::MysqlPing' => {
'dsn' => 'DBI:mysql:database=test;host=localhost;mysql_auto_reconnect=1;mysql_enable_utf8=1;mysql_server_prepare=1;',
},
'My::Workers::ImageThumbnail' => {
'default_format' => 'jpeg',
'default_size => ' 133 x 100 ',
}
}
);
You should define these in a runtime config (See also "configfile"),
might be:
---
worker_options:
'My::App::Worker::MysqlPing':
'dsn': 'DBI:mysql:database=test;host=localhost;mysql_auto_reconnect=1;mysql_enable_utf8=1;mysql_server_prepare=1;'
'user': 'root'
'password:': ''
'My::App::Worker::ImageThumbnail':
'default_format': 'jpeg'
'default_size': '133x100'
Job runtime attributes
You can override a job attribute by its name here. This help to tuning
job some runtime-related options (like max_processes, min_processes)
handy. You just change the options in a config file, no need to modify
the worker code anymore.
Currently only 'max_processes', 'min_processes' make sense. The hash key
is "worker_module::job_key", job_key is ProcessGroup attribute or job
method name.
#in your config file: /etc/gearman-driver.yml (YAML)
---
job_runtime_attributes:
'My::App::Worker::job1':
( run in 1.865 second using v1.01-cache-2.11-cpan-fe3c2283af0 )