| 1 | package Dancer2::Plugin::JobScheduler::Client::TheSchwartz; | ||||||||||||||||||||
| 2 | ## no critic (ControlStructures::ProhibitPostfixControls) | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | # ABSTRACT: A front to the client of the job scheduler or other object via which the jobs are submitted | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 3 | our $VERSION = '0.007'; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =pod | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | =encoding utf8 | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | =for Pod::Coverage config submit_job list_jobs | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | Dancer2::Plugin::JobScheduler::Client::TheSchwartz - Client connector to TheSchwartz | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | Internal class. | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | Not to be used separately. Please see L<Dancer2::Plugin::JobScheduler>. | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =cut | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 3 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 30 | 3 | ▶ | use English '-no_match_vars'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | 3 | ▶ | use Module::Load; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | 3 | ▶ | use Const::Fast; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 3 | ▶ | use Log::Any qw( $log ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 3 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | 3 | ▶ | use TheSchwartz::JobScheduler; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 3 | ▶ | use TheSchwartz::JobScheduler::Job; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 3 | const my $DEFAULT_HANDLE_UNIQKEY => 'no_check'; | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | =head1 CONFIGURATION | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | The configuration of Dancer2::Plugin::JobScheduler::Client::TheSchwartz | ||||||||||||||||||||
| 45 | requires only the knowledge of how to connect with | ||||||||||||||||||||
| 46 | its database backends. TheSchwartz can use simultaneously | ||||||||||||||||||||
| 47 | several databases as backends. When inserting a new task, TheSchwartz | ||||||||||||||||||||
| 48 | loops over all available databases until it finds one | ||||||||||||||||||||
| 49 | that it can connect to and inserts the task there. | ||||||||||||||||||||
| 50 | TheSchwartz client does not maintain its own database handles. | ||||||||||||||||||||
| 51 | Instead, it requires the calling program to give a | ||||||||||||||||||||
| 52 | subroutine pointer or the name and method of a class | ||||||||||||||||||||
| 53 | which can provide the handle. | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | In a long running process, such as a web service, | ||||||||||||||||||||
| 56 | the database handle can become invalid. Database can | ||||||||||||||||||||
| 57 | close the handle if it stays unused a long period of time. | ||||||||||||||||||||
| 58 | The database handle has to be recreated if that happens. | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | If callback is a subroutine pointer, | ||||||||||||||||||||
| 61 | then Dancer2::Plugin::JobScheduler will call the given | ||||||||||||||||||||
| 62 | pointer and give one argument: the name of the database | ||||||||||||||||||||
| 63 | it wants to reach. | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | This example creates two databases and uses a locally defined | ||||||||||||||||||||
| 66 | subroutine to get the database handle: | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | use Database::Temp; | ||||||||||||||||||||
| 69 | use DBI; | ||||||||||||||||||||
| 70 | my %test_dbs = ( | ||||||||||||||||||||
| 71 | theschwartz_db1 => Database::Temp->new( driver => 'SQLite', ); | ||||||||||||||||||||
| 72 | theschwartz_db2 => Database::Temp->new( driver => 'SQLite', ); | ||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||
| 74 | my $get_dbh = sub { | ||||||||||||||||||||
| 75 | my ($id) = @_; | ||||||||||||||||||||
| 76 | return DBI->connect( $test_dbs{ $id }->connection_info ); | ||||||||||||||||||||
| 77 | }; | ||||||||||||||||||||
| 78 | my %plugin_config = ( | ||||||||||||||||||||
| 79 | default => 'theschwartz', | ||||||||||||||||||||
| 80 | schedulers => { | ||||||||||||||||||||
| 81 | theschwartz => { | ||||||||||||||||||||
| 82 | package => 'TheSchwartz', | ||||||||||||||||||||
| 83 | parameters => { | ||||||||||||||||||||
| 84 | dbh_callback => $get_dbh, | ||||||||||||||||||||
| 85 | databases => [ | ||||||||||||||||||||
| 86 | { | ||||||||||||||||||||
| 87 | id => 'theschwartz_db1', | ||||||||||||||||||||
| 88 | prefix => q{}, | ||||||||||||||||||||
| 89 | }, | ||||||||||||||||||||
| 90 | ] | ||||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | This example uses the Perl package L<Database::ManagedHandle> | ||||||||||||||||||||
| 97 | to provide an open database handle. | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | my %plugin_config = ( | ||||||||||||||||||||
| 100 | default => 'theschwartz', | ||||||||||||||||||||
| 101 | schedulers => { | ||||||||||||||||||||
| 102 | theschwartz => { | ||||||||||||||||||||
| 103 | package => 'TheSchwartz', | ||||||||||||||||||||
| 104 | parameters => { | ||||||||||||||||||||
| 105 | dbh_callback => 'Database::ManagedHandle->instance->dbh', | ||||||||||||||||||||
| 106 | databases => [ | ||||||||||||||||||||
| 107 | { | ||||||||||||||||||||
| 108 | id => 'theschwartz_db1', | ||||||||||||||||||||
| 109 | prefix => q{}, | ||||||||||||||||||||
| 110 | }, | ||||||||||||||||||||
| 111 | ] | ||||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||
| 116 | my $get_dbh = sub { | ||||||||||||||||||||
| 117 | my ($id) = @_; | ||||||||||||||||||||
| 118 | return DBI->connect( $test_dbs{ $id }->connection_info ); | ||||||||||||||||||||
| 119 | }; | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | Please see L<Dancer2::Plugin::JobScheduler> on how to attach this | ||||||||||||||||||||
| 122 | configuration to the plugin's configuration. | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =cut | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | has config => ( | ||||||||||||||||||||
| 127 | is => 'ro', | ||||||||||||||||||||
| 128 | 3 | ▶ | isa => sub { croak if( ref $_[0] ne 'HASH' ) }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 3 | required => 1, | |||||||||||||||||||
| 130 | ); | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 3 | ▶ | sub _verify_configuration { | ||||||||||||||||||
|
Subroutine
_verify_configuration: called CC=5 SCAR=20.9
| |||||||||||||||||||||
| 133 | 3 | my ($self) = @_; | |||||||||||||||||||
| 134 | 3 | ▶ | if( ! $self->config->{'dbh_callback'} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 135 | 0 | my $e = 'Invalid config. Must define dbh_callback.'; | |||||||||||||||||||
| 136 | 0 | $log->errorf( $e ); | |||||||||||||||||||
| 137 | 0 | croak $e; | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | 3 | ▶ | if( $self->config->{'databases'} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 140 | 3 | my $databases = $self->config->{'databases'}; | |||||||||||||||||||
| 141 | 3 | foreach my $key (keys %{ $databases }) { | |||||||||||||||||||
| 142 | 3 | my $database = $databases->{ $key }; | |||||||||||||||||||
| 143 | 3 | ▶ | if( $database->{'dbh_callback'} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 144 | 0 | my $e = q{Invalid config. } | |||||||||||||||||||
| 145 | . q{databases->%s has item dbh_callback; database specific callbacks not supported}; | ||||||||||||||||||||
| 146 | 0 | $log->errorf( $e, $key); | |||||||||||||||||||
| 147 | 0 | croak sprintf $e, $key; | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | } else { | ||||||||||||||||||||
| 151 | 0 | my $e = 'Invalid config. Must define databases.'; | |||||||||||||||||||
| 152 | 0 | $log->errorf( $e ); | |||||||||||||||||||
| 153 | 0 | croak $e; | |||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | 3 | return; | |||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 3 | has _client => ( | |||||||||||||||||||
| 159 | is => 'lazy', | ||||||||||||||||||||
| 160 | ); | ||||||||||||||||||||
| 161 | 3 | ▶ | sub _build__client { ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||||||||||
|
Subroutine
_build__client: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 162 | 3 | my $self = shift; | |||||||||||||||||||
| 163 | 3 | $self->_verify_configuration(); | |||||||||||||||||||
| 164 | 3 | my $c = $self->config; | |||||||||||||||||||
| 165 | 3 | $log->debugf( 'config: %s', $c ); | |||||||||||||||||||
| 166 | 3 | ▶ | my $handle_uniqkey = $c->{'handle_uniqkey'} // $DEFAULT_HANDLE_UNIQKEY; | ||||||||||||||||||
|
Condition66%2/3
$c->{"handle_uniqkey"} // $DEFAULT_HANDLE_UNIQKEY
MC/DC0%0/2
$c->{"handle_uniqkey"} // $DEFAULT_HANDLE_UNIQKEY
$c->{"handle_uniqkey"}
$DEFAULT_HANDLE_UNIQKEY
Truth table66%2/3
A // B
A= $c->{"handle_uniqkey"}B= $DEFAULT_HANDLE_UNIQKEY
| |||||||||||||||||||||
| 167 | my $client = TheSchwartz::JobScheduler->new( | ||||||||||||||||||||
| 168 | 3 | databases => $c->{'databases'}, | |||||||||||||||||||
| 169 | dbh_callback => 'Database::ManagedHandle->instance->dbh', | ||||||||||||||||||||
| 170 | opts => { | ||||||||||||||||||||
| 171 | handle_uniqkey => $handle_uniqkey, | ||||||||||||||||||||
| 172 | }, | ||||||||||||||||||||
| 173 | ); | ||||||||||||||||||||
| 174 | 3 | return $client; | |||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 9 | ▶ | sub submit_job { | ||||||||||||||||||
|
Subroutine
submit_job: called CC=8 SCAR=21.4
Pod
undocumented
| |||||||||||||||||||||
| 178 | 9 | my ($self, $job, $opts) = @_; | |||||||||||||||||||
| 179 | 9 | $log->debugf('submit_job: %s, %s', $job, $opts); | |||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 9 | ▶ | croak 'No task name' if( ! $job->{'task'} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 182 | 9 | my $j = TheSchwartz::JobScheduler::Job->new; | |||||||||||||||||||
| 183 | 9 | $j->funcname( $job->{'task'} ); | |||||||||||||||||||
| 184 | 9 | ▶ | $j->arg( $job->{'args'} ) if $job->{'args'}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 185 | 9 | ▶ | $j->uniqkey( $job->{'opts'}->{'unique_key'} ) if $job->{'opts'}->{'unique_key'}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 186 | 9 | ▶ | $j->uniqkey( $job->{'opts'}->{'uniqkey'} ) if $job->{'opts'}->{'uniqkey'}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 187 | 9 | ▶ | $j->run_after( $job->{'opts'}->{'run_after'} ) if $job->{'opts'}->{'run_after'}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 9 | my %args = ( job => $j, ); | |||||||||||||||||||
| 190 | 9 | ▶ | if( $opts->{'dbh_callback'} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 191 | 4 | $args{'dbh_callback'} = $opts->{'dbh_callback'}; | |||||||||||||||||||
| 192 | } | ||||||||||||||||||||
| 193 | 9 | my $job_id = $self->_client->insert( %args ); | |||||||||||||||||||
| 194 | 9 | $log->debugf( 'job_id: %s', $job_id ); | |||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | 9 | ▶ | if( $job_id ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 197 | return ( | ||||||||||||||||||||
| 198 | 9 | success => 1, | |||||||||||||||||||
| 199 | status => 'OK', | ||||||||||||||||||||
| 200 | error => undef, | ||||||||||||||||||||
| 201 | id => $job_id, | ||||||||||||||||||||
| 202 | ); | ||||||||||||||||||||
| 203 | } else { | ||||||||||||||||||||
| 204 | return ( | ||||||||||||||||||||
| 205 | 0 | success => 0, | |||||||||||||||||||
| 206 | status => 'FAIL', | ||||||||||||||||||||
| 207 | error => undef, | ||||||||||||||||||||
| 208 | ); | ||||||||||||||||||||
| 209 | } | ||||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | 6 | ▶ | sub list_jobs { | ||||||||||||||||||
|
Subroutine
list_jobs: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||
| 213 | 6 | my ($self, $search_params, $opts) = @_; | |||||||||||||||||||
| 214 | 6 | $log->debugf('list_jobs(%s, %s)', $search_params, $opts); | |||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | 6 | ▶ | croak 'No task name', if( ! $search_params->{'task'} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 217 | 6 | $search_params->{'funcname'} = delete $search_params->{'task'}; | |||||||||||||||||||
| 218 | 6 | my %args = ( search_params => $search_params, ); | |||||||||||||||||||
| 219 | 6 | ▶ | if( $opts->{'dbh_callback'} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 220 | 2 | $args{'dbh_callback'} = $opts->{'dbh_callback'}; | |||||||||||||||||||
| 221 | } | ||||||||||||||||||||
| 222 | 6 | my @jobs = $self->_client->list_jobs( %args, ); | |||||||||||||||||||
| 223 | 6 | $log->debugf('list_jobs(): jobs: %s', \@jobs); | |||||||||||||||||||
| 224 | 6 | my @r_jobs; | |||||||||||||||||||
| 225 | 6 | foreach my $job (@jobs) { | |||||||||||||||||||
| 226 | 3 | my %opts; | |||||||||||||||||||
| 227 | 3 | ▶ | $opts{'unique_key'} = $job->uniqkey if $job->uniqkey; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 228 | push @r_jobs, { | ||||||||||||||||||||
| 229 | 3 | task => $search_params->{'funcname'}, | |||||||||||||||||||
| 230 | args => $job->arg, | ||||||||||||||||||||
| 231 | opts => \%opts, | ||||||||||||||||||||
| 232 | }; | ||||||||||||||||||||
| 233 | } | ||||||||||||||||||||
| 234 | 6 | my %r = ( | |||||||||||||||||||
| 235 | success => 1, | ||||||||||||||||||||
| 236 | status => 'OK', | ||||||||||||||||||||
| 237 | error => undef, | ||||||||||||||||||||
| 238 | jobs => \@r_jobs, | ||||||||||||||||||||
| 239 | ); | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | 6 | return %r; | |||||||||||||||||||
| 242 | } | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | =pod | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | =head1 ADOPTION | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | If you're interested in adopting this module, and the author/maintainer | ||||||||||||||||||||
| 249 | appears to be no longer active, please consult the PAUSE module | ||||||||||||||||||||
| 250 | adoption process documented at L<https://github.com/Perl-Toolchain-Gang/pause/blob/master/doc/takeover-policy.md>. | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | The PAUSE admins (modules@perl.org) may grant co-maintainer or | ||||||||||||||||||||
| 253 | primary-maintainer permissions to a suitable adopter if: | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | =over 4 | ||||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | =item * | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | There has been no release for a year or more, AND | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | =item * | ||||||||||||||||||||
| 262 | |||||||||||||||||||||
| 263 | There are outstanding issues, pull requests, or bug reports that would | ||||||||||||||||||||
| 264 | benefit from attention, AND | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | =item * | ||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | Reasonable attempts to contact me have failed (CPAN email address, | ||||||||||||||||||||
| 269 | GitHub issues on the project repository, and any other channels listed | ||||||||||||||||||||
| 270 | in this distribution) over a period of at least one month, AND | ||||||||||||||||||||
| 271 | |||||||||||||||||||||
| 272 | =item * | ||||||||||||||||||||
| 273 | |||||||||||||||||||||
| 274 | The prospective adopter intends to make changes that benefit users of | ||||||||||||||||||||
| 275 | the module. | ||||||||||||||||||||
| 276 | |||||||||||||||||||||
| 277 | =back | ||||||||||||||||||||
| 278 | |||||||||||||||||||||
| 279 | =for stopwords maintainership | ||||||||||||||||||||
| 280 | |||||||||||||||||||||
| 281 | In the event of my death or permanent incapacity, my heirs are not | ||||||||||||||||||||
| 282 | obligated to maintain these modules, and I explicitly authorize the | ||||||||||||||||||||
| 283 | PAUSE admins to transfer maintainership without further consultation | ||||||||||||||||||||
| 284 | once the conditions above are met. | ||||||||||||||||||||
| 285 | |||||||||||||||||||||
| 286 | =head1 AUTHOR | ||||||||||||||||||||
| 287 | |||||||||||||||||||||
| 288 | Mikko Koivunalho <mikkoi@cpan.org> | ||||||||||||||||||||
| 289 | |||||||||||||||||||||
| 290 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 291 | |||||||||||||||||||||
| 292 | This software is copyright (c) 2026 by Mikko Koivunalho. | ||||||||||||||||||||
| 293 | |||||||||||||||||||||
| 294 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 295 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 296 | |||||||||||||||||||||
| 297 | =cut | ||||||||||||||||||||
| 298 | |||||||||||||||||||||
| 299 | 3 | 1; | |||||||||||||||||||