
=head1  NAME

cPanel::TaskQueue - FIFO queue of tasks to perform

=head1 VERSION

This document describes cPanel::TaskQueue version 0.605

=head1 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" );

    # 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;
        }
    }

=head1  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.

=head1 PUBLIC METHODS

=over 4

=item cPanel::TaskQueue->new( $hashref )

Creates a new TaskQueue object based on the parameters from the supplied hashref.

=over 4

=item I<state_dir>

This required parameter specifies a directory where the state file should be
written.  This directory is created if it does not exist.

=item I<cache_dir>

I<Deprecated> parameter that has been replaced with I<state_dir>. If no value is
supplied by I<state_dir>, I<cache_dir>'s value will be used.

=item I<name>

This required parameter specifies the name of the queue. This name is used to
construct the name of the state file used to store the queue information.

=item I<state_timeout>

This optional parameter specifies the timeout to use for flocking the state file.
The value is in seconds and defaults to the cPanel::StateFile default value.

=item I<default_timeout>

This optional parameter specifies the default amount of time (in seconds) to wait
for an in-process task to run. The default value is 60 seconds.
    
This timeout may be overridden for a particular command by the
C<cPanel::TaskQueue::Processor> object.

=item I<max_timeout>

This optional parameter specifies the maximum amount of time (in seconds) to wait
for an in-process task to run. The default value is 300 seconds (5 minutes).

This value prevents a given C<cPanel::TaskQueue::Processor> object from setting the timeout
too high.

=item I<max_running>

This optional parameter specifies the maximum number of child processes that the
queue will allow to be running at one time. If this number is reached, all queue
processing blocks until one or more child processes exit. The default value is 2.

=item I<default_child_timeout>

This optional parameter specifies the default amount of time (in seconds) to wait
for a child task to run. The default value is 3600 seconds (1 hour).
    
This timeout may be overridden for a particular command by the
C<cPanel::TaskQueue::Processor> object.

=back

If these parameters are not specified, but a C<TaskQueue> with this I<name> and
I<state_dir> has already been created, the new C<TaskQueue> will use the parameters
that were stored in the file.  This causes all instances of this C<TaskQueue>
to act the same. Providing these parameters also updates all other instances of
this C<TaskQueue> the next time they need to C<synch>.

=item $q->queue_task( $command )

Create a new task from the command and put it at the end of the queue if it meets
certain minimum criteria.

=over 4

=item Command must be legal.

The command type must have been registered with the TaskQueue module.

=item Command must not duplicate a command already in the queue.

Each command type can have it's own definition of duplicate. It can depend on
one or more of the arguments or not.

=back

If the task is successfully queued, a non-empty I<uuid> is returned. This id can
be used to remove the task from the queue at a later time.

If the task was not queued, a false value is returned.

The C<queue_task> method can also be called with a C<cPanel::TaskQueue::Task>
object which will be tested and inserted as usual.

=item $q->unqueue_task( $uuid )

Remove the task associated with the supplied I<uuid> from the queue, if it
has not been processed yet. Returns true on success.

=back

=head2 QUEUE PROCESSING

=over 4

=item $q->has_work_to_do()

Returns a true value if there are any tasks in the queue and we can process them.
This method does not block on child processes that are currently running if we
cannot launch a new task.

=item $q->process_next_task()

This method is called to process another task from the wait queue.

If there are any tasks remaining and we have not reached the limit of tasks we
can process at once, the next is removed from the queue. The task is checked to
make certain we know how to process it, if not it is discarded.  Then it is added
to the processing list and the C<cPanel::TaskQueue::Processor> object for that
command is asked to process it. If we have reached our processing limit, block
until a task can be executed.

If the command is completed by the C<cPanel::TaskQueue::Processor>, the task is
removed from the processing list. If the C<cPanel::TaskQueue::Processor> launched
a child process, the task is left in the processing list.

The method returns true if the task was completed or otherwise removed from the
system. If the task was launched as a child process, the method returns false. The
method will also return true if there is nothing to process.

=item $q->finish_all_processing()

This method does not return until all tasks currently being processed in the
background are completed. It is most useful to call as part of shutdown of the
program that processes the queue. While waiting for processing to complete,
this method does not start any new tasks out of the queue.

=back

=head2 QUEUE INFORMATION

