App-Basis-Queue

 view release on metacpan or  search on metacpan

bin/qpubsub  view on Meta::CPAN

    > abq -q fred "message to tweet"

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

    # add to a pubsub queue
    > abq --queue=notes --type=pubsub "started a program"

    # listen to all pubsub queue messages forever, messages are writen to stdout
    > abq --queue=notes --type=pubsub --listen

    # take an item from a simple queue
    > abq -q fred --pop

    # 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!
    > abq -q work --type=task --exec "/command/to/run"

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

    to get full help use
    > abq --help

=head1 DESCRIPTION

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

config file is in ~/.abq

    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 FIVE_DAYS    => 5 * 24 * 3600 ;
use constant PEEK_DEFAULT => 10 ;
use constant EXEC_DEFAULT => 1 ;


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

use constant QUEUE_CONFIG => "$ENV{HOME}/.abq" ;

my @queue_types = qw(task simple pubsub) ;

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

}

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

my $program = get_program() ;

# URL parsing from https://metacpan.org/pod/URI
my $valid_url
    = "(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?" ;

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



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