good_job 1.12.1 → 1.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b4629010561604c4f1d77812c3ca7301e5600fa8e0637fd584a769898d43aa5
4
- data.tar.gz: 1216b9d590c40c047fa25c13afec94021891752bab8ec36ff7f6c968af8e285e
3
+ metadata.gz: ef04aa133c3e3f39b4e5967f61441617808d51a8d13b79e9f4e61a89c3da0c63
4
+ data.tar.gz: a5abe5ff64d1c7abb197b78f0aa7812834b9034fb0340dbb8abb6d2a42912af9
5
5
  SHA512:
6
- metadata.gz: ef51328facf01deeec6a2e67c38c526c944d1c7381fa45cf97f7f05aa4897ac711c324f616e8d948c3c5e6e4e5fae636ea2c7c3c9d48d79de0ead78ed438580f
7
- data.tar.gz: e4efa15858ac7a6c0849ec084a00de8fbb8f6751523e78c9234b118bee2c84f3709c549cfc1f161af5166b2d54ff1f358b2dbd18e54adca06d6c62ad10757827
6
+ metadata.gz: 6effeb75fd0d5a0541f4be984e6a0931fba2d6849b1d5061941276b5ee977b3620ce13b2328b64fdd27b1d66ab1753087aed612d7c301b7a100fc709a3740697
7
+ data.tar.gz: 4694a54a3f1dffd84e4875fc7169e49f6edff3baa92fc458720939738b3db6b6f907883601c480fd4aa1d64549b7a1a0352b3c2481df1ae67057685efa9414a6
data/CHANGELOG.md CHANGED
@@ -1,5 +1,52 @@
1
1
  # Changelog
2
2
 