=over 4

=item $q->get_default_child_timeout

Returns the default timeout value for a child process.

=item $q->get_default_timeout

Returns the default timeout value for a task.

=item $q->get_max_running

Returns the maximum number of child processes that can be running at once.

=item $q->get_max_timeout

Returns the maximum timeout value for a task.

=item $q->get_name

Returns the TaskQueue's name.

=item $q->peek_next_task()

Get a copy of the first task descriptor in the queue or C<undef> if the queue is
empty.

Because of the nature of a task queue, there is no guarantee that this task will
remain unscheduled after the method call. That is one reason that a copy is
returned.

=item $q->is_task_queued( $uuid )

Does the specified I<uuid> reference a task in the queue?

Because of the nature of a task queue, the particular I<uuid> tested may be scheduled
for processsing immediately after the test. Therefore, a true answer is not as useful as
it might seem. A false answer does tell us that the item is no longer waiting. 

=item $q->find_task( $uuid )

Returns a copy of the task in the queue with the supplied I<uuid>. Returns
C<undef> if no task with that I<uuid> is found. Because of the nature of the
task queue, the task that is returned may not be in the queue shortly after
return from this method. Another process may have handled it and removed it from
the queue.

However, the returned copy is a faithful representation of the task at the point
in time that it was found.

=item $q->find_command( $command )

Returns a copy of the first command with the supplied I<command> (sans
arguments).  Returns C<undef> if no task with that command name is found.
Because of the nature of the task queue, the task that is returned may not be
in the queue shortly after return from this method. Another process may have
handled it and removed it from the queue.

Remember that C<$command> is just the name of the command, not the whole
command string with arguments.

=item $q->find_commands( $command )

Returns a list of copies of commands with the supplied I<command> (sans
arguments).  Because of the nature of the task queue, the tasks that are
returned may not be in the queue shortly after return from this method. Another
process may have handled one or more tasks and removed then from the queue.

Remember that C<$command> is just the name of the command, not the whole
command string with arguments.

=item $q->how_many_queued()

Gives a count at this particular point in time of the number of items currently
in the queue. Since an item may be removed and processed any time the
C<process_next_task()> method is called, this count may not be correct immediately
after the method returns.

Most useful for the general case of telling if the queue is really full, or mostly
empty.

=item $q->is_task_processing( $uuid )

Does the specified I<uuid> reference a task currently being processed?

Because of the nature of a task queue, the particular I<uuid> tested may be scheduled
for processsing or finish processing immediately after the test. I'm not sure if this
test is actually useful for anything.

=item $q->is_task_deferred( $uuid )

Does the specified I<uuid> reference a task that is currently deferred?

Because of the nature of a task queue, the particular I<uuid> tested may be scheduled
for processsing or finish processing immediately after the test. I'm not sure if this
test is actually useful for anything.

=item $q->how_many_deferred()

Returns a count of the number of tasks currently that are currently in the
deferred state. Since a task can complete at any time, the exact value returned
by this method is not guaranteed for any length of time after the method
returns.

=item $q->how_many_in_process()

Returns a count of the number of items currently being processed. Since a task
can complete at any time, the exact value returned by this method is not
guaranteed for any length of time after the method returns. May be useful to get
a statistical measure of how busy the C<cPanel::TaskQueue> system is.

=item $q->snapshot_task_lists()

Returns a reference to a hash containing copies of the current queues. The value
of I<waiting> is an array reference containing copies of all of the C<Task>s
waiting to execute. The value of I<processing> is an array reference containing
copies of all of the C<Tasks> currently being processed.

Since a task can complete at any time and whatever process handles the queue can
start processing a task at any time, the output of this method may be out of
date as soon as it returns. This method is only really useful for a general idea
of the state of the queue.

=item $q->pause_processing()

Prevent any more tasks from moving from the waiting state into the processing
state. This does not stop any tasks from processing once they begin processing.
If the queue is paused, no more tasks will move from the waitig state to the
processing state.

=item $q->resume_processing()

Allow the queue to resume processing tasks.

=item $q->is_paused()

Returns true if the queue processing has been paused, false otherwise.

=back

=head2 CACHE SUPPORT

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

=over 4

=item $q->load_from_cache( $fh )

