| 1 | package Minion::Backend::Pg; | ||
| 2 | use Mojo::Base 'Minion::Backend'; | ||
| 3 | |||
| 4 | use Carp qw(croak); | ||
| 5 | use Mojo::File qw(path); | ||
| 6 | use Mojo::IOLoop; | ||
| 7 | use Mojo::Pg 4.0; | ||
| 8 | use Sys::Hostname qw(hostname); | ||
| 9 | |||
| 10 | has 'pg'; | ||
| 11 | |||
| 12 | sub broadcast { | ||
| 13 | my ($self, $command, $args, $ids) = (shift, shift, shift || [], shift || []); | ||
| 14 | return !!$self->pg->db->query( | ||
| 15 | q{UPDATE minion_workers SET inbox = inbox || $1::JSONB WHERE (id = ANY ($2) OR $2 = '{}')}, | ||
| 16 | {json => [[$command, @$args]]}, $ids)->rows; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub dequeue { | ||
| 20 | my ($self, $id, $wait, $options) = @_; | ||
| 21 | |||
| 22 | if ((my $job = $self->_try($id, $options))) { return $job } | ||
| 23 | return undef if Mojo::IOLoop->is_running; | ||
| 24 | |||
| 25 | my $db = $self->pg->db; | ||
| 26 | $db->listen('minion.job')->on(notification => sub { Mojo::IOLoop->stop }); | ||
| 27 | my $timer = Mojo::IOLoop->timer($wait => sub { Mojo::IOLoop->stop }); | ||
| 28 | Mojo::IOLoop->start; | ||
| 29 | $db->unlisten('*') and Mojo::IOLoop->remove($timer); | ||
| 30 | undef $db; | ||
| 31 | |||
| 32 | return $self->_try($id, $options); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub enqueue { | ||
| 36 | my ($self, $task, $args, $options) = (shift, shift, shift || [], shift || {}); | ||
| 37 | |||
| 38 | return $self->pg->db->query( | ||
| 39 | q{INSERT INTO minion_jobs (args, attempts, delayed, expires, lax, notes, parents, priority, queue, task) | ||
| 40 | VALUES ($1, $2, (NOW() + (INTERVAL '1 second' * $3)), | ||
| 41 | CASE WHEN $4::BIGINT IS NOT NULL THEN NOW() + (INTERVAL '1 second' * $4::BIGINT) END, $5, $6, $7, $8, $9, $10) | ||
| 42 | RETURNING id}, {json => $args}, $options->{attempts} // 1, $options->{delay} // 0, $options->{expire}, | ||
| 43 | $options->{lax} ? 1 : 0, {json => $options->{notes} || {}}, $options->{parents} || [], $options->{priority} // 0, | ||
| 44 | $options->{queue} // 'default', $task | ||
| 45 | )->hash->{id}; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub fail_job { shift->_update(1, @_) } | ||
| 49 | sub finish_job { shift->_update(0, @_) } | ||
| 50 | |||
| 51 | sub history { | ||
| 52 | my $self = shift; | ||
| 53 | |||
| 54 | my $daily = $self->pg->db->query( | ||
| 55 | "SELECT EXTRACT(EPOCH FROM ts) AS epoch, COALESCE(failed_jobs, 0) AS failed_jobs, | ||
| 56 | COALESCE(finished_jobs, 0) AS finished_jobs | ||
| 57 | FROM ( | ||
| 58 | SELECT EXTRACT (DAY FROM finished) AS day, EXTRACT(HOUR FROM finished) AS hour, | ||
| 59 | COUNT(*) FILTER (WHERE state = 'failed') AS failed_jobs, | ||
| 60 | COUNT(*) FILTER (WHERE state = 'finished') AS finished_jobs | ||
| 61 | FROM minion_jobs | ||
| 62 | WHERE finished > NOW() - INTERVAL '23 hours' | ||
| 63 | GROUP BY day, hour | ||
| 64 | ) AS j RIGHT OUTER JOIN ( | ||
| 65 | SELECT * | ||
| 66 | FROM GENERATE_SERIES(NOW() - INTERVAL '23 hour', NOW(), '1 hour') AS ts | ||
| 67 | ) AS s ON EXTRACT(HOUR FROM ts) = j.hour AND EXTRACT(DAY FROM ts) = j.day | ||
| 68 | ORDER BY epoch ASC" | ||
| 69 | )->hashes->to_array; | ||
| 70 | |||
| 71 | return {daily => $daily}; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub list_jobs { | ||
| 75 | my ($self, $offset, $limit, $options) = @_; | ||
| 76 | |||
| 77 | my $jobs = $self->pg->db->query( | ||
| 78 | q{SELECT id, args, attempts, | ||
| 79 | ARRAY(SELECT id FROM minion_jobs WHERE parents @> ARRAY[j.id]) AS children, | ||
| 80 | EXTRACT(epoch FROM created) AS created, EXTRACT(EPOCH FROM delayed) AS delayed, | ||
| 81 | EXTRACT(EPOCH FROM expires) AS expires, EXTRACT(EPOCH FROM finished) AS finished, lax, notes, parents, priority, | ||
| 82 | queue, result, EXTRACT(EPOCH FROM retried) AS retried, retries, EXTRACT(EPOCH FROM started) AS started, state, | ||
| 83 | task, EXTRACT(EPOCH FROM now()) AS time, COUNT(*) OVER() AS total, worker | ||
| 84 | FROM minion_jobs AS j | ||
| 85 | WHERE (id < $1 OR $1 IS NULL) AND (id = ANY ($2) OR $2 IS NULL) AND (notes \? ANY ($3) OR $3 IS NULL) | ||
| 86 | AND (queue = ANY ($4) OR $4 IS null) AND (state = ANY ($5) OR $5 IS NULL) AND (task = ANY ($6) OR $6 IS NULL) | ||
| 87 | AND (state != 'inactive' OR expires IS null OR expires > NOW()) | ||
| 88 | ORDER BY id DESC | ||
| 89 | LIMIT $7 OFFSET $8}, @$options{qw(before ids notes queues states tasks)}, $limit, $offset | ||
| 90 | )->expand->hashes->to_array; | ||
| 91 | |||
| 92 | return _total('jobs', $jobs); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub list_locks { | ||
| 96 | my ($self, $offset, $limit, $options) = @_; | ||
| 97 | |||
| 98 | my $locks = $self->pg->db->query( | ||
| 99 | 'SELECT name, EXTRACT(EPOCH FROM expires) AS expires, COUNT(*) OVER() AS total FROM minion_locks | ||
| 100 | WHERE expires > NOW() AND (name = ANY ($1) OR $1 IS NULL) | ||
| 101 | ORDER BY id DESC LIMIT $2 OFFSET $3', $options->{names}, $limit, $offset | ||
| 102 | )->hashes->to_array; | ||
| 103 | return _total('locks', $locks); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub list_workers { | ||
| 107 | my ($self, $offset, $limit, $options) = @_; | ||
| 108 | |||
| 109 | my $workers = $self->pg->db->query( | ||
| 110 | "SELECT id, EXTRACT(EPOCH FROM notified) AS notified, ARRAY( | ||
| 111 | SELECT id FROM minion_jobs WHERE state = 'active' AND worker = minion_workers.id | ||
| 112 | ) AS jobs, host, pid, status, EXTRACT(EPOCH FROM started) AS started, | ||
| 113 | COUNT(*) OVER() AS total | ||
| 114 | FROM minion_workers | ||
| 115 | WHERE (id < \$1 OR \$1 IS NULL) AND (id = ANY (\$2) OR \$2 IS NULL) | ||
| 116 | ORDER BY id DESC LIMIT \$3 OFFSET \$4", @$options{qw(before ids)}, $limit, $offset | ||
| 117 | )->expand->hashes->to_array; | ||
| 118 | return _total('workers', $workers); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub lock { | ||
| 122 | my ($self, $name, $duration, $options) = (shift, shift, shift, shift // {}); | ||
| 123 | return !!$self->pg->db->query('SELECT * FROM minion_lock(?, ?, ?)', $name, $duration, $options->{limit} || 1) | ||
| 124 | ->array->[0]; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub new { | ||
| 128 | my $self = shift->SUPER::new(pg => Mojo::Pg->new(@_)); | ||
| 129 | |||
| 130 | my $db = Mojo::Pg->new(@_)->db; | ||
| 131 | croak 'PostgreSQL 9.5 or later is required' if $db->dbh->{pg_server_version} < 90500; | ||
| 132 | $db->disconnect; | ||
| 133 | |||
| 134 | my $schema = path(__FILE__)->dirname->child('resources', 'migrations', 'pg.sql'); | ||
| 135 | $self->pg->auto_migrate(1)->migrations->name('minion')->from_file($schema); | ||
| 136 | |||
| 137 | return $self; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub note { | ||
| 141 | my ($self, $id, $merge) = @_; | ||
| 142 | return !!$self->pg->db->query('UPDATE minion_jobs SET notes = JSONB_STRIP_NULLS(notes || ?) WHERE id = ?', | ||
| 143 | {json => $merge}, $id)->rows; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub receive { | ||
| 147 | my $array = shift->pg->db->query( | ||
| 148 | "UPDATE minion_workers AS new SET inbox = '[]' | ||
| 149 | FROM (SELECT id, inbox FROM minion_workers WHERE id = ? FOR UPDATE) AS old | ||
| 150 | WHERE new.id = old.id AND old.inbox != '[]' | ||
| 151 | RETURNING old.inbox", shift | ||
| 152 | )->expand->array; | ||
| 153 | return $array ? $array->[0] : []; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub register_worker { | ||
| 157 | my ($self, $id, $options) = (shift, shift, shift || {}); | ||
| 158 | |||
| 159 | return $self->pg->db->query( | ||
| 160 | q{INSERT INTO minion_workers (id, host, pid, status) | ||
| 161 | VALUES (COALESCE($1, NEXTVAL('minion_workers_id_seq')), $2, $3, $4) | ||
| 162 | ON CONFLICT(id) DO UPDATE SET notified = now(), status = $4 | ||
| 163 | RETURNING id}, $id, $self->{host} //= hostname, $$, {json => $options->{status} // {}} | ||
| 164 | )->hash->{id}; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub remove_job { | ||
| 168 | my ($self, $id) = @_; | ||
| 169 | return !!$self->pg->db->query( | ||
| 170 | "DELETE FROM minion_jobs WHERE id = ? AND state IN ('inactive', 'failed', 'finished') RETURNING 1", $id)->rows; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub repair { | ||
| 174 | my $self = shift; | ||
| 175 | |||
| 176 | # Workers without heartbeat | ||
| 177 | my $db = $self->pg->db; | ||
| 178 | my $minion = $self->minion; | ||
| 179 | $db->query("DELETE FROM minion_workers WHERE notified < NOW() - INTERVAL '1 second' * ?", $minion->missing_after); | ||
| 180 | |||
| 181 | # Old jobs with no unresolved dependencies and expired jobs | ||
| 182 | $db->query( | ||
| 183 | "DELETE FROM minion_jobs WHERE id IN ( | ||
| 184 | SELECT j.id FROM minion_jobs AS j LEFT JOIN minion_jobs AS children | ||
| 185 | ON children.state != 'finished' AND ARRAY_LENGTH(children.parents, 1) > 0 AND j.id = ANY(children.parents) | ||
| 186 | WHERE j.state = 'finished' AND j.finished <= NOW() - INTERVAL '1 second' * ? AND children.id IS NULL | ||
| 187 | UNION ALL | ||
| 188 | SELECT id FROM minion_jobs WHERE state = 'inactive' AND expires <= NOW() | ||
| 189 | )", $minion->remove_after | ||
| 190 | ); | ||
| 191 | |||
| 192 | # Jobs with missing worker (can be retried) | ||
| 193 | $db->query( | ||
| 194 | "SELECT id, retries FROM minion_jobs AS j | ||
| 195 | WHERE state = 'active' AND queue != 'minion_foreground' | ||
| 196 | AND NOT EXISTS (SELECT 1 FROM minion_workers WHERE id = j.worker)" | ||
| 197 | )->hashes->each(sub { $self->fail_job(@$_{qw(id retries)}, 'Worker went away') }); | ||
| 198 | |||
| 199 | # Jobs in queue without workers or not enough workers (cannot be retried and requires admin attention) | ||
| 200 | $db->query( | ||
| 201 | q{UPDATE minion_jobs SET state = 'failed', result = '"Job appears stuck in queue"' | ||
| 202 | WHERE state = 'inactive' AND delayed + ? * INTERVAL '1 second' < NOW()}, $minion->stuck_after | ||
| 203 | ); | ||
| 204 | } | ||
| 205 | |||
| 206 | sub reset { | ||
| 207 | my ($self, $options) = (shift, shift // {}); | ||
| 208 | |||
| 209 | if ($options->{all}) { $self->pg->db->query('TRUNCATE minion_jobs, minion_locks, minion_workers RESTART IDENTITY') } | ||
| 210 | elsif ($options->{locks}) { $self->pg->db->query('TRUNCATE minion_locks') } | ||
| 211 | } | ||
| 212 | |||
| 213 | sub retry_job { | ||
| 214 | my ($self, $id, $retries, $options) = (shift, shift, shift, shift || {}); | ||
| 215 | |||
| 216 | return !!$self->pg->db->query( | ||
| 217 | q{UPDATE minion_jobs SET attempts = COALESCE($1, attempts), delayed = (NOW() + (INTERVAL '1 second' * $2)), | ||
| 218 | expires = CASE WHEN $3::BIGINT IS NOT NULL THEN NOW() + (INTERVAL '1 second' * $3::BIGINT) ELSE expires END, | ||
| 219 | lax = COALESCE($4, lax), parents = COALESCE($5, parents), priority = COALESCE($6, priority), | ||
| 220 | queue = COALESCE($7, queue), retried = NOW(), retries = retries + 1, state = 'inactive' | ||
| 221 | WHERE id = $8 AND retries = $9 | ||
| 222 | RETURNING 1}, $options->{attempts}, $options->{delay} // 0, $options->{expire}, | ||
| 223 | exists $options->{lax} ? $options->{lax} ? 1 : 0 : undef, @$options{qw(parents priority queue)}, $id, $retries | ||
| 224 | )->rows; | ||
| 225 | } | ||
| 226 | |||
| 227 | sub stats { | ||
| 228 | my $self = shift; | ||
| 229 | |||
| 230 | my $stats = $self->pg->db->query( | ||
| 231 | "SELECT COUNT(*) FILTER (WHERE state = 'inactive' AND (expires IS NULL OR expires > NOW())) AS inactive_jobs, | ||
| 232 | COUNT(*) FILTER (WHERE state = 'active') AS active_jobs, COUNT(*) FILTER (WHERE state = 'failed') AS failed_jobs, | ||
| 233 | COUNT(*) FILTER (WHERE state = 'finished') AS finished_jobs, | ||
| 234 | COUNT(*) FILTER (WHERE state = 'inactive' AND delayed > NOW()) AS delayed_jobs, | ||
| 235 | (SELECT COUNT(*) FROM minion_locks WHERE expires > NOW()) AS active_locks, | ||
| 236 | COUNT(DISTINCT worker) FILTER (WHERE state = 'active') AS active_workers, | ||
| 237 | (SELECT CASE WHEN is_called THEN last_value ELSE 0 END FROM minion_jobs_id_seq) AS enqueued_jobs, | ||
| 238 | (SELECT COUNT(*) FROM minion_workers) AS workers, | ||
| 239 | EXTRACT(EPOCH FROM NOW() - PG_POSTMASTER_START_TIME()) AS uptime | ||
| 240 | FROM minion_jobs" | ||
| 241 | )->hash; | ||
| 242 | $stats->{inactive_workers} = $stats->{workers} - $stats->{active_workers}; | ||
| 243 | |||
| 244 | return $stats; | ||
| 245 | } | ||
| 246 | |||
| 247 | sub unlock { | ||
| 248 | !!shift->pg->db->query( | ||
| 249 | 'DELETE FROM minion_locks WHERE id = ( | ||
| 250 | SELECT id FROM minion_locks WHERE expires > NOW() AND name = ? ORDER BY expires LIMIT 1 FOR UPDATE | ||
| 251 | ) RETURNING 1', shift | ||
| 252 | )->rows; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub unregister_worker { shift->pg->db->query('DELETE FROM minion_workers WHERE id = ?', shift) } | ||
| 256 | |||
| 257 | sub _total { | ||
| 258 | my ($name, $results) = @_; | ||
| 259 | my $total = @$results ? $results->[0]{total} : 0; | ||
| 260 | delete $_->{total} for @$results; | ||
| 261 | return {total => $total, $name => $results}; | ||
| 262 | } | ||
| 263 | |||
| 264 | sub _try { | ||
| 265 | my ($self, $id, $options) = @_; | ||
| 266 | |||
| 267 | return $self->pg->db->query( | ||
| 268 | q{UPDATE minion_jobs SET started = NOW(), state = 'active', worker = ? | ||
| 269 | WHERE id = ( | ||
| 270 | SELECT id FROM minion_jobs AS j | ||
| 271 | WHERE delayed <= NOW() AND id = COALESCE(?, id) AND (parents = '{}' OR NOT EXISTS ( | ||
| 272 | SELECT 1 FROM minion_jobs WHERE id = ANY (j.parents) AND ( | ||
| 273 | state = 'active' OR (state = 'failed' AND NOT j.lax) | ||
| 274 | OR (state = 'inactive' AND (expires IS NULL OR expires > NOW()))) | ||
| 275 | )) AND priority >= COALESCE(?, priority) AND queue = ANY (?) AND state = 'inactive' AND task = ANY (?) | ||
| 276 | AND (EXPIRES IS NULL OR expires > NOW()) | ||
| 277 | ORDER BY priority DESC, id | ||
| 278 | LIMIT 1 | ||
| 279 | FOR UPDATE SKIP LOCKED | ||
| 280 | ) | ||
| 281 | RETURNING id, args, retries, task}, $id, $options->{id}, $options->{min_priority}, | ||
| 282 | $options->{queues} || ['default'], [keys %{$self->minion->tasks}] | ||
| 283 | )->expand->hash; | ||
| 284 | } | ||
| 285 | |||
| 286 | sub _update { | ||
| 287 | my ($self, $fail, $id, $retries, $result) = @_; | ||
| 288 | |||
| 289 | return undef unless my $row = $self->pg->db->query( | ||
| 290 | "UPDATE minion_jobs SET finished = NOW(), result = ?, state = ? | ||
| 291 | WHERE id = ? AND retries = ? AND state = 'active' | ||
| 292 | RETURNING attempts", {json => $result}, $fail ? 'failed' : 'finished', $id, $retries | ||
| 293 | )->array; | ||
| 294 | |||
| 295 | return $fail ? $self->auto_retry_job($id, $retries, $row->[0]) : 1; | ||
| 296 | } | ||
| 297 | |||
| 298 | 1; | ||
| 299 | |||
| 300 | =encoding utf8 | ||
| 301 | |||
| 302 | =head1 NAME | ||
| 303 | |||
| 304 | Minion::Backend::Pg - PostgreSQL backend | ||
| 305 | |||
| 306 | =head1 SYNOPSIS | ||
| 307 | |||
| 308 | use Minion::Backend::Pg; | ||
| 309 | |||
| 310 | my $backend = Minion::Backend::Pg->new('postgresql://postgres@/test'); | ||
| 311 | |||
| 312 | =head1 DESCRIPTION | ||
| 313 | |||
| 314 | L<Minion::Backend::Pg> is a backend for L<Minion> based on L<Mojo::Pg>. All necessary tables will be created | ||
| 315 | automatically with a set of migrations named C<minion>. Note that this backend uses many bleeding edge features, so | ||
| 316 | only the latest, stable version of PostgreSQL is fully supported. | ||
| 317 | |||
| 318 | =head1 ATTRIBUTES | ||
| 319 | |||
| 320 | L<Minion::Backend::Pg> inherits all attributes from L<Minion::Backend> and implements the following new ones. | ||
| 321 | |||
| 322 | =head2 pg | ||
| 323 | |||
| 324 | my $pg = $backend->pg; | ||
| 325 | $backend = $backend->pg(Mojo::Pg->new); | ||
| 326 | |||
| 327 | L<Mojo::Pg> object used to store all data. | ||
| 328 | |||
| 329 | =head1 METHODS | ||
| 330 | |||
| 331 | L<Minion::Backend::Pg> inherits all methods from L<Minion::Backend> and implements the following new ones. | ||
| 332 | |||
| 333 | =head2 broadcast | ||
| 334 | |||
| 335 | my $bool = $backend->broadcast('some_command'); | ||
| 336 | my $bool = $backend->broadcast('some_command', [@args]); | ||
| 337 | my $bool = $backend->broadcast('some_command', [@args], [$id1, $id2, $id3]); | ||
| 338 | |||
| 339 | Broadcast remote control command to one or more workers. | ||
| 340 | |||
| 341 | =head2 dequeue | ||
| 342 | |||
| 343 | my $job_info = $backend->dequeue($worker_id, 0.5); | ||
| 344 | my $job_info = $backend->dequeue($worker_id, 0.5, {queues => ['important']}); | ||
| 345 | |||
| 346 | Wait a given amount of time in seconds for a job, dequeue it and transition from C<inactive> to C<active> state, or | ||
| 347 | return C<undef> if queues were empty. | ||
| 348 | |||
| 349 | These options are currently available: | ||
| 350 | |||
| 351 | =over 2 | ||
| 352 | |||
| 353 | =item id | ||
| 354 | |||
| 355 | id => '10023' | ||
| 356 | |||
| 357 | Dequeue a specific job. | ||
| 358 | |||
| 359 | =item min_priority | ||
| 360 | |||
| 361 | min_priority => 3 | ||
| 362 | |||
| 363 | Do not dequeue jobs with a lower priority. | ||
| 364 | |||
| 365 | =item queues | ||
| 366 | |||
| 367 | queues => ['important'] | ||
| 368 | |||
| 369 | One or more queues to dequeue jobs from, defaults to C<default>. | ||
| 370 | |||
| 371 | =back | ||
| 372 | |||
| 373 | These fields are currently available: | ||
| 374 | |||
| 375 | =over 2 | ||
| 376 | |||
| 377 | =item args | ||
| 378 | |||
| 379 | args => ['foo', 'bar'] | ||
| 380 | |||
| 381 | Job arguments. | ||
| 382 | |||
| 383 | =item id | ||
| 384 | |||
| 385 | id => '10023' | ||
| 386 | |||
| 387 | Job ID. | ||
| 388 | |||
| 389 | =item retries | ||
| 390 | |||
| 391 | retries => 3 | ||
| 392 | |||
| 393 | Number of times job has been retried. | ||
| 394 | |||
| 395 | =item task | ||
| 396 | |||
| 397 | task => 'foo' | ||
| 398 | |||
| 399 | Task name. | ||
| 400 | |||
| 401 | =back | ||
| 402 | |||
| 403 | =head2 enqueue | ||
| 404 | |||
| 405 | my $job_id = $backend->enqueue('foo'); | ||
| 406 | my $job_id = $backend->enqueue(foo => [@args]); | ||
| 407 | my $job_id = $backend->enqueue(foo => [@args] => {priority => 1}); | ||
| 408 | |||
| 409 | Enqueue a new job with C<inactive> state. | ||
| 410 | |||
| 411 | These options are currently available: | ||
| 412 | |||
| 413 | =over 2 | ||
| 414 | |||
| 415 | =item attempts | ||
| 416 | |||
| 417 | attempts => 25 | ||
| 418 | |||
| 419 | Number of times performing this job will be attempted, with a delay based on L<Minion/"backoff"> after the first | ||
| 420 | attempt, defaults to C<1>. | ||
| 421 | |||
| 422 | =item delay | ||
| 423 | |||
| 424 | delay => 10 | ||
| 425 | |||
| 426 | Delay job for this many seconds (from now), defaults to C<0>. | ||
| 427 | |||
| 428 | =item expire | ||
| 429 | |||
| 430 | expire => 300 | ||
| 431 | |||
| 432 | Job is valid for this many seconds (from now) before it expires. | ||
| 433 | |||
| 434 | =item lax | ||
| 435 | |||
| 436 | lax => 1 | ||
| 437 | |||
| 438 | Existing jobs this job depends on may also have transitioned to the C<failed> state to allow for it to be processed, | ||
| 439 | defaults to C<false>. Note that this option is B<EXPERIMENTAL> and might change without warning! | ||
| 440 | |||
| 441 | =item notes | ||
| 442 | |||
| 443 | notes => {foo => 'bar', baz => [1, 2, 3]} | ||
| 444 | |||
| 445 | Hash reference with arbitrary metadata for this job. | ||
| 446 | |||
| 447 | =item parents | ||
| 448 | |||
| 449 | parents => [$id1, $id2, $id3] | ||
| 450 | |||
| 451 | One or more existing jobs this job depends on, and that need to have transitioned to the state C<finished> before it | ||
| 452 | can be processed. | ||
| 453 | |||
| 454 | =item priority | ||
| 455 | |||
| 456 | priority => 5 | ||
| 457 | |||
| 458 | Job priority, defaults to C<0>. Jobs with a higher priority get performed first. Priorities can be positive or negative, | ||
| 459 | but should be in the range between C<100> and C<-100>. | ||
| 460 | |||
| 461 | =item queue | ||
| 462 | |||
| 463 | queue => 'important' | ||
| 464 | |||
| 465 | Queue to put job in, defaults to C<default>. | ||
| 466 | |||
| 467 | =back | ||
| 468 | |||
| 469 | =head2 fail_job | ||
| 470 | |||
| 471 | my $bool = $backend->fail_job($job_id, $retries); | ||
| 472 | my $bool = $backend->fail_job($job_id, $retries, 'Something went wrong!'); | ||
| 473 | my $bool = $backend->fail_job( | ||
| 474 | $job_id, $retries, {whatever => 'Something went wrong!'}); | ||
| 475 | |||
| 476 | Transition from C<active> to C<failed> state with or without a result, and if there are attempts remaining, transition | ||
| 477 | back to C<inactive> with a delay based on L<Minion/"backoff">. | ||
| 478 | |||
| 479 | =head2 finish_job | ||
| 480 | |||
| 481 | my $bool = $backend->finish_job($job_id, $retries); | ||
| 482 | my $bool = $backend->finish_job($job_id, $retries, 'All went well!'); | ||
| 483 | my $bool = $backend->finish_job( | ||
| 484 | $job_id, $retries, {whatever => 'All went well!'}); | ||
| 485 | |||
| 486 | Transition from C<active> to C<finished> state with or without a result. | ||
| 487 | |||
| 488 | =head2 history | ||
| 489 | |||
| 490 | my $history = $backend->history; | ||
| 491 | |||
| 492 | Get history information for job queue. | ||
| 493 | |||
| 494 | These fields are currently available: | ||
| 495 | |||
| 496 | =over 2 | ||
| 497 | |||
| 498 | =item daily | ||
| 499 | |||
| 500 | daily => [{epoch => 12345, finished_jobs => 95, failed_jobs => 2}, ...] | ||
| 501 | |||
| 502 | Hourly counts for processed jobs from the past day. | ||
| 503 | |||
| 504 | =back | ||
| 505 | |||
| 506 | =head2 list_jobs | ||
| 507 | |||
| 508 | my $results = $backend->list_jobs($offset, $limit); | ||
| 509 | my $results = $backend->list_jobs($offset, $limit, {states => ['inactive']}); | ||
| 510 | |||
| 511 | Returns the information about jobs in batches. | ||
| 512 | |||
| 513 | # Get the total number of results (without limit) | ||
| 514 | my $num = $backend->list_jobs(0, 100, {queues => ['important']})->{total}; | ||
| 515 | |||
| 516 | # Check job state | ||
| 517 | my $results = $backend->list_jobs(0, 1, {ids => [$job_id]}); | ||
| 518 | my $state = $results->{jobs}[0]{state}; | ||
| 519 | |||
| 520 | # Get job result | ||
| 521 | my $results = $backend->list_jobs(0, 1, {ids => [$job_id]}); | ||
| 522 | my $result = $results->{jobs}[0]{result}; | ||
| 523 | |||
| 524 | These options are currently available: | ||
| 525 | |||
| 526 | =over 2 | ||
| 527 | |||
| 528 | =item before | ||
| 529 | |||
| 530 | before => 23 | ||
| 531 | |||
| 532 | List only jobs before this id. | ||
| 533 | |||
| 534 | =item ids | ||
| 535 | |||
| 536 | ids => ['23', '24'] | ||
| 537 | |||
| 538 | List only jobs with these ids. | ||
| 539 | |||
| 540 | =item notes | ||
| 541 | |||
| 542 | notes => ['foo', 'bar'] | ||
| 543 | |||
| 544 | List only jobs with one of these notes. | ||
| 545 | |||
| 546 | =item queues | ||
| 547 | |||
| 548 | queues => ['important', 'unimportant'] | ||
| 549 | |||
| 550 | List only jobs in these queues. | ||
| 551 | |||
| 552 | =item states | ||
| 553 | |||
| 554 | states => ['inactive', 'active'] | ||
| 555 | |||
| 556 | List only jobs in these states. | ||
| 557 | |||
| 558 | =item tasks | ||
| 559 | |||
| 560 | tasks => ['foo', 'bar'] | ||
| 561 | |||
| 562 | List only jobs for these tasks. | ||
| 563 | |||
| 564 | =back | ||
| 565 | |||
| 566 | These fields are currently available: | ||
| 567 | |||
| 568 | =over 2 | ||
| 569 | |||
| 570 | =item args | ||
| 571 | |||
| 572 | args => ['foo', 'bar'] | ||
| 573 | |||
| 574 | Job arguments. | ||
| 575 | |||
| 576 | =item attempts | ||
| 577 | |||
| 578 | attempts => 25 | ||
| 579 | |||
| 580 | Number of times performing this job will be attempted. | ||
| 581 | |||
| 582 | =item children | ||
| 583 | |||
| 584 | children => ['10026', '10027', '10028'] | ||
| 585 | |||
| 586 | Jobs depending on this job. | ||
| 587 | |||
| 588 | =item created | ||
| 589 | |||
| 590 | created => 784111777 | ||
| 591 | |||
| 592 | Epoch time job was created. | ||
| 593 | |||
| 594 | =item delayed | ||
| 595 | |||
| 596 | delayed => 784111777 | ||
| 597 | |||
| 598 | Epoch time job was delayed to. | ||
| 599 | |||
| 600 | =item expires | ||
| 601 | |||
| 602 | expires => 784111777 | ||
| 603 | |||
| 604 | Epoch time job is valid until before it expires. | ||
| 605 | |||
| 606 | =item finished | ||
| 607 | |||
| 608 | finished => 784111777 | ||
| 609 | |||
| 610 | Epoch time job was finished. | ||
| 611 | |||
| 612 | =item id | ||
| 613 | |||
| 614 | id => 10025 | ||
| 615 | |||
| 616 | Job id. | ||
| 617 | |||
| 618 | =item lax | ||
| 619 | |||
| 620 | lax => 0 | ||
| 621 | |||
| 622 | Existing jobs this job depends on may also have failed to allow for it to be processed. | ||
| 623 | |||
| 624 | =item notes | ||
| 625 | |||
| 626 | notes => {foo => 'bar', baz => [1, 2, 3]} | ||
| 627 | |||
| 628 | Hash reference with arbitrary metadata for this job. | ||
| 629 | |||
| 630 | =item parents | ||
| 631 | |||
| 632 | parents => ['10023', '10024', '10025'] | ||
| 633 | |||
| 634 | Jobs this job depends on. | ||
| 635 | |||
| 636 | =item priority | ||
| 637 | |||
| 638 | priority => 3 | ||
| 639 | |||
| 640 | Job priority. | ||
| 641 | |||
| 642 | =item queue | ||
| 643 | |||
| 644 | queue => 'important' | ||
| 645 | |||
| 646 | Queue name. | ||
| 647 | |||
| 648 | =item result | ||
| 649 | |||
| 650 | result => 'All went well!' | ||
| 651 | |||
| 652 | Job result. | ||
| 653 | |||
| 654 | =item retried | ||
| 655 | |||
| 656 | retried => 784111777 | ||
| 657 | |||
| 658 | Epoch time job has been retried. | ||
| 659 | |||
| 660 | =item retries | ||
| 661 | |||
| 662 | retries => 3 | ||
| 663 | |||
| 664 | Number of times job has been retried. | ||
| 665 | |||
| 666 | =item started | ||
| 667 | |||
| 668 | started => 784111777 | ||
| 669 | |||
| 670 | Epoch time job was started. | ||
| 671 | |||
| 672 | =item state | ||
| 673 | |||
| 674 | state => 'inactive' | ||
| 675 | |||
| 676 | Current job state, usually C<active>, C<failed>, C<finished> or C<inactive>. | ||
| 677 | |||
| 678 | =item task | ||
| 679 | |||
| 680 | task => 'foo' | ||
| 681 | |||
| 682 | Task name. | ||
| 683 | |||
| 684 | =item time | ||
| 685 | |||
| 686 | time => 78411177 | ||
| 687 | |||
| 688 | Server time. | ||
| 689 | |||
| 690 | =item worker | ||
| 691 | |||
| 692 | worker => '154' | ||
| 693 | |||
| 694 | Id of worker that is processing the job. | ||
| 695 | |||
| 696 | =back | ||
| 697 | |||
| 698 | =head2 list_locks | ||
| 699 | |||
| 700 | my $results = $backend->list_locks($offset, $limit); | ||
| 701 | my $results = $backend->list_locks($offset, $limit, {names => ['foo']}); | ||
| 702 | |||
| 703 | Returns information about locks in batches. | ||
| 704 | |||
| 705 | # Get the total number of results (without limit) | ||
| 706 | my $num = $backend->list_locks(0, 100, {names => ['bar']})->{total}; | ||
| 707 | |||
| 708 | # Check expiration time | ||
| 709 | my $results = $backend->list_locks(0, 1, {names => ['foo']}); | ||
| 710 | my $expires = $results->{locks}[0]{expires}; | ||
| 711 | |||
| 712 | These options are currently available: | ||
| 713 | |||
| 714 | =over 2 | ||
| 715 | |||
| 716 | =item names | ||
| 717 | |||
| 718 | names => ['foo', 'bar'] | ||
| 719 | |||
| 720 | List only locks with these names. | ||
| 721 | |||
| 722 | =back | ||
| 723 | |||
| 724 | These fields are currently available: | ||
| 725 | |||
| 726 | =over 2 | ||
| 727 | |||
| 728 | =item expires | ||
| 729 | |||
| 730 | expires => 784111777 | ||
| 731 | |||
| 732 | Epoch time this lock will expire. | ||
| 733 | |||
| 734 | =item name | ||
| 735 | |||
| 736 | name => 'foo' | ||
| 737 | |||
| 738 | Lock name. | ||
| 739 | |||
| 740 | =back | ||
| 741 | |||
| 742 | =head2 list_workers | ||
| 743 | |||
| 744 | my $results = $backend->list_workers($offset, $limit); | ||
| 745 | my $results = $backend->list_workers($offset, $limit, {ids => [23]}); | ||
| 746 | |||
| 747 | Returns information about workers in batches. | ||
| 748 | |||
| 749 | # Get the total number of results (without limit) | ||
| 750 | my $num = $backend->list_workers(0, 100)->{total}; | ||
| 751 | |||
| 752 | # Check worker host | ||
| 753 | my $results = $backend->list_workers(0, 1, {ids => [$worker_id]}); | ||
| 754 | my $host = $results->{workers}[0]{host}; | ||
| 755 | |||
| 756 | These options are currently available: | ||
| 757 | |||
| 758 | =over 2 | ||
| 759 | |||
| 760 | =item before | ||
| 761 | |||
| 762 | before => 23 | ||
| 763 | |||
| 764 | List only workers before this id. | ||
| 765 | |||
| 766 | =item ids | ||
| 767 | |||
| 768 | ids => ['23', '24'] | ||
| 769 | |||
| 770 | List only workers with these ids. | ||
| 771 | |||
| 772 | =back | ||
| 773 | |||
| 774 | These fields are currently available: | ||
| 775 | |||
| 776 | =over 2 | ||
| 777 | |||
| 778 | =item id | ||
| 779 | |||
| 780 | id => 22 | ||
| 781 | |||
| 782 | Worker id. | ||
| 783 | |||
| 784 | =item host | ||
| 785 | |||
| 786 | host => 'localhost' | ||
| 787 | |||
| 788 | Worker host. | ||
| 789 | |||
| 790 | =item jobs | ||
| 791 | |||
| 792 | jobs => ['10023', '10024', '10025', '10029'] | ||
| 793 | |||
| 794 | Ids of jobs the worker is currently processing. | ||
| 795 | |||
| 796 | =item notified | ||
| 797 | |||
| 798 | notified => 784111777 | ||
| 799 | |||
| 800 | Epoch time worker sent the last heartbeat. | ||
| 801 | |||
| 802 | =item pid | ||
| 803 | |||
| 804 | pid => 12345 | ||
| 805 | |||
| 806 | Process id of worker. | ||
| 807 | |||
| 808 | =item started | ||
| 809 | |||
| 810 | started => 784111777 | ||
| 811 | |||
| 812 | Epoch time worker was started. | ||
| 813 | |||
| 814 | =item status | ||
| 815 | |||
| 816 | status => {queues => ['default', 'important']} | ||
| 817 | |||
| 818 | Hash reference with whatever status information the worker would like to share. | ||
| 819 | |||
| 820 | =back | ||
| 821 | |||
| 822 | =head2 lock | ||
| 823 | |||
| 824 | my $bool = $backend->lock('foo', 3600); | ||
| 825 | my $bool = $backend->lock('foo', 3600, {limit => 20}); | ||
| 826 | |||
| 827 | Try to acquire a named lock that will expire automatically after the given amount of time in seconds. An expiration | ||
| 828 | time of C<0> can be used to check if a named lock already exists without creating one. | ||
| 829 | |||
| 830 | These options are currently available: | ||
| 831 | |||
| 832 | =over 2 | ||
| 833 | |||
| 834 | =item limit | ||
| 835 | |||
| 836 | limit => 20 | ||
| 837 | |||
| 838 | Number of shared locks with the same name that can be active at the same time, defaults to C<1>. | ||
| 839 | |||
| 840 | =back | ||
| 841 | |||
| 842 | =head2 new | ||
| 843 | |||
| 844 | my $backend = Minion::Backend::Pg->new('postgresql://postgres@/test'); | ||
| 845 | my $backend = Minion::Backend::Pg->new(Mojo::Pg->new); | ||
| 846 | |||
| 847 | Construct a new L<Minion::Backend::Pg> object. | ||
| 848 | |||
| 849 | =head2 note | ||
| 850 | |||
| 851 | my $bool = $backend->note($job_id, {mojo => 'rocks', minion => 'too'}); | ||
| 852 | |||
| 853 | Change one or more metadata fields for a job. Setting a value to C<undef> will remove the field. | ||
| 854 | |||
| 855 | =head2 receive | ||
| 856 | |||
| 857 | my $commands = $backend->receive($worker_id); | ||
| 858 | |||
| 859 | Receive remote control commands for worker. | ||
| 860 | |||
| 861 | =head2 register_worker | ||
| 862 | |||
| 863 | my $worker_id = $backend->register_worker; | ||
| 864 | my $worker_id = $backend->register_worker($worker_id); | ||
| 865 | my $worker_id = $backend->register_worker( | ||
| 866 | $worker_id, {status => {queues => ['default', 'important']}}); | ||
| 867 | |||
| 868 | Register worker or send heartbeat to show that this worker is still alive. | ||
| 869 | |||
| 870 | These options are currently available: | ||
| 871 | |||
| 872 | =over 2 | ||
| 873 | |||
| 874 | =item status | ||
| 875 | |||
| 876 | status => {queues => ['default', 'important']} | ||
| 877 | |||
| 878 | Hash reference with whatever status information the worker would like to share. | ||
| 879 | |||
| 880 | =back | ||
| 881 | |||
| 882 | =head2 remove_job | ||
| 883 | |||
| 884 | my $bool = $backend->remove_job($job_id); | ||
| 885 | |||
| 886 | Remove C<failed>, C<finished> or C<inactive> job from queue. | ||
| 887 | |||
| 888 | =head2 repair | ||
| 889 | |||
| 890 | $backend->repair; | ||
| 891 | |||
| 892 | Repair worker registry and job queue if necessary. | ||
| 893 | |||
| 894 | =head2 reset | ||
| 895 | |||
| 896 | $backend->reset({all => 1}); | ||
| 897 | |||
| 898 | Reset job queue. | ||
| 899 | |||
| 900 | These options are currently available: | ||
| 901 | |||
| 902 | =over 2 | ||
| 903 | |||
| 904 | =item all | ||
| 905 | |||
| 906 | all => 1 | ||
| 907 | |||
| 908 | Reset everything. | ||
| 909 | |||
| 910 | =item locks | ||
| 911 | |||
| 912 | locks => 1 | ||
| 913 | |||
| 914 | Reset only locks. | ||
| 915 | |||
| 916 | =back | ||
| 917 | |||
| 918 | =head2 retry_job | ||
| 919 | |||
| 920 | my $bool = $backend->retry_job($job_id, $retries); | ||
| 921 | my $bool = $backend->retry_job($job_id, $retries, {delay => 10}); | ||
| 922 | |||
| 923 | Transition job back to C<inactive> state, already C<inactive> jobs may also be retried to change options. | ||
| 924 | |||
| 925 | These options are currently available: | ||
| 926 | |||
| 927 | =over 2 | ||
| 928 | |||
| 929 | =item attempts | ||
| 930 | |||
| 931 | attempts => 25 | ||
| 932 | |||
| 933 | Number of times performing this job will be attempted. | ||
| 934 | |||
| 935 | =item delay | ||
| 936 | |||
| 937 | delay => 10 | ||
| 938 | |||
| 939 | Delay job for this many seconds (from now), defaults to C<0>. | ||
| 940 | |||
| 941 | =item expire | ||
| 942 | |||
| 943 | expire => 300 | ||
| 944 | |||
| 945 | Job is valid for this many seconds (from now) before it expires. | ||
| 946 | |||
| 947 | =item lax | ||
| 948 | |||
| 949 | lax => 1 | ||
| 950 | |||
| 951 | Existing jobs this job depends on may also have transitioned to the C<failed> state to allow for it to be processed, | ||
| 952 | defaults to C<false>. Note that this option is B<EXPERIMENTAL> and might change without warning! | ||
| 953 | |||
| 954 | =item parents | ||
| 955 | |||
| 956 | parents => [$id1, $id2, $id3] | ||
| 957 | |||
| 958 | Jobs this job depends on. | ||
| 959 | |||
| 960 | =item priority | ||
| 961 | |||
| 962 | priority => 5 | ||
| 963 | |||
| 964 | Job priority. | ||
| 965 | |||
| 966 | =item queue | ||
| 967 | |||
| 968 | queue => 'important' | ||
| 969 | |||
| 970 | Queue to put job in. | ||
| 971 | |||
| 972 | =back | ||
| 973 | |||
| 974 | =head2 stats | ||
| 975 | |||
| 976 | my $stats = $backend->stats; | ||
| 977 | |||
| 978 | Get statistics for the job queue. | ||
| 979 | |||
| 980 | These fields are currently available: | ||
| 981 | |||
| 982 | =over 2 | ||
| 983 | |||
| 984 | =item active_jobs | ||
| 985 | |||
| 986 | active_jobs => 100 | ||
| 987 | |||
| 988 | Number of jobs in C<active> state. | ||
| 989 | |||
| 990 | =item active_locks | ||
| 991 | |||
| 992 | active_locks => 100 | ||
| 993 | |||
| 994 | Number of active named locks. | ||
| 995 | |||
| 996 | =item active_workers | ||
| 997 | |||
| 998 | active_workers => 100 | ||
| 999 | |||
| 1000 | Number of workers that are currently processing a job. | ||
| 1001 | |||
| 1002 | =item delayed_jobs | ||
| 1003 | |||
| 1004 | delayed_jobs => 100 | ||
| 1005 | |||
| 1006 | Number of jobs in C<inactive> state that are scheduled to run at specific time in the future. | ||
| 1007 | |||
| 1008 | =item enqueued_jobs | ||
| 1009 | |||
| 1010 | enqueued_jobs => 100000 | ||
| 1011 | |||
| 1012 | Rough estimate of how many jobs have ever been enqueued. | ||
| 1013 | |||
| 1014 | =item failed_jobs | ||
| 1015 | |||
| 1016 | failed_jobs => 100 | ||
| 1017 | |||
| 1018 | Number of jobs in C<failed> state. | ||
| 1019 | |||
| 1020 | =item finished_jobs | ||
| 1021 | |||
| 1022 | finished_jobs => 100 | ||
| 1023 | |||
| 1024 | Number of jobs in C<finished> state. | ||
| 1025 | |||
| 1026 | =item inactive_jobs | ||
| 1027 | |||
| 1028 | inactive_jobs => 100 | ||
| 1029 | |||
| 1030 | Number of jobs in C<inactive> state. | ||
| 1031 | |||
| 1032 | =item inactive_workers | ||
| 1033 | |||
| 1034 | inactive_workers => 100 | ||
| 1035 | |||
| 1036 | Number of workers that are currently not processing a job. | ||
| 1037 | |||
| 1038 | =item uptime | ||
| 1039 | |||
| 1040 | uptime => 1000 | ||
| 1041 | |||
| 1042 | Uptime in seconds. | ||
| 1043 | |||
| 1044 | =item workers | ||
| 1045 | |||
| 1046 | workers => 200; | ||
| 1047 | |||
| 1048 | Number of registered workers. | ||
| 1049 | |||
| 1050 | =back | ||
| 1051 | |||
| 1052 | =head2 unlock | ||
| 1053 | |||
| 1054 | my $bool = $backend->unlock('foo'); | ||
| 1055 | |||
| 1056 | Release a named lock. | ||
| 1057 | |||
| 1058 | =head2 unregister_worker | ||
| 1059 | |||
| 1060 | $backend->unregister_worker($worker_id); | ||
| 1061 | |||
| 1062 | Unregister worker. | ||
| 1063 | |||
| 1064 | =head1 SEE ALSO | ||
| 1065 | |||
| 1066 | L<Minion>, L<Minion::Guide>, L<https://minion.pm>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||
| 1067 | |||
| 1068 | =cut |