Gearman-Driver
view release on metacpan or search on metacpan
lib/Gearman/Driver.pm view on Meta::CPAN
it will call this CodeRef, passing following arguments:
=over 4
=item * C<$driver>
=item * C<$status>
=back
my $driver = Gearman::Driver->new(
namespaces => [qw(My::Workers)],
unknown_job_callback => sub {
my ( $driver, $status ) = @_;
# notify nagios here for example
}
);
C<$status> might look like:
$VAR1 = {
'busy' => 0,
'free' => 0,
'name' => 'GDExamples::Convert::unknown_job',
'queue' => 6,
'running' => 0
};
=cut
has 'unknown_job_callback' => (
default => sub {
sub { }
},
is => 'rw',
isa => 'CodeRef',
traits => [qw(NoGetopt)],
);
=head2 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 )
=over 4
=item * default: C<{}>
=item * isa: C<HashRef>
=back
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 L</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'
=cut
has 'worker_options' => (
isa => 'HashRef',
is => 'rw',
default => sub { {} },
traits => [qw(Hash NoGetopt)],
);
=head2 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':
max_processes: 25
min_processes: 2
#job has a ProcessGroup attribute named 'group1'
'My::App::Worker::group1':
max_processes: 10
min_processes: 2
#then run as:
gearman_driver.pl --configfile /etc/gearman_driver.yml
=cut
has 'job_runtime_attributes' => (
isa => 'HashRef',
is => 'rw',
default => sub { {} },
traits => [qw(Hash NoGetopt)],
);
=head2 configfile
Runtime config file path, You can provide a default configfile pathname like so:
has +configfile ( default => '/etc/gearman-driver.yaml' );
You can pass an array of filenames if you want, like:
has +configfile ( default => sub { [ '/etc/gearman-driver.yaml','/opt/my-app/etc/config.yml' ] });
=cut
has '+configfile' => (
documentation => 'Gearman-driver runtime config path',
);
( run in 0.995 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )