Build Status

NAME

cPanel::TaskQueue - FIFO queue of tasks to perform

SYNOPSIS

use cPanel::TaskQueue ();

my $queue = cPanel::TaskQueue->new( { name => 'tasks', state_dir => "/home/$user/.cpanel/queue" } );

$queue->queue_task( "init_quota" );
$queue->queue_task( "edit_quota fred 0" );
$queue->queue_tasks( "init_quota", "edit_quota fred 0" );

# Processing loop
while (1) {
    # if work, process, else sleep
    if ( $queue->has_work_to_do() ) {
        eval { $queue->process_next_task() };
        if ( $@ ) {
            Carp::carp( $@ );
        }
    }
    else {
        # wait for work.
        sleep 300;
    }
}

DESCRIPTION

This module provides an abstraction for a FIFO queue of tasks that may be executed asynchronously. Each command determines whether it runs in the current process or forks a child to do the work in the background.

The TaskQueue has support for limiting the number of background tasks running at one time and for preventing duplicate tasks from being scheduled.

PUBLIC METHODS

QUEUE PROCESSING

QUEUE INFORMATION

CACHE SUPPORT

These methods should not be used directly, they exist to support the cPanel::StateFile interface that persists the queue information to disk.

CLASS METHODS

The class also supports a few methods that apply to the Task Queuing system as a whole. These methods manage the registering of task processing objects.

LOGGER OBJECT

By default, the TaskQueue uses die and warn for all messages during runtime. However, it supports a mechanism that allows you to insert a logging/reporting system in place by providing an object to do the logging for us.

To provide a different method of logging/reporting, supply an object to do the logging as follows when useing the module.

use cPanel::TaskQueue ( '-logger' => $logger );

The supplied object should supply (at least) 4 methods: throw, warn, info, and notify. When needed these methods will be called with the messages to be logged.

This only works once for a given program, so you can't reset the policy in multiple modules and expect it to work.

In addition to setting a global logger, a new logger object can be supplied when creating a specific TaskQueue object.

See cPanel::TaskQueue::Cookbook for examples.

DIAGNOSTICS

The following messages can be reported by this module:

DEPENDENCIES

YAML::Syck, POSIX

cPanel::TaskQueue::Processor, cPanel::TaskQueue::Task, cPanel::StateFile

INCOMPATIBILITIES

None reported.

BUGS AND LIMITATIONS

In spite of the locking that's used to prevent multiple concurrent writes or reads being combined with writes, it is sometimes possible for a state file to become corrupt or improperly emptied if many processes are attempting to update it at the same time. There is most likely still a race condition that's exposed under heavy load.

SEE ALSO

cPanel::TaskQueue::Processor, cPanel::TaskQueue::Task, and cPanel::StateFile

LICENCE AND COPYRIGHT

Copyright (c) 2014, cPanel, Inc. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.