3
+ ## [v1.13.2](https://github.com/bensheldon/good_job/tree/v1.13.2) (2021-08-18)
4
+
5
+ [Full Changelog](https://github.com/bensheldon/good_job/compare/v1.13.1...v1.13.2)
6
+
7
+ **Merged pull requests:**
8
+
9
+ - Add deprecation notice that `async` mode will be renamed `async_all` in GoodJob v2.0 [\#339](https://github.com/bensheldon/good_job/pull/339) ([bensheldon](https://github.com/bensheldon))
10
+
11
+ ## [v1.13.1](https://github.com/bensheldon/good_job/tree/v1.13.1) (2021-08-18)
12
+
13
+ [Full Changelog](https://github.com/bensheldon/good_job/compare/v1.13.0...v1.13.1)
14
+
15
+ **Fixed bugs:**
16
+
17
+ - Don’t attempt to enforce concurrency limits with other queue adapters [\#333](https://github.com/bensheldon/good_job/pull/333) ([codyrobbins](https://github.com/codyrobbins))
18
+
19
+ ## [v1.13.0](https://github.com/bensheldon/good_job/tree/v1.13.0) (2021-08-18)
20
+
21
+ [Full Changelog](https://github.com/bensheldon/good_job/compare/v1.12.2...v1.13.0)
22
+
23
+ **Implemented enhancements:**
24
+
25
+ - Track if a GoodJob::Job has been subsequently retried [\#331](https://github.com/bensheldon/good_job/pull/331) ([bensheldon](https://github.com/bensheldon))
26
+ - Wrap and truncate error message, which can be a huge text [\#294](https://github.com/bensheldon/good_job/pull/294) ([morgoth](https://github.com/morgoth))
27
+
28
+ **Closed issues:**
29
+
30
+ - Add hyphen to lock string. e.g. "table\_name-column" instead of "table\_namecolumn [\#334](https://github.com/bensheldon/good_job/issues/334)
31
+ - Optimize db indexes in advance of v2.0.0 [\#332](https://github.com/bensheldon/good_job/issues/332)
32
+ - wait\_until in development? [\#330](https://github.com/bensheldon/good_job/issues/330)
33
+ - Race conditions in ActiveJob concurrency extension [\#325](https://github.com/bensheldon/good_job/issues/325)
34
+ - Store in database if a job has been ActiveJob retried [\#321](https://github.com/bensheldon/good_job/issues/321)
35
+ - Revisit and embrace concurrency control, scheduled jobs, and other extensions of ActiveJob [\#255](https://github.com/bensheldon/good_job/issues/255)
36
+ - Why 1 million jobs per day? [\#222](https://github.com/bensheldon/good_job/issues/222)
37
+
38
+ ## [v1.12.2](https://github.com/bensheldon/good_job/tree/v1.12.2) (2021-08-13)
39
+
40
+ [Full Changelog](https://github.com/bensheldon/good_job/compare/v1.12.1...v1.12.2)
41
+
42
+ **Fixed bugs:**
43
+
44
+ - Fixes for race conditions in ActiveJob concurrency extension [\#326](https://github.com/bensheldon/good_job/pull/326) ([codyrobbins](https://github.com/codyrobbins))
45
+
46
+ **Merged pull requests:**
47
+
48
+ - On gem release, add instructions to author a Github Release [\#324](https://github.com/bensheldon/good_job/pull/324) ([bensheldon](https://github.com/bensheldon))
49
+
3
50
  ## [v1.12.1](https://github.com/bensheldon/good_job/tree/v1.12.1) (2021-08-05)
4
51
 
5
52
  [Full Changelog](https://github.com/bensheldon/good_job/compare/v1.12.0...v1.12.1)
@@ -29,7 +76,7 @@
29
76
  - Add the ability to schedule repeating / recurring / cron-like jobs [\#53](https://github.com/bensheldon/good_job/issues/53)
30
77
  - Add cron-like support for recurring/repeating jobs [\#297](https://github.com/bensheldon/good_job/pull/297) ([bensheldon](https://github.com/bensheldon))
31
78
 
32
- **Merged pull requests:**
79
+ **Fixed bugs:**
33
80
 
34
81
  - Place Dashboard shared view partials under `good_job` namespace [\#310](https://github.com/bensheldon/good_job/pull/310) ([bensheldon](https://github.com/bensheldon))
35
82
  - Ensure Dashboard inline javascript has CSP nonce for strict Content-Security Policy [\#309](https://github.com/bensheldon/good_job/pull/309) ([bensheldon](https://github.com/bensheldon))
data/README.md CHANGED
@@ -10,7 +10,7 @@ GoodJob is a multithreaded, Postgres-based, ActiveJob backend for Ruby on Rails.
10
10
  - **Designed for ActiveJob.** Complete support for [async, queues, delays, priorities, timeouts, and retries](https://edgeguides.rubyonrails.org/active_job_basics.html) with near-zero configuration.
11
11
  - **Built for Rails.** Fully adopts Ruby on Rails [threading and code execution guidelines](https://guides.rubyonrails.org/threading_and_code_execution.html) with [Concurrent::Ruby](https://github.com/ruby-concurrency/concurrent-ruby).
12
12
  - **Backed by Postgres.** Relies upon Postgres integrity, session-level Advisory Locks to provide run-once safety and stay within the limits of `schema.rb`, and LISTEN/NOTIFY to reduce queuing latency.
13
- - **For most workloads.** Targets full-stack teams, economy-minded solo developers, and applications that enqueue less than 1-million jobs/day.
13
+ - **For most workloads.** Targets full-stack teams, economy-minded solo developers, and applications that enqueue 1-million jobs/day and more.
14
14
 
15
15
  For more of the story of GoodJob, read the [introductory blog post](https://island94.org/2020/07/introducing-goodjob-1-0).
16
16
 
@@ -19,7 +19,7 @@
19
19
  <td><%= job.serialized_params['job_class'] %></td>
20
20
  <td><%= job.queue_name %></td>
21
21
  <td><%= job.scheduled_at || job.created_at %></td>
22
- <td><%= job.error %></td>
22
+ <td class="text-break"><%= truncate(job.error, length: 1_000) %></td>
23
23
  <td>
24
24
  <%= tag.button "Preview", type: "button", class: "btn btn-sm btn-outline-primary", role: "button",
25
25
  data: {bs_toggle: "collapse", bs_target: "##{dom_id(job, 'params')}"},
@@ -16,6 +16,7 @@ class CreateGoodJobs < ActiveRecord::Migration[5.2]
16
16
  t.uuid :active_job_id
17
17
  t.text :concurrency_key
18
18
  t.text :cron_key
19
+ t.uuid :retried_good_job_id
19
20
  end
20
21
 
21
22
  add_index :good_jobs, :scheduled_at, where: "(finished_at IS NULL)", name: "index_good_jobs_on_scheduled_at"
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ class AddRetriedGoodJobIdToGoodJobs < ActiveRecord::Migration[5.2]
3
+ def change
4
+ reversible do |dir|
5
+ dir.up do
6
+ # Ensure this incremental update migration is idempotent
7
+ # with monolithic install migration.
8
+ return if connection.column_exists?(:good_jobs, :retried_good_job_id)
9
+ end
10
+ end
11
+
12
+ add_column :good_jobs, :retried_good_job_id, :uuid
13
+ end
14
+ end
@@ -9,21 +9,24 @@ module GoodJob
9
9
  included do
10
10
  class_attribute :good_job_concurrency_config, instance_accessor: false, default: {}
11
11
 
12
- before_enqueue do |job|
12
+ around_enqueue do |job, block|
13
+ # Don't attempt to enforce concurrency limits with other queue adapters.
14
+ next(block.call) unless job.class.queue_adapter.is_a?(GoodJob::Adapter)
15
+
13
16
  # Always allow jobs to be retried because the current job's execution will complete momentarily
14
- next if CurrentExecution.active_job_id == job.job_id
17
+ next(block.call) if CurrentExecution.active_job_id == job.job_id
15
18
 
16
19
  limit = job.class.good_job_concurrency_config.fetch(:enqueue_limit, Float::INFINITY)
17
- next if limit.blank? || (0...Float::INFINITY).exclude?(limit)
20
+ next(block.call) if limit.blank? || (0...Float::INFINITY).exclude?(limit)
18
21
 
19
22
  key = job.good_job_concurrency_key
20
- next if key.blank?
23
+ next(block.call) if key.blank?
21
24
 
22
25
  GoodJob::Job.new.with_advisory_lock(key: key, function: "pg_advisory_lock") do
23
26
  # TODO: Why is `unscoped` necessary? Nested scope is bleeding into subsequent query?
24
27
  enqueue_concurrency = GoodJob::Job.unscoped.where(concurrency_key: key).unfinished.count
25
28
  # The job has not yet been enqueued, so check if adding it will go over the limit
26
- throw :abort if enqueue_concurrency + 1 > limit
29
+ block.call unless enqueue_concurrency + 1 > limit
27
30
  end
28
31
  end
29
32
 
@@ -34,6 +37,9 @@ module GoodJob
34
37
  )
35
38
 
36
39
  before_perform do |job|
40
+ # Don't attempt to enforce concurrency limits with other queue adapters.
41
+ next unless job.class.queue_adapter.is_a?(GoodJob::Adapter)
42
+
37
43
  limit = job.class.good_job_concurrency_config.fetch(:perform_limit, Float::INFINITY)
38
44
  next if limit.blank? || (0...Float::INFINITY).exclude?(limit)
39
45
 
@@ -41,9 +47,9 @@ module GoodJob
41
47
  next if key.blank?
42
48
 
43
49
  GoodJob::Job.new.with_advisory_lock(key: key, function: "pg_advisory_lock") do
44
- perform_concurrency = GoodJob::Job.unscoped.where(concurrency_key: key).advisory_locked.count
50
+ allowed_active_job_ids = GoodJob::Job.unscoped.where(concurrency_key: key).advisory_locked.order(Arel.sql("COALESCE(performed_at, scheduled_at, created_at) ASC")).limit(limit).pluck(:active_job_id)
45
51
  # The current job has already been locked and will appear in the previous query
46
- raise GoodJob::ActiveJobExtensions::Concurrency::ConcurrencyExceededError if perform_concurrency > limit
52
+ raise GoodJob::ActiveJobExtensions::Concurrency::ConcurrencyExceededError unless allowed_active_job_ids.include? job.job_id
47
53
  end
48
54
  end
49
55
  end
@@ -130,7 +130,7 @@ module GoodJob
130
130
  # Whether in +:async+ execution mode.
131
131
  # @return [Boolean]
132
132
  def execute_async?
133
- @configuration.execution_mode == :async ||
133
+ @configuration.execution_mode.in?([:async, :async_all]) ||
134
134
  @configuration.execution_mode == :async_server && in_server_process?
135
135
  end
136
136
 
@@ -7,7 +7,7 @@ module GoodJob
7
7
  #
8
8
  class Configuration
9
9
  # Valid execution modes.
10
- EXECUTION_MODES = [:async, :async_server, :external, :inline].freeze
10
+ EXECUTION_MODES = [:async, :async_all, :async_server, :external, :inline].freeze
11
11
  # Default number of threads to use per {Scheduler}
12
12
  DEFAULT_MAX_THREADS = 5
13
13
  # Default number of seconds between polls for jobs
@@ -58,7 +58,18 @@ module GoodJob
58
58
  end
59
59
 
60
60
  if mode
61
- mode.to_sym
61
+ mode_sym = mode.to_sym
62
+ if mode_sym == :async
63
+ ActiveSupport::Deprecation.warn <<~DEPRECATION
64
+ The next major version of GoodJob will redefine the meaning of 'async'
65
+ execution mode to be equivalent to 'async_server' and only execute
66
+ within the webserver process.
67
+
68
+ To continue using the v1.0 semantics of 'async', use `async_all` instead.
69
+
70
+ DEPRECATION
71
+ end
72
+ mode_sym
62
73
  elsif Rails.env.development? || Rails.env.test?
63
74
  :inline
64
75
  else
@@ -5,12 +5,6 @@ module GoodJob
5
5
  # Thread-local attributes for passing values from Instrumentation.
6
6
  # (Cannot use ActiveSupport::CurrentAttributes because ActiveJob resets it)
7
7
  module CurrentExecution
8
- # @!attribute [rw] active_job_id
9
- # @!scope class
10
- # ActiveJob ID
11
- # @return [String, nil]
12
- thread_mattr_accessor :active_job_id
13
-
14
8
  # @!attribute [rw] cron_key
15
9
  # @!scope class
16
10
  # Cron Key
@@ -29,14 +23,26 @@ module GoodJob
29
23
  # @return [Exception, nil]
30
24
  thread_mattr_accessor :error_on_retry
31
25
 
26
+ # @!attribute [rw] good_job
27
+ # @!scope class
28
+ # Cron Key
29
+ # @return [GoodJob::Job, nil]
30
+ thread_mattr_accessor :good_job
31
+
32
32
  # Resets attributes
33
33
  # @return [void]
34
34
  def self.reset
35
- self.active_job_id = nil
35
+ self.cron_key = nil
36
+ self.good_job = nil
36
37
  self.error_on_discard = nil
37
38
  self.error_on_retry = nil
38
39
  end
39
40
 
41
+ # @return [String] UUID of the currently executing GoodJob::Job
42
+ def self.active_job_id
43
+ good_job&.active_job_id
44
+ end
45
+
40
46
  # @return [Integer] Current process ID
41
47
  def self.process_id
42
48
  Process.pid
data/lib/good_job/job.rb CHANGED
@@ -52,6 +52,20 @@ module GoodJob
52
52
  end
53
53
  end
54
54
 
55
+ def self._migration_pending_warning
56
+ ActiveSupport::Deprecation.warn(<<~DEPRECATION)
57
+ GoodJob has pending database migrations. To create the migration files, run:
58
+
59
+ rails generate good_job:update
60
+
61
+ To apply the migration files, run:
62
+
63
+ rails db:migrate
64
+
65
+ DEPRECATION
66
+ nil
67
+ end
68
+
55
69
  # Get Jobs with given class name
56
70
  # @!method with_job_class
57
71
  # @!scope class
@@ -110,6 +124,18 @@ module GoodJob
110
124
  # @return [ActiveRecord::Relation]
111
125
  scope :running, -> { where.not(performed_at: nil).where(finished_at: nil) }
112
126
 
127
+ # Get Jobs that do not have subsequent retries
128
+ # @!method running
129
+ # @!scope class
130
+ # @return [ActiveRecord::Relation]
131
+ scope :head, -> { where(retried_good_job_id: nil) }
132
+
133
+ # Get Jobs have errored that will not be retried further
134
+ # @!method running
135
+ # @!scope class
136
+ # @return [ActiveRecord::Relation]
137
+ scope :dead, -> { head.where.not(error: nil) }
138
+
113
139
  # Get Jobs on queues that match the given queue string.
114
140
  # @!method queue_string(string)
115
141
  # @!scope class
@@ -199,7 +225,6 @@ module GoodJob
199
225
  def self.enqueue(active_job, scheduled_at: nil, create_with_advisory_lock: false)
200
226
  ActiveSupport::Notifications.instrument("enqueue_job.good_job", { active_job: active_job, scheduled_at: scheduled_at, create_with_advisory_lock: create_with_advisory_lock }) do |instrument_payload|
201
227
  good_job_args = {
202
- cron_key: CurrentExecution.cron_key,
203
228
  queue_name: active_job.queue_name.presence || DEFAULT_QUEUE_NAME,
204
229
  priority: active_job.priority || DEFAULT_PRIORITY,
205
230
  serialized_params: active_job.serialize,
@@ -210,31 +235,23 @@ module GoodJob
210
235
  if column_names.include?('active_job_id')
211
236
  good_job_args[:active_job_id] = active_job.job_id
212
237
  else
213
- ActiveSupport::Deprecation.warn(<<~DEPRECATION)
214
- GoodJob has pending database migrations. To create the migration files, run:
215
-
216
- rails generate good_job:update
217
-
218
- To apply the migration files, run:
219
-
220
- rails db:migrate
221
-
222
- DEPRECATION
238
+ _migration_pending_warning
223
239
  end
224
240
 
225
241
  if column_names.include?('concurrency_key')
226
242
  good_job_args[:concurrency_key] = active_job.good_job_concurrency_key if active_job.respond_to?(:good_job_concurrency_key)
227
243
  else
228
- ActiveSupport::Deprecation.warn(<<~DEPRECATION)
229
- GoodJob has pending database migrations. To create the migration files, run:
230
-
231
- rails generate good_job:update
232
-
233
- To apply the migration files, run:
234
-
235
- rails db:migrate
244
+ _migration_pending_warning
245
+ end
236
246
 
237
- DEPRECATION
247
+ if column_names.include?('cron_key')
248
+ if CurrentExecution.cron_key
249
+ good_job_args[:cron_key] = CurrentExecution.cron_key
250
+ elsif CurrentExecution.active_job_id == active_job.job_id
251
+ good_job_args[:cron_key] = CurrentExecution.good_job.cron_key
252
+ end
253
+ else
254
+ _migration_pending_warning
238
255
  end
239
256
 
240
257
  good_job = GoodJob::Job.new(**good_job_args)
@@ -244,6 +261,12 @@ module GoodJob
244
261
  good_job.save!
245
262
  active_job.provider_job_id = good_job.id
246
263
 
264
+ if column_names.include?('retried_good_job_id')
265
+ CurrentExecution.good_job.retried_good_job_id = good_job.id if CurrentExecution.good_job && CurrentExecution.good_job.active_job_id == active_job.job_id
266
+ else
267
+ _migration_pending_warning
268
+ end
269
+
247
270
  good_job
248
271
  end
249
272
  end
@@ -283,24 +306,19 @@ module GoodJob
283
306
  end
284
307
 
285
308
  def active_job_id
286
- super || serialized_params['job_id']
309
+ if self.class.column_names.include?('active_job_id')
310
+ super
311
+ else
312
+ self.class._migration_pending_warning
313
+ serialized_params['job_id']
314
+ end
287
315
  end
288
316
 
289
317
  def cron_key
290
318
  if self.class.column_names.include?('cron_key')
291
319
  super
292
320
  else
293
- ActiveSupport::Deprecation.warn(<<~DEPRECATION)
294
- GoodJob has pending database migrations. To create the migration files, run:
295
-
296
- rails generate good_job:update
297
-
298
- To apply the migration files, run:
299
-
300
- rails db:migrate
301
-
302
- DEPRECATION
303
-
321
+ self.class._migration_pending_warning
304
322
  nil
305
323
  end
306
324
  end
@@ -314,8 +332,7 @@ module GoodJob
314
332
  )
315
333
 
316
334
  GoodJob::CurrentExecution.reset
317
- GoodJob::CurrentExecution.active_job_id = active_job_id
318
- GoodJob::CurrentExecution.cron_key = cron_key
335
+ GoodJob::CurrentExecution.good_job = self
319
336
  ActiveSupport::Notifications.instrument("perform_job.good_job", { good_job: self, process_id: GoodJob::CurrentExecution.process_id, thread_name: GoodJob::CurrentExecution.thread_name }) do
320
337
  value = ActiveJob::Base.execute(params)
321
338
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  module GoodJob
3
3
  # GoodJob gem version.
4
- VERSION = '1.12.1'
4
+ VERSION = '1.13.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: good_job
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.12.1
4
+ version: 1.13.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Sheldon
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-05 00:00:00.000000000 Z
11
+ date: 2021-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob
@@ -374,6 +374,7 @@ files:
374
374
  - lib/generators/good_job/templates/update/migrations/01_create_good_jobs.rb
375
375
  - lib/generators/good_job/templates/update/migrations/02_add_active_job_id_concurrency_key_cron_key_to_good_jobs.rb
376
376
  - lib/generators/good_job/templates/update/migrations/03_add_active_job_id_index_and_concurrency_key_index_to_good_jobs.rb
377
+ - lib/generators/good_job/templates/update/migrations/04_add_retried_good_job_id_to_good_jobs.rb
377
378
  - lib/generators/good_job/update_generator.rb
378
379
  - lib/good_job.rb
379
380
  - lib/good_job/active_job_extensions.rb