diff --git a/CHANGELOG.md b/CHANGELOG.md index 5377a23..914c07c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Fixed + +- Reset the `job.opts[:failed]` flag after a failed job [@arlantir] +- Restoring tests [@arlantir] + +### Changed + +- Refactoring tests [@arlantir] + ## 0.2.0 (2022-10-19) ### Added @@ -27,3 +36,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. [@skryukov]: https://github.com/skryukov [@bibendi]: https://github.com/bibendi +[@arlantir]: https://github.com/arlantir diff --git a/lib/yabeda/schked.rb b/lib/yabeda/schked.rb index e654d35..19aec60 100644 --- a/lib/yabeda/schked.rb +++ b/lib/yabeda/schked.rb @@ -41,6 +41,8 @@ def self.job_name(job) labels = {success: !job.opts[:failed], name: job_name(job)} Yabeda.schked.job_execution_runtime.measure(labels, job.last_work_time.round(3)) Yabeda.schked.jobs_executed_total.increment(labels) + ensure + job.opts[:failed] = false end ::Schked.config.register_callback(:on_error) do |job| diff --git a/spec/support/schedule.rb b/spec/support/schedule.rb index 6757233..e0bce65 100644 --- a/spec/support/schedule.rb +++ b/spec/support/schedule.rb @@ -1,13 +1,13 @@ # frozen_string_literal: true -at "2020-01-01 00:00:00", as: "SuccessfulJob", blocking: true do +cron "*/30 * * * *", as: "SuccessfulJob", blocking: true do :ok end -at "2020-01-01 00:00:00", as: "FailedJob", blocking: true do +cron "*/30 * * * *", as: "FailedJob", blocking: true do raise StandardError, "Boom!" end -at "2020-01-01 00:00:00", tag: "without_name", blocking: true do +cron "*/30 * * * *", tag: "without_name", blocking: true do :ok end diff --git a/spec/yabeda/schked_spec.rb b/spec/yabeda/schked_spec.rb index a246ca5..2ce1624 100644 --- a/spec/yabeda/schked_spec.rb +++ b/spec/yabeda/schked_spec.rb @@ -9,12 +9,40 @@ let(:worker) { Schked.worker.tap(&:pause) } let(:job) { worker.job(job_name) } + before do + Yabeda.schked.jobs_executed_total.values.clear + Yabeda.schked.job_execution_runtime.values.clear + end + context "when job is successful" do let(:job_name) { "SuccessfulJob" } it "measures called job" do - Yabeda.schked.jobs_executed_total.values.clear - Yabeda.schked.job_execution_runtime.values.clear + job.trigger_off_schedule + + expect(Yabeda.schked.jobs_executed_total.values).to include( + {name: job_name, success: true} => 1 + ) + expect(Yabeda.schked.job_execution_runtime.values).to include( + {name: job_name, success: true} => kind_of(Numeric) + ) + end + end + + context "when job fails on first call but succeeds on second" do + let(:job_name) { "SuccessfulJob" } + + it "measures the job with failure and success" do + job.opts[:failed] = true + + job.trigger_off_schedule + + expect(Yabeda.schked.jobs_executed_total.values).to include( + {name: job_name, success: false} => 1 + ) + expect(Yabeda.schked.job_execution_runtime.values).to include( + {name: job_name, success: false} => kind_of(Numeric) + ) job.trigger_off_schedule @@ -31,9 +59,6 @@ let(:job_name) { "FailedJob" } it "measures called job" do - Yabeda.schked.jobs_executed_total.values.clear - Yabeda.schked.job_execution_runtime.values.clear - job.trigger_off_schedule expect(Yabeda.schked.jobs_executed_total.values).to include( @@ -49,9 +74,6 @@ let(:job_name) { nil } it "measures called job" do - Yabeda.schked.jobs_executed_total.values.clear - Yabeda.schked.job_execution_runtime.values.clear - expect { job.trigger_off_schedule }.to output( /Warning: No name specified for the job/ ).to_stderr