From f326e53a2d35fd89c46ac04ea3983a343073f4dc Mon Sep 17 00:00:00 2001 From: Arlantir <11597534+Arlantir@users.noreply.github.com> Date: Fri, 5 Apr 2024 11:42:32 +0300 Subject: [PATCH] reset job failed flag --- .gitignore | 1 + CHANGELOG.md | 12 ++++++++++++ lib/yabeda/schked.rb | 2 ++ lib/yabeda/schked/version.rb | 2 +- spec/support/schedule.rb | 6 +++--- spec/yabeda/schked_spec.rb | 38 ++++++++++++++++++++++++++++-------- 6 files changed, 49 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 6355904..1570dfb 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ /pkg/ /spec/reports/ /tmp/ +/.idea Gemfile.lock diff --git a/CHANGELOG.md b/CHANGELOG.md index 5377a23..8e07d44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## 0.2.1 (2024-04-05) + +### 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 +38,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..bf8dd36 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) + + job.opts[:failed] = false end ::Schked.config.register_callback(:on_error) do |job| diff --git a/lib/yabeda/schked/version.rb b/lib/yabeda/schked/version.rb index ad949df..5efce5c 100644 --- a/lib/yabeda/schked/version.rb +++ b/lib/yabeda/schked/version.rb @@ -2,6 +2,6 @@ module Yabeda module Schked - VERSION = "0.2.0" + VERSION = "0.2.1" end end 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