Dancer2-Plugin-JobScheduler

 view release on metacpan or  search on metacpan

lib/Dancer2/Plugin/JobScheduler/Client/TheSchwartz.pm  view on Meta::CPAN

package Dancer2::Plugin::JobScheduler::Client::TheSchwartz;
## no critic (ControlStructures::ProhibitPostfixControls)

use strict;
use warnings;

# ABSTRACT: A front to the client of the job scheduler or other object via which the jobs are submitted

our $VERSION = '0.006'; # VERSION: generated by DZP::OurPkgVersion

use Carp;
use English '-no_match_vars';
use Module::Load;
use Const::Fast;

use Log::Any qw( $log ), hooks => { build_context => [ \&_build_context, ], };
use Log::Any::Adapter::Util;

sub _build_context {

    # my ($level, $category, $data) = @_;
    my %ctx;
    my @caller = Log::Any::Adapter::Util::get_correct_caller();
    $ctx{file} = $caller[1];
    $ctx{line} = $caller[2];
    return %ctx;
}

use Moo;
use TheSchwartz::JobScheduler;
use TheSchwartz::JobScheduler::Job;

const my $DEFAULT_HANDLE_UNIQKEY => 'no_check';

has config => (
    is       => 'ro',
    isa      => sub { croak if ( ref $_[0] ne 'HASH' ) },
    required => 1,
);

sub _verify_configuration {
    my ($self) = @_;
    if ( !$self->config->{'dbh_callback'} ) {
        my $e = 'Invalid config. Must define dbh_callback.';
        $log->errorf($e);
        croak $e;
    }
    if ( $self->config->{'databases'} ) {
        my $databases = $self->config->{'databases'};
        foreach my $key ( keys %{$databases} ) {
            my $database = $databases->{$key};
            if ( $database->{'dbh_callback'} ) {
                my $e = q{Invalid config. } . q{databases->%s has item dbh_callback; database specific callbacks not supported};
                $log->errorf( $e, $key );
                croak sprintf $e, $key;
            }
        }
    }
    else {
        my $e = 'Invalid config. Must define databases.';
        $log->errorf($e);
        croak $e;
    }
    return;
}

has _client => ( is => 'lazy', );

sub _build__client {    ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
    my $self = shift;
    $self->_verify_configuration();
    my $c = $self->config;
    $log->debugf( 'config: %s', $c );
    my $handle_uniqkey = $c->{'handle_uniqkey'} // $DEFAULT_HANDLE_UNIQKEY;
    my $client         = TheSchwartz::JobScheduler->new(
        databases    => $c->{'databases'},
        dbh_callback => 'Database::ManagedHandle->instance->dbh',
        opts         => {
            handle_uniqkey => $handle_uniqkey,
        },
    );
    return $client;
}

sub submit_job {
    my ( $self, $job, $opts ) = @_;
    $log->debugf( 'submit_job: %s, %s', $job, $opts );

    croak 'No task name' if ( !$job->{'task'} );
    my $j = TheSchwartz::JobScheduler::Job->new;
    $j->funcname( $job->{'task'} );
    $j->arg( $job->{'args'} )                      if $job->{'args'};
    $j->uniqkey( $job->{'opts'}->{'unique_key'} )  if $job->{'opts'}->{'unique_key'};
    $j->uniqkey( $job->{'opts'}->{'uniqkey'} )     if $job->{'opts'}->{'uniqkey'};
    $j->run_after( $job->{'opts'}->{'run_after'} ) if $job->{'opts'}->{'run_after'};

    my %args = ( job => $j, );
    if ( $opts->{'dbh_callback'} ) {
        $args{'dbh_callback'} = $opts->{'dbh_callback'};
    }
    my $job_id = $self->_client->insert(%args);
    $log->debugf( 'job_id: %s', $job_id );

    if ($job_id) {
        return (
            success => 1,
            status  => 'OK',
            error   => undef,
            id      => $job_id,
        );
    }
    else {
        return (



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