NAME

Maplat::Worker - the Maplat Worker

SYNOPSIS

The worker module is the one responsible for loading all actual working modules, dispatches calls and callbacks/hooks.

my $config = XMLin($configfile,
                    ForceArray => [ 'module', 'directory' ],);

$APPNAME = $config->{appname};
print "Changing application name to '$APPNAME'\n\n";

# set required values to default if they don't exist
if(!defined($config->{mincycletime})) {
    $config->{mincycletime} = 10;
}


my @modlist = @{$config->{module}};

$worker->startconfig();

foreach my $module (@modlist) {
    $worker->configure($module->{modname}, $module->{pm}, %{$module->{options}});
}

$worker->endconfig();

# main loop
$cycleStartTime = time;
while(1) {
    my $workCount = $worker->run();

    my $tmptime = time;
    my $workTime = $tmptime - $cycleStartTime;
    my $sleeptime = $config->{mincycletime} - $workTime;
    if($sleeptime > 0) {
        print "** Fast cycle ($workTime sec), sleeping for $sleeptime sec **\n";
        sleep($sleeptime);
        print "** Wake-up call **\n";
    } else {
        print "** Cycle time $workTime sec **\n";
    }
    $cycleStartTime = time;
}

DESCRIPTION

This worker is "the root of all evil". It loads and configures the working modules and dispatches callbacks/hooks.

Configuration and Startup

Configuration is done in stages from the main application, after new(), the first thing to call is startconfig() to prepare the worker for module configuration.

After that, for each module to load, configure() is called, during which the module is loaded and configured.

Next thing is to call endconfig(), which notifies the worker that all required modules are loaded (the worker then automatically calls reload() to load all cached data).

Running is done in a while loop or similar calling run(). As most workers dont have to react in the millisecond range, it's a good idea to have some code in place to try to do cyclic calls in a configurable cycle time. See also the synopsis and the example included in the tarball.

new

Create a new instance of Maplat::Worker.

startconfig

Prepare the worker instance for module configuration.

configure

Configure a worker module.

endconfig

Finish up configuration and prepare for run().

add_worker

Add a worker callback. Called by the various worker modules.

run

Do a single run of all registered worker callbacks.

SEE ALSO

Maplat::Web Maplat::Worker::BaseModule

Please also take a look in the example provided in the tarball available on CPAN.

AUTHOR

Rene Schickbauer, <rene.schickbauer@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2008-2010 by Rene Schickbauer

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.