NAME
Gearman::Driver::Loader - Loads worker classes
DESCRIPTION
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.
ATTRIBUTES
namespaces
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".
isa:
ArrayRef
required:
True
wanted
isa:
CodeRef
required:
False
This 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
:
My::Project::Web
My::Project::Web::Controller::Root
My::Project::Web::Controller::Admin
My::Project::Web::Controller::User
My::Project::Web::Model::DBIC
My::Project::Worker::ScaleImage
My::Project::Worker::RemoveUser
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:
My::Project::Worker::ScaleImage
My::Project::Worker::RemoveUser
lib
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
isa:
Str
modules
Every worker module loaded by Module::Find will be added to this list. There are also two methods: get_modules and has_modules.
isa:
ArrayRef
readonly:
True
METHODS
get_namespaces
Returns a sorted list of namespaces.
get_modules
Returns a sorted list of modules.
has_modules
Returns the count of modules.
is_valid_worker_subclass
Parameters: $package
Checks if the given $package
is a valid subclass of Gearman::Driver::Worker.
has_job_method
Parameters: $package
Checks if the given $package
has a valid job method.
load_namespaces
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.
AUTHOR
See Gearman::Driver.
COPYRIGHT AND LICENSE
See Gearman::Driver.