This method loads the queue information from the disk state file. It is called
by the C<cPanel::StateFile> object owned by this object.

The user of this class should never need to call this method.

=item $q->save_to_cache( $fh )

This method saves the queue information to the disk state file. It is called by
the C<cPanel::StateFile> object owned by this object.

The user of this class should never need to call this method.

=item $q->throw( $msg )

Log the supplied message and C<die>.

=item $q->warn( $msg )

Log the supplied message as a warning.

=item $q->info( $msg )

Log the supplied message as an informational message.

=back

=head1 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.

=over 4

=item cPanel::TaskQueue->register_task_processor( $cmdname, $processor )

Add a task processing object for the command name given as the first argument.
The second argument must either be a C<cPanel::TaskQueue::Processor>-derived object
or a code reference that will be wrapped in a C<cPanel::TaskQueue::Processor::CodeRef>
object.

=item cPanel::TaskQueue->unregister_task_processor( $cmdname )

Removes the task processing object for the command given as the only argument.

After a call to this method, that particular command can not be queued any more
and any already queued objects will be discarded when the C<cPanel::TaskQueue>
tries to process them.

=back

=head1 LOGGER OBJECT

By default, the C<TaskQueue> uses C<die> and C<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 C<use>ing the module.

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

The supplied object should supply (at least) 4 methods: C<throw>, C<warn>,
C<info>, and C<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 C<TaskQueue> object.

See L<cPanel::TaskQueue::Cookbook> for examples.

=head1 DIAGNOSTICS

The following messages can be reported by this module:

=over 4

=item C<< Missing command in register_task_processor. >>

No command name was given when calling the C<register_task_processor> class
method to register a processing object to handle a command.

=item C<< Missing task processor in register_task_processor. >>

No command processor object was supplied when calling the C<register_task_processor>
class method to register an action to attach to a command.

=item C<< Command '%s' already has a TaskQueue::Processor registered. >>

The supplied command name already has a registered processing object. If you want
to change it, you must first remove the other processor using
C<unregister_task_processor>.

=item C<< Unrecognized task processor object. >>

The second parameter to C<register_task_processor> was not recognized as a
C<TaskQueue::Processor>-derived object or as a C<coderef>.

=item C<< Missing command in unregister_task_processor. >>

No command name string was supplied when calling this method.

=item C<< Command '%s' not registered, ignoring. >>

The supplied argument to C<unregister_task_processor> was not a registered
command name.

=item C<< No caching directory supplied. >>

The required I<state_dir> parameter was missing when constructing the C<TaskQueue>
object. The object was not created.

=item C<< No queue name supplied. >>

The required I<name> parameter was missing when constructing the C<TaskQueue>
object. The object was not created.

=item C<< Not a recognized TaskQueue state file. >>

=item C<< Invalid version of TaskQueue state file. >>

Either the state file is invalid or it is not a TaskQueue state file.

=item C<< Cannot queue an empty command. >>

The command string supplied to C<queue_task> was either C<undef> or empty.

=item C<< Task with 0 retries not queued. >>

The C<Task> supplied to C<queue_task> has a remaining retry count of 0. The task
has been discarded. This is a warning message only.

=item C<< No known processor for '%s'. >>

The specified command has no defined processor. The command has been discarded.

=item C<< Requested command [%s] has invalid arguments. >>

The supplied full command has arguments that are not valid as defined by the
command processor.

=item C<< No Task uuid argument passed to %s. >>

The specified method requires a I<uuid> to specify which task to operate
on. None was supplied.

=item C<< No processor found for '%s'. >>

Either the program inserting tasks into the queue has a different list of commands
than the program processing the queue, or a TaskQueue::Processor was unregistered
after this command was queued.

=item C<< Task '%s' timed out during processing. >>

The supplied command timed out while being executed in-process.

=item C<< Undefined tasks found in the queue, removing... >>

Somehow a task item of C<undef> has appeared in the queue. This should never
happen, so if it does, we remove them.

=back

=head1 DEPENDENCIES

YAML::Syck, POSIX

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

=head1 INCOMPATIBILITIES

None reported.

=head1 BUGS AND LIMITATIONS

none reported.

=head1 SEE ALSO

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

=head1 LICENCE AND COPYRIGHT

Copyright (c) 2013, 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 L<perlartistic>.

=head1 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.
