| 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 => 20; |
|---|
| 10 | |
|---|
| 11 | # for testing: |
|---|
| 12 | $TheSchwartz::T_EXITSTATUS_CLEAN_THRES = 1; # delete 100% of the time, not 10% of the time |
|---|
| 13 | $TheSchwartz::T_ERRORS_MAX_AGE = 2; # keep errors for 3 seconds, not 1 week |
|---|
| 14 | |
|---|
| 15 | run_tests(10, sub { |
|---|
| 16 | my $client = test_client(dbs => ['ts1']); |
|---|
| 17 | my $dbh = DBI->connect(dsn_for("ts1"), 'root', ''); |
|---|
| 18 | $client->can_do("Worker::Fail"); |
|---|
| 19 | $client->can_do("Worker::Complete"); |
|---|
| 20 | |
|---|
| 21 | # insert a job which will fail, then succeed. |
|---|
| 22 | { |
|---|
| 23 | my $handle = $client->insert("Worker::Fail"); |
|---|
| 24 | isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job"; |
|---|
| 25 | |
|---|
| 26 | $client->work_until_done; |
|---|
| 27 | is($handle->failures, 1, "job has failed once"); |
|---|
| 28 | |
|---|
| 29 | my $min; |
|---|
| 30 | my $rows = $dbh->selectrow_array("SELECT COUNT(*) FROM exitstatus"); |
|---|
| 31 | is($rows, 1, "has 1 exitstatus row"); |
|---|
| 32 | |
|---|
| 33 | ok($client->insert("Worker::Complete"), "inserting to-pass job"); |
|---|
| 34 | $client->work_until_done; |
|---|
| 35 | $rows = $dbh->selectrow_array("SELECT COUNT(*) FROM exitstatus"); |
|---|
| 36 | is($rows, 2, "has 2 exitstatus rows"); |
|---|
| 37 | ($rows, $min) = $dbh->selectrow_array("SELECT COUNT(*), MIN(jobid) FROM error"); |
|---|
| 38 | is($rows, 1, "has 1 error rows"); |
|---|
| 39 | is($min, 1, "error jobid is the old one"); |
|---|
| 40 | |
|---|
| 41 | # wait for exit status to pass |
|---|
| 42 | sleep 3; |
|---|
| 43 | |
|---|
| 44 | # now make another job fail to cleanup some errors |
|---|
| 45 | $handle = $client->insert("Worker::Fail"); |
|---|
| 46 | $client->work_until_done; |
|---|
| 47 | |
|---|
| 48 | $rows = $dbh->selectrow_array("SELECT COUNT(*) FROM exitstatus"); |
|---|
| 49 | is($rows, 1, "1 exit status row now"); |
|---|
| 50 | |
|---|
| 51 | ($rows, $min) = $dbh->selectrow_array("SELECT COUNT(*), MIN(jobid) FROM error"); |
|---|
| 52 | is($rows, 1, "has 1 error row still"); |
|---|
| 53 | is($min, 3, "error jobid is only the new one"); |
|---|
| 54 | |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | teardown_dbs('ts1'); |
|---|
| 58 | }); |
|---|
| 59 | |
|---|
| 60 | ############################################################################ |
|---|
| 61 | package Worker::Fail; |
|---|
| 62 | use base 'TheSchwartz::Worker'; |
|---|
| 63 | |
|---|
| 64 | sub work { |
|---|
| 65 | my ($class, $job) = @_; |
|---|
| 66 | $job->failed("an error message"); |
|---|
| 67 | return; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | sub keep_exit_status_for { 1 } # keep exit status for 20 seconds after on_complete |
|---|
| 71 | |
|---|
| 72 | sub max_retries { 0 } |
|---|
| 73 | |
|---|
| 74 | sub retry_delay { 1 } |
|---|
| 75 | |
|---|
| 76 | # --------------- |
|---|
| 77 | |
|---|
| 78 | package Worker::Complete; |
|---|
| 79 | use base 'TheSchwartz::Worker'; |
|---|
| 80 | sub work { |
|---|
| 81 | my ($class, $job) = @_; |
|---|
| 82 | $job->completed; |
|---|
| 83 | return; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | sub keep_exit_status_for { 1 } |
|---|
| 87 | |
|---|