App-Koyomi
view release on metacpan or search on metacpan
lib/App/Koyomi/Job.pm view on Meta::CPAN
package App::Koyomi::Job;
use strict;
use warnings;
use 5.010_001;
use Class::Accessor::Lite (
ro => [qw/ctx times/],
);
use DateTime;
use IPC::Cmd;
use Log::Minimal env_debug => 'KOYOMI_LOG_DEBUG';
use Smart::Args;
use App::Koyomi::Semaphore;
use version; our $VERSION = 'v0.6.1';
our @JOB_FIELDS = qw/id user command memo created_on updated_at/;
our @TIME_FIELDS = qw/id job_id year month day hour minute weekday created_on updated_at/;
{
no strict 'refs';
for my $field (@JOB_FIELDS) {
*{ __PACKAGE__ . '::' . $field } = sub {
my $self = shift;
$self->{_data}->$field;
};
}
}
sub new {
my $class = shift;
return bless +{}, $class;
}
sub get_jobs {
args(
my $class,
my $ctx => 'App::Koyomi::Context',
);
my @data = $ctx->datasource_job->gets(ctx => $ctx);
my @jobs;
for my $d (@data) {
my $job = bless +{
ctx => $ctx,
_data => $d,
times => $d->times,
}, $class;
debugf(q/(id,user,command) = (%d,%s,"%s")/, $job->id, $job->user || '<NULL>', $job->command);
push(@jobs, $job);
}
return \@jobs;
}
sub proceed {
my $self = shift;
my $now = shift // $self->ctx->now;
my $header = sprintf(q/%d %d/, $$, $self->id);
unless ($self->_get_lock(now => $now)) {
infof(q/%s Failed to get lock. Quit./, $header);
return;
}
my $user = $self->user || $self->_proc_user;
infof(q/%s USER=%s COMMAND="%s"/, $header, $user, $self->command_to_exec);
my ($ok, $err, undef, $stdout, $stderr) = IPC::Cmd::run(command => $self->command_to_exec);
if ($ok) {
infof(q/%s Succeeded./, $header);
if (scalar(@$stdout)) {
infof(q/%s ===== STDOUT =====/, $header);
infof(q/%s %s/, $header, $_) for @$stdout;
}
if (scalar(@$stderr)) {
infof(q/%s ===== STDERR =====/, $header);
infof(q/%s %s/, $header, $_) for @$stderr;
}
} else {
critf(q/%d Failed!!/, $self->id);
( run in 2.108 seconds using v1.01-cache-2.11-cpan-ceb78f64989 )