From 5e59a1c411556ac8bf247b4f860dcd8af437f067 Mon Sep 17 00:00:00 2001 From: Sam Bostock Date: Mon, 4 Jul 2022 11:09:34 -0400 Subject: [PATCH] [SQUASH] Prefix max_job_runtime class attr --- CHANGELOG.md | 2 +- guides/best-practices.md | 4 ++-- lib/job-iteration/iteration.rb | 10 +++++----- test/unit/iteration_test.rb | 20 ++++++++++---------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b329032..c98a47ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ - [241](https://github.com/Shopify/job-iteration/pull/241) - Require Ruby 2.7+, dropping 2.6 support - [241](https://github.com/Shopify/job-iteration/pull/241) - Require Rails 6.0+, dropping 5.2 support -- [240](https://github.com/Shopify/job-iteration/pull/240) - Allow setting inheritable per-job `max_job_runtime` +- [240](https://github.com/Shopify/job-iteration/pull/240) - Allow setting inheritable per-job `job_iteration_max_job_runtime` ## v1.3.6 (Mar 9, 2022) diff --git a/guides/best-practices.md b/guides/best-practices.md index 743eb36b..30790c55 100644 --- a/guides/best-practices.md +++ b/guides/best-practices.md @@ -69,13 +69,13 @@ Use this accessor to tweak how often you'd like the job to interrupt itself. ### Per job max job runtime -For more granular control, `max_job_runtime` can be set **per-job class**. This allows both incremental adoption, as well as using a conservative global setting, and an aggressive setting on a per-job basis. +For more granular control, `job_iteration_max_job_runtime` can be set **per-job class**. This allows both incremental adoption, as well as using a conservative global setting, and an aggressive setting on a per-job basis. ```ruby class MyJob < ApplicationJob include JobIteration::Iteration - self.max_job_runtime = 3.minutes + self.job_iteration_max_job_runtime = 3.minutes # ... ``` diff --git a/lib/job-iteration/iteration.rb b/lib/job-iteration/iteration.rb index 0c3a3564..28f1d78b 100644 --- a/lib/job-iteration/iteration.rb +++ b/lib/job-iteration/iteration.rb @@ -41,7 +41,7 @@ def inspected_cursor define_callbacks :complete class_attribute( - :max_job_runtime, + :job_iteration_max_job_runtime, instance_writer: false, instance_predicate: false, default: JobIteration.max_job_runtime, @@ -51,15 +51,15 @@ def inspected_cursor end module PrependedClassMethods - def max_job_runtime=(new) - existing = max_job_runtime + def job_iteration_max_job_runtime=(new) + existing = job_iteration_max_job_runtime if existing && (!new || new > existing) existing_label = existing.inspect new_label = new ? new.inspect : "#{new.inspect} (no limit)" raise( ArgumentError, - "max_job_runtime may only decrease; #{self} tried to increase it from #{existing_label} to #{new_label}", + "job_iteration_max_job_runtime may only decrease; #{self} tried to increase it from #{existing_label} to #{new_label}", ) end @@ -288,7 +288,7 @@ def output_interrupt_summary end def job_should_exit? - if max_job_runtime && start_time && (Time.now.utc - start_time) > max_job_runtime + if job_iteration_max_job_runtime && start_time && (Time.now.utc - start_time) > job_iteration_max_job_runtime return true end diff --git a/test/unit/iteration_test.rb b/test/unit/iteration_test.rb index 0e44c0a2..8f367c47 100644 --- a/test/unit/iteration_test.rb +++ b/test/unit/iteration_test.rb @@ -255,7 +255,7 @@ def test_per_class_max_job_runtime_with_default_global freeze_time parent = build_slow_job_class(iterations: 3, iteration_duration: 30.seconds) child = Class.new(parent) do - self.max_job_runtime = 1.minute + self.job_iteration_max_job_runtime = 1.minute end parent.perform_now @@ -270,7 +270,7 @@ def test_per_class_max_job_runtime_with_global_set_to_nil with_global_max_job_runtime(nil) do parent = build_slow_job_class(iterations: 3, iteration_duration: 30.seconds) child = Class.new(parent) do - self.max_job_runtime = 1.minute + self.job_iteration_max_job_runtime = 1.minute end parent.perform_now @@ -286,7 +286,7 @@ def test_per_class_max_job_runtime_with_global_set with_global_max_job_runtime(1.minute) do parent = build_slow_job_class(iterations: 3, iteration_duration: 30.seconds) child = Class.new(parent) do - self.max_job_runtime = 30.seconds + self.job_iteration_max_job_runtime = 30.seconds end parent.perform_now @@ -305,11 +305,11 @@ def test_max_job_runtime_cannot_unset_global end error = assert_raises(ArgumentError) do - klass.max_job_runtime = nil + klass.job_iteration_max_job_runtime = nil end assert_equal( - "max_job_runtime may only decrease; #{klass} tried to increase it from 30 seconds to nil (no limit)", + "job_iteration_max_job_runtime may only decrease; #{klass} tried to increase it from 30 seconds to nil (no limit)", error.message, ) end @@ -322,11 +322,11 @@ def test_max_job_runtime_cannot_be_higher_than_global end error = assert_raises(ArgumentError) do - klass.max_job_runtime = 1.minute + klass.job_iteration_max_job_runtime = 1.minute end assert_equal( - "max_job_runtime may only decrease; #{klass} tried to increase it from 30 seconds to 1 minute", + "job_iteration_max_job_runtime may only decrease; #{klass} tried to increase it from 30 seconds to 1 minute", error.message, ) end @@ -336,16 +336,16 @@ def test_max_job_runtime_cannot_be_higher_than_parent with_global_max_job_runtime(1.minute) do parent = Class.new(ActiveJob::Base) do include JobIteration::Iteration - self.max_job_runtime = 30.seconds + self.job_iteration_max_job_runtime = 30.seconds end child = Class.new(parent) error = assert_raises(ArgumentError) do - child.max_job_runtime = 45.seconds + child.job_iteration_max_job_runtime = 45.seconds end assert_equal( - "max_job_runtime may only decrease; #{child} tried to increase it from 30 seconds to 45 seconds", + "job_iteration_max_job_runtime may only decrease; #{child} tried to increase it from 30 seconds to 45 seconds", error.message, ) end