App-Basis-Queue

 view release on metacpan or  search on metacpan

bin/qtask  view on Meta::CPAN

#!/usr/bin/env perl
# PODNAME: qtask 'app basis queue' script to add things to task queues
# ABSTRACT: add things to queues, using App::Basis::Queue

=head1 SYNOPSIS

    # add a message to the work task queue
    > qtask -q work --type=task "process /some/file/path"

    # process an item in a task queue, exit status will determin if it is processed
    # the queue message is passed to the exec command in quotes
    # obviously there are security concerns around doing this, clean your inputs!
    > qtask -q work --type=task --exec "/command/to/run"

    # peek at work items in a task queue, --type=task is default for a peek
    > qtask --peek --count=10 -q work

    # add to queue from output of another process
    # '-' says read from STDIN
    some_script | qtask -q work -

    # to get full help use
    > qtask --help

=head1 DESCRIPTION

Add am item to a queue or process an item from a queue

config file is in ~/.abq

    queue:
      dsn: dbi:SQLite:/tmp/abq.sqlite
      user:
      password:


The queue entry holds information about the queue database that you want to connect to, this is
obviously a perl DBI style connection

=cut

#
# (c) Kevin Mulholland, moodfarm@cpan.org
# this code is released under the Perl Artistic License

# -----------------------------------------------------------------------------

use 5.10.0 ;
use strict ;
use warnings ;
use POSIX qw(strftime) ;
use App::Basis ;
use App::Basis::Config ;
use DBI ;
use App::Basis::Queue ;
use Date::Manip::Date ;
use feature 'say' ;
use Lingua::EN::Inflexion ;
# what about Data::Dumper::GUI or YAML::Tiny::Color
# use Data::Printer ;

# -----------------------------------------------------------------------------

use constant QUEUE_CONFIG => "$ENV{HOME}/.abq" ;
use constant FIVE_DAYS    => 5 * 24 * 3600 ;
use constant PEEK_DEFAULT => 10 ;
use constant EXEC_DEFAULT => 1 ;


# -----------------------------------------------------------------------------
# lets do the testing stuff with private variables
{
    my $testing = 0 ;
    sub set_testing
    {
        $testing = 1 ;
    }
    sub is_testing
    {
        $testing ;
    }

}

# -----------------------------------------------------------------------------

my $program = get_program() ;

# -----------------------------------------------------------------------------
# always create the datetime strings the same way
sub std_datetime
{
    my ($secs) = @_ ;
    $secs ||= time() ;
    return strftime( "%Y-%m-%d %H:%M:%S UTC", gmtime($secs) ) ;
}

# -----------------------------------------------------------------------------
# convert something like a datetime string or an epoch value into a standardised
# datetime string and epoch value

sub parse_datetime
{
    my ($datetime) = @_ ;
    state $date = Date::Manip::Date->new() ;
    my @ret ;

    if ( !$datetime ) {
        return wantarray ? ( undef, undef ) : undef ;
    } elsif ( $datetime =~ /^\d+$/ ) {
        # assume anything less than five days is a time into the future
        $datetime += time() if ( $datetime <= FIVE_DAYS ) ;
        @ret = ( std_datetime($datetime), $datetime ) ;
    } else {
        # so parse will parse in locale time not as UTC
        $date->parse($datetime) ;
        {
            # if we get a warning about converting the date to a day, there
            # must be a problem with parsing the input date string



( run in 0.694 second using v1.01-cache-2.11-cpan-39bf76dae61 )