| 1 | # -*-perl-*- |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | require 't/lib/db-common.pl'; |
|---|
| 7 | |
|---|
| 8 | use TheSchwartz; |
|---|
| 9 | use Test::More tests => 30; |
|---|
| 10 | |
|---|
| 11 | run_tests(10, sub { |
|---|
| 12 | my $client = test_client(dbs => ['ts1']); |
|---|
| 13 | |
|---|
| 14 | my $handle = $client->insert("Worker::Foo", { cluster => 'all'}); |
|---|
| 15 | ok($handle); |
|---|
| 16 | |
|---|
| 17 | my $job = Worker::Foo->grab_job($client); |
|---|
| 18 | ok($job, "no addition jobs to be grabbed"); |
|---|
| 19 | |
|---|
| 20 | Worker::Foo->work_safely($job); |
|---|
| 21 | |
|---|
| 22 | $client->can_do("Worker::Foo"); |
|---|
| 23 | $client->work_until_done; # should process 5 jobs. |
|---|
| 24 | |
|---|
| 25 | # finish a job by replacing it with nothing |
|---|
| 26 | $handle = $client->insert("Worker::Foo", { cluster => 'gibberish'}); |
|---|
| 27 | ok($handle->is_pending, "job is still pending"); |
|---|
| 28 | $job = $handle->job; |
|---|
| 29 | $job->replace_with(); |
|---|
| 30 | ok(! $handle->is_pending, "job no longer pending"); |
|---|
| 31 | |
|---|
| 32 | teardown_dbs('ts1'); |
|---|
| 33 | }); |
|---|
| 34 | |
|---|
| 35 | ############################################################################ |
|---|
| 36 | package Worker::Foo; |
|---|
| 37 | use base 'TheSchwartz::Worker'; |
|---|
| 38 | |
|---|
| 39 | use Test::More; ## Import test methods. |
|---|
| 40 | |
|---|
| 41 | sub work { |
|---|
| 42 | my ($class, $job) = @_; |
|---|
| 43 | my $args = $job->arg; |
|---|
| 44 | |
|---|
| 45 | if ($args->{cluster} eq "all") { |
|---|
| 46 | ok(1, "got the expand job"); |
|---|
| 47 | my @jobs; |
|---|
| 48 | for (1..5) { |
|---|
| 49 | push @jobs, TheSchwartz::Job->new_from_array("Worker::Foo", |
|---|
| 50 | { cluster => $_ } |
|---|
| 51 | ); |
|---|
| 52 | } |
|---|
| 53 | # which does a $job->completed iff all the @jobs, in one txn, insert |
|---|
| 54 | # on the same database that $job was on. and it should DIE if the |
|---|
| 55 | # transaction fails, just so txn flow doesn't proceed on accident. |
|---|
| 56 | # then work_safely with catch the die and call $job->failed |
|---|
| 57 | $job->replace_with(@jobs); |
|---|
| 58 | return; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | if ($args->{cluster} =~ /^\d+$/) { |
|---|
| 62 | ok(1, "got job $args->{cluster}"); |
|---|
| 63 | $job->completed; |
|---|
| 64 | return; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | # if anything were to fall through the bottom of here without |
|---|
| 68 | # first calling fail/completed/replace_with, or dying, then the |
|---|
| 69 | # work_safely wrapper should treat it as a "fall-through" failure |
|---|
| 70 | # and log it, doing the whole retries/delay thing as with a |
|---|
| 71 | # regular die. |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | sub grab_for { 30 } |
|---|
| 75 | |
|---|