| Gearman-Driver documentation | Contained in the Gearman-Driver distribution. |
Gearman::Driver::Loader - Loads worker classes
This module is responsible for loading worker classes and doing the introspection on them (looking for job method attributes etc). All methods and attributes are internally used in Gearman::Driver but this module (implemented as Moose::Role) might be of interest to use outside of Gearman::Driver.
Will be passed to Module::Find findallmod method to load worker
modules. Each one of those modules has to be inherited from
Gearman::Driver::Worker or a subclass of it. It's also possible
to use the full package name to load a single module/file. There is
also a method get_namespaces which
returns a sorted list of all namespaces.
See also: wanted.
ArrayRefTrueCodeRefFalseThis CodeRef will be called on each of the modules found in your namespace. The first and only parameter to this sub is the name of the module. If a true value is returned, the module will be loaded and checked if it's a valid Gearman::Driver::Worker subclass.
Let's say you have a namespace called My::Project:
To avoid every module being loaded and inspected being a
Gearman::Driver::Worker subclass you can use wanted
to only load classes having Worker in the package name:
my $driver = Gearman::Driver->new(
interval => 0,
namespaces => [qw(My::Project)],
wanted => sub {
return 1 if /Worker/;
return 0;
},
);
This would only load:
This is just for convenience to extend @INC from command line
using gearman_driver.pl:
gearman_driver.pl --lib ./lib --lib /custom/lib --namespaces My::Workers
StrEvery worker module loaded by Module::Find will be added to this list. There are also two methods: get_modules and has_modules.
ArrayRefTrueReturns a sorted list of namespaces.
Returns a sorted list of modules.
Returns the count of modules.
Parameters: $package
Checks if the given $package is a valid subclass of
Gearman::Driver::Worker.
Parameters: $package
Checks if the given $package has a valid job method.
Loops over all namespaces and uses findallmod to generate a list of modules to load. It verifies the module is wanted before it's being loaded using Class::MOP::load_class. After loading is_valid_worker_subclass and has_wanted is used to verify it. After all tests have passed the modules are added. So finally the loader is ready and can be queried with get_modules for example.
See Gearman::Driver.
See Gearman::Driver.
| Gearman-Driver documentation | Contained in the Gearman-Driver distribution. |
package Gearman::Driver::Loader; use Moose::Role; use Class::MOP; use Module::Find; use Try::Tiny;
has 'namespaces' => ( default => sub { [] }, documentation => 'Example: --namespaces My::Workers --namespaces My::OtherWorkers', handles => { get_namespaces => 'sort' }, is => 'rw', isa => 'ArrayRef[Str]', required => 0, traits => [qw(Array)], );
has 'wanted' => ( is => 'rw', isa => 'CodeRef', predicate => 'has_wanted', );
has 'lib' => ( default => sub { [] }, documentation => 'Example: --lib ./lib --lib /custom/lib', is => 'rw', isa => 'ArrayRef[Str]', );
has 'modules' => ( default => sub { [] }, handles => { add_module => 'push', get_modules => 'sort', has_modules => 'count', }, is => 'ro', isa => 'ArrayRef[Str]', traits => [qw(Array)], );
sub is_valid_worker_subclass { my ( $self, $package ) = @_; return 0 unless $package; return 0 unless $package->can('meta'); return 0 unless $package->meta->can('linearized_isa'); return 0 unless grep $_ eq 'Gearman::Driver::Worker', $package->meta->linearized_isa; return 1; }
sub has_job_method { my ( $self, $package ) = @_; return 0 unless $package; return 0 unless $package->meta->can('get_nearest_methods_with_attributes'); foreach my $method ( $package->meta->get_nearest_methods_with_attributes ) { next unless grep $_ eq 'Job', @{ $method->attributes }; return 1; } return 0; }
sub load_namespaces { my ($self) = @_; my @modules = (); foreach my $ns ( $self->get_namespaces ) { my @modules_ns = findallmod $ns; # Module::Find::findallmod($ns) does not load $ns itself push @modules_ns, $ns; if ( $self->has_wanted ) { @modules_ns = grep { $self->wanted->($_) } @modules_ns; } push @modules, @modules_ns; } foreach my $module (@modules) { Class::MOP::load_class($module); next unless $self->is_valid_worker_subclass($module); next unless $self->has_job_method($module); $self->add_module($module); } }
no Moose::Role; 1;