good_job 2.15.2 → 2.16.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb6d6387b67811dec6d23e26267a4fe356c42ddc3feb14cb046f57c61c2b5802
4
- data.tar.gz: 7ce4090bfeb05c67913f0d6f99fb2517a4370b669fdbfb648a17bfdca3d92efc
3
+ metadata.gz: 1811646b013e9432de87917a2be838ee634902b22ecc942bae74a7fefab82487
4
+ data.tar.gz: 14a301ce871b2761484d1c40d71dc76a64ce40131b6c4f3ff5573c1a2201ee58
5
5
  SHA512:
6
- metadata.gz: b2efda0438bff75e52ab9e390ee3bf33bdbb64b13a4937ed3e58170100b0afd0a0ead5a3b1b9d902548c4356eba1b9f21296bd94f4f84ecd7884ef2b28fa0a09
7
- data.tar.gz: d66ebb51d7c83b0b8c77d1300707a5c82df9bdafd1bc91231093ce331d59ff788c9c59539e10f3996664b572dec3b1dab019d01d2855bf497e3b4efd86929616
6
+ metadata.gz: f719e920a42b626b022ea6e90572344a660adf9fac9978fcde0ae98abbb0433d89a32af25f51e14a7250ffa2ffb128accc95fce9121630cc178b957f114e2fdf
7
+ data.tar.gz: 83265d3360d14d249303179d5ef506e39709ddf9fbbc87df99b42e5b6057d1e9acc0a59712e8b82f0d79df69b868a26b0f5de9ebf0e1adac2e421f0b1288b94f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## [v2.16.0](https://github.com/bensheldon/good_job/tree/v2.16.0) (2022-06-17)
4
+
5
+ [Full Changelog](https://github.com/bensheldon/good_job/compare/v2.15.2...v2.16.0)
6
+
7
+ **Closed issues:**
8
+
9
+ - Upgrading zeitwerk to 2.6.0 causes a warning related to good\_job [\#616](https://github.com/bensheldon/good_job/issues/616)
10
+
11
+ **Merged pull requests:**
12
+
13
+ - Allow inline executor to respect scheduled jobs; deprecate old behavior. Add `GoodJob.perform_inline` [\#615](https://github.com/bensheldon/good_job/pull/615) ([bensheldon](https://github.com/bensheldon))
14
+
3
15
  ## [v2.15.2](https://github.com/bensheldon/good_job/tree/v2.15.2) (2022-06-17)
4
16
 
5
17
  [Full Changelog](https://github.com/bensheldon/good_job/compare/v2.15.1...v2.15.2)
data/README.md CHANGED
@@ -59,6 +59,7 @@ For more of the story of GoodJob, read the [introductory blog post](https://isla
59
59
  - [Execute jobs async / in-process](#execute-jobs-async--in-process)
60
60
  - [Migrate to GoodJob from a different ActiveJob backend](#migrate-to-goodjob-from-a-different-activejob-backend)
61
61
  - [Monitor and preserve worked jobs](#monitor-and-preserve-worked-jobs)
62
+ - [Write tests](#write-tests)
62
63
  - [PgBouncer compatibility](#pgbouncer-compatibility)
63
64
  - [CLI HTTP health check probes](#cli-http-health-check-probes)
64
65
  - [Contribute](#contribute)
@@ -276,6 +277,7 @@ Available configuration options are:
276
277
  - `cleanup_preserved_jobs_before_seconds_ago` (integer) number of seconds to preserve jobs when using the `$ good_job cleanup_preserved_jobs` CLI command or calling `GoodJob.cleanup_preserved_jobs`. Defaults to `86400` (1 day). Can also be set with the environment variable `GOOD_JOB_CLEANUP_PRESERVED_JOBS_BEFORE_SECONDS_AGO`. _This configuration is only used when {GoodJob.preserve_job_records} is `true`._
277
278
  - `cleanup_interval_jobs` (integer) Number of jobs a Scheduler will execute before cleaning up preserved jobs. Defaults to `nil`. Can also be set with the environment variable `GOOD_JOB_CLEANUP_INTERVAL_JOBS`.
278
279
  - `cleanup_interval_seconds` (integer) Number of seconds a Scheduler will wait before cleaning up preserved jobs. Defaults to `nil`. Can also be set with the environment variable `GOOD_JOB_CLEANUP_INTERVAL_SECONDS`.
280
+ - `inline_execution_respects_schedule` (boolean) Opt-in to future behavior of inline execution respecting scheduled jobs. Defaults to `false`.
279
281
  - `logger` ([Rails Logger](https://api.rubyonrails.org/classes/ActiveSupport/Logger.html)) lets you set a custom logger for GoodJob. It should be an instance of a Rails `Logger` (Default: `Rails.logger`).
280
282
  - `preserve_job_records` (boolean) keeps job records in your database even after jobs are completed. (Default: `false`)
281
283
  - `retry_on_unhandled_error` (boolean) causes jobs to be re-queued and retried if they raise an instance of `StandardError`. Be advised this may lead to jobs being repeated infinitely ([see below for more on retries](#retries)). Instances of `Exception`, like SIGINT, will *always* be retried, regardless of this attribute’s value. (Default: `true`)
@@ -878,6 +880,23 @@ It is also necessary to destroy these preserved jobs from the database after a c
878
880
  bundle exec good_job cleanup_preserved_jobs --before-seconds-ago=86400
879
881
  ```
880
882
 
883
+ ### Write tests
884
+
885
+ By default, GoodJob uses its inline adapter in the test environment; the inline adapter is designed for the test environment. When enquing a job with GoodJob's inline adapter, the job will be executed immediately on the current thread; unhandled exceptions will be raised.
886
+
887
+ In GoodJob 2.0, the inline adapter will execute future scheduled jobs immediately. In the next major release, GoodJob 3.0, the inline adapter will not execute future scheduled jobs and instead enqueue them in the database.
888
+
889
+ To opt into this behavior immediately set: `config.good_job.inline_execution_respects_schedule = true`
890
+
891
+ To perform jobs inline at any time, use `GoodJob.perform_inline`. For example, using time helpers within an integration test:
892
+
893
+ ```ruby
894
+ MyJob.set(wait: 10.minutes).perform_later
895
+ travel_to(15.minutes.from_now) { GoodJob.perform_inline }
896
+ ```
897
+
898
+ _Note: Rails `travel`/`travel_to` time helpers do not have millisecond precision, so you must leave at least 1 second between the schedule and time traveling for the job to be executed. This [behavior may change in Rails 7.1](https://github.com/rails/rails/pull/44088)._
899
+
881
900
  ### PgBouncer compatibility
882
901
 
883
902
  GoodJob is not compatible with PgBouncer in _transaction_ mode, but is compatible with PgBouncer's _connection_ mode. GoodJob uses connection-based advisory locks and LISTEN/NOTIFY, both of which require full database connections.
@@ -62,19 +62,48 @@ module GoodJob
62
62
  # @param timestamp [Integer, nil] the epoch time to perform the job
63
63
  # @return [GoodJob::Execution]
64
64
  def enqueue_at(active_job, timestamp)
65
+ scheduled_at = timestamp ? Time.zone.at(timestamp) : nil
66
+
67
+ if execute_inline?
68
+ future_scheduled = (scheduled_at.nil? || scheduled_at > Time.current)
69
+ will_execute_inline = !future_scheduled || (future_scheduled && !@configuration.inline_execution_respects_schedule?)
70
+ end
71
+
65
72
  execution = GoodJob::Execution.enqueue(
66
73
  active_job,
67
- scheduled_at: timestamp ? Time.zone.at(timestamp) : nil,
68
- create_with_advisory_lock: execute_inline?
74
+ scheduled_at: scheduled_at,
75
+ create_with_advisory_lock: will_execute_inline
69
76
  )
70
77
 
71
- if execute_inline?
78
+ if will_execute_inline
79
+ if future_scheduled && !@configuration.inline_execution_respects_schedule?
80
+ ActiveSupport::Deprecation.warn(<<~DEPRECATION)
81
+ In the next major release, GoodJob will not *inline* execute
82
+ future-scheduled jobs.
83
+
84
+ To opt into this behavior immediately set:
85
+ `config.good_job.inline_execution_respects_schedule = true`
86
+
87
+ To perform jobs inline at any time, use `GoodJob.perform_inline`.
88
+
89
+ For example, using time helpers within an integration test:
90
+
91
+ ```
92
+ MyJob.set(wait: 10.minutes).perform_later
93
+ travel_to(15.minutes.from_now) { GoodJob.perform_inline }
94
+ ```
95
+
96
+ Note: Rails `travel`/`travel_to` time helpers do not have millisecond
97
+ precision, so you must leave at least 1 second between the schedule
98
+ and time traveling for the job to be executed.
99
+ DEPRECATION
100
+ end
101
+
72
102
  begin
73
103
  result = execution.perform
74
104
  ensure
75
105
  execution.advisory_unlock
76
106
  end
77
-
78
107
  raise result.unhandled_error if result.unhandled_error
79
108
  else
80
109
  job_state = { queue_name: execution.queue_name }
@@ -119,6 +119,10 @@ module GoodJob
119
119
  end
120
120
  end
121
121
 
122
+ def inline_execution_respects_schedule?
123
+ !!rails_config[:inline_execution_respects_schedule]
124
+ end
125
+
122
126
  # The maximum number of future-scheduled jobs to store in memory.
123
127
  # Storing future-scheduled jobs in memory reduces execution latency
124
128
  # at the cost of increased memory usage. 10,000 stored jobs = ~20MB.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  module GoodJob
3
3
  # GoodJob gem version.
4
- VERSION = '2.15.2'
4
+ VERSION = '2.16.0'
5
5
  end
data/lib/good_job.rb CHANGED
@@ -153,6 +153,20 @@ module GoodJob
153
153
  end
154
154
  end
155
155
 
156
+ # Perform all queued jobs in the current thread.
157
+ # This is primarily intended for usage in a test environment.
158
+ # Unhandled job errors will be raised.
159
+ # @param queue_string [String] Queues to execute jobs from
160
+ # @return [void]
161
+ def self.perform_inline(queue_string = "*")
162
+ job_performer = JobPerformer.new(queue_string)
163
+ loop do
164
+ result = job_performer.next
165
+ break unless result
166
+ raise result.unhandled_error if result.unhandled_error
167
+ end
168
+ end
169
+
156
170
  def self._executables
157
171
  [].concat(
158
172
  CronManager.instances,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: good_job
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.15.2
4
+ version: 2.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Sheldon