inst-jobs 2.4.11 → 3.0.0

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: 207f92edc1bf044fe040fac47e1eb3eff01d8d11ff66f3a5fa3ff3429ffca748
4
- data.tar.gz: 0aa8bb0e7c81fe22eca78171b6827ba84f325e2981cda075691072f4305a355d
3
+ metadata.gz: 8a9b37f7e43ec441a00056cbacce6f749e93de4496f4cbdff2c765015056ac71
4
+ data.tar.gz: ccbb0ed96932e436e109db2ae5c3076ce64245bc98a04424f5f9858f045d7a81
5
5
  SHA512:
6
- metadata.gz: d9278910b958ca2854c7b3d5e10a0b3911ee14938067897abb166929db2888f8577d8507622e372b12a5e5db53fbb09d50d5bdcea591364ca5b40ab7a46d1a27
7
- data.tar.gz: 9b4b76b471d442e102a217d16d426dc0eed0e3e2a9bded47cc5d8505806264f189629cea337d084f0300398b96c52f9036a6670aa4b5ef426fff9f56775001a0
6
+ metadata.gz: 560e60386a4ea4e2851b3c6304ab38982134be7141c6d84671259f744c1d1fbb2dbb7af099ecbe2606fe35364b0d20dd54a6a6dccc6cea95ed02dc56c840d8d7
7
+ data.tar.gz: 9aceb6dc65083dc71a7f360d6a7e3fafbb8992f17b9a080e53e190d5a7772b417ef7caecd05f3c6f9fe8462e7ea9f67f788e9423d45dd251979547e3329924a0
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UpdateConflictingSingletonFunctionToUseIndex < ActiveRecord::Migration[5.2]
4
+ def up
5
+ execute(<<~SQL)
6
+ CREATE OR REPLACE FUNCTION delayed_jobs_before_unlock_delete_conflicting_singletons_row_fn () RETURNS trigger AS $$
7
+ BEGIN
8
+ DELETE FROM delayed_jobs WHERE id<>OLD.id AND singleton=OLD.singleton AND locked_by IS NULL;
9
+ RETURN NEW;
10
+ END;
11
+ $$ LANGUAGE plpgsql;
12
+ SQL
13
+ end
14
+
15
+ def down
16
+ execute(<<~SQL)
17
+ CREATE OR REPLACE FUNCTION delayed_jobs_before_unlock_delete_conflicting_singletons_row_fn () RETURNS trigger AS $$
18
+ BEGIN
19
+ IF EXISTS (SELECT 1 FROM delayed_jobs j2 WHERE j2.singleton=OLD.singleton) THEN
20
+ DELETE FROM delayed_jobs WHERE id<>OLD.id AND singleton=OLD.singleton;
21
+ END IF;
22
+ RETURN NEW;
23
+ END;
24
+ $$ LANGUAGE plpgsql;
25
+ SQL
26
+ end
27
+ end
data/exe/inst_jobs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- require File.expand_path("config/environment")
4
+ require_relative "config/environment"
5
5
  Delayed::CLI.new.run
@@ -62,14 +62,9 @@ module Delayed
62
62
  _write_attribute(column, current_time) unless attribute_present?(column)
63
63
  end
64
64
 
65
- if Rails.version >= "6"
66
- attribute_names = attribute_names_for_partial_writes
67
- attribute_names = attributes_for_create(attribute_names)
68
- values = attributes_with_values(attribute_names)
69
- else
70
- attribute_names = partial_writes? ? keys_for_partial_write : self.attribute_names
71
- values = attributes_with_values_for_create(attribute_names)
72
- end
65
+ attribute_names = attribute_names_for_partial_writes
66
+ attribute_names = attributes_for_create(attribute_names)
67
+ values = attributes_with_values(attribute_names)
73
68
 
74
69
  im = self.class.arel_table.compile_insert(self.class.send(:_substitute_values, values))
75
70
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Delayed
4
- VERSION = "2.4.11"
4
+ VERSION = "3.0.0"
5
5
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "activerecord-pg-extensions"
4
+
3
5
  module Delayed
4
6
  module WorkQueue
5
7
  class ParentProcess
@@ -192,29 +194,54 @@ module Delayed
192
194
  end
193
195
  end
194
196
 
195
- def unlock_timed_out_prefetched_jobs
197
+ def unlock_prefetched_jobs
196
198
  @prefetched_jobs.each do |(worker_config, jobs)|
197
199
  next if jobs.empty?
198
- next unless jobs.first.locked_at < Time.now.utc - Settings.parent_process[:prefetched_jobs_timeout]
200
+ next if block_given? && !yield(jobs)
199
201
 
200
- Delayed::Job.transaction do
202
+ connection = Delayed::Job.connection
203
+ connection.transaction do
204
+ # make absolutely sure we don't get hung up and leave things
205
+ # locked in the database
206
+ if connection.postgresql_version >= 9_06_00 # rubocop:disable Style/NumericLiterals
207
+ connection.idle_in_transaction_session_timeout = 5
208
+ end
209
+ # relatively short timeout for acquiring the lock
210
+ connection.statement_timeout = Settings.sleep_delay
201
211
  Delayed::Job.advisory_lock(Delayed::Job.prefetch_jobs_lock_name)
212
+
213
+ # this query might take longer, and we really want to get it
214
+ # done if we got the lock, but still don't want an inadvertent
215
+ # hang
216
+ connection.statement_timeout = 30
202
217
  Delayed::Job.unlock(jobs)
218
+ @prefetched_jobs[worker_config] = []
203
219
  end
204
- @prefetched_jobs[worker_config] = []
220
+ rescue ActiveRecord::QueryCanceled
221
+ # ignore; we'll retry anyway
222
+ logger.warn("unable to unlock prefetched jobs; skipping for now")
223
+ rescue ActiveRecord::StatementInvalid
224
+ # see if we dropped the connection
225
+ raise if connection.active?
226
+
227
+ # otherwise just reconnect and let it retry
228
+ logger.warn("failed to unlock prefetched jobs - connection terminated; skipping for now")
229
+ Delayed::Job.clear_all_connections!
205
230
  end
206
231
  end
207
232
 
208
- def unlock_all_prefetched_jobs
209
- @prefetched_jobs.each do |(_worker_config, jobs)|
210
- next if jobs.empty?
233
+ def unlock_timed_out_prefetched_jobs
234
+ unlock_prefetched_jobs do |jobs|
235
+ jobs.first.locked_at < Time.now.utc - Settings.parent_process[:prefetched_jobs_timeout]
236
+ end
237
+ end
211
238
 
212
- Delayed::Job.transaction do
213
- Delayed::Job.advisory_lock(Delayed::Job.prefetch_jobs_lock_name)
214
- Delayed::Job.unlock(jobs)
215
- end
239
+ def unlock_all_prefetched_jobs
240
+ # we try really hard; it may not have done any work if it timed out
241
+ 10.times do
242
+ unlock_prefetched_jobs
243
+ break if @prefetched_jobs.each_value.all?(&:empty?)
216
244
  end
217
- @prefetched_jobs = {}
218
245
  end
219
246
 
220
247
  def drop_socket(socket)
@@ -104,6 +104,53 @@ RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
104
104
  expect(Marshal.load(client)).to eq(job2)
105
105
  end
106
106
 
107
+ context "prefetched job unlocking" do
108
+ let(:job_args) do
109
+ [["worker_name1"], "queue_name", 1, 2,
110
+ { prefetch: 4, prefetch_owner: "prefetch:work_queue:X", forced_latency: 6.0 }]
111
+ end
112
+ let(:job2) { Delayed::Job.new(tag: "tag").tap { |j| j.create_and_lock!("prefetch:work_queue:X") } }
113
+ let(:job3) { Delayed::Job.new(tag: "tag").tap { |j| j.create_and_lock!("prefetch:work_queue:X") } }
114
+
115
+ before do
116
+ client = Socket.unix(subject.listen_socket.local_address.unix_path)
117
+ subject.run_once
118
+
119
+ jobs = { "worker_name1" => job, "prefetch:work_queue:X" => [job2, job3] }
120
+ allow(subject).to receive(:prefetch_owner).and_return("prefetch:work_queue:X")
121
+ allow(Delayed::Job).to receive(:get_and_lock_next_available).once.with(*job_args).and_return(jobs)
122
+ Marshal.dump(["worker_name1", worker_config], client)
123
+ subject.run_once
124
+ end
125
+
126
+ it "doesn't unlock anything if nothing is timed out" do
127
+ expect(Delayed::Job).not_to receive(:advisory_lock)
128
+ expect(Delayed::Job).not_to receive(:unlock)
129
+ subject.unlock_timed_out_prefetched_jobs
130
+ end
131
+
132
+ it "unlocks timed out prefetched jobs" do
133
+ allow(Delayed::Settings).to receive(:parent_process).and_return(prefetched_jobs_timeout: -1)
134
+ expect(Delayed::Job).to receive(:unlock).with([job2, job3])
135
+ subject.unlock_timed_out_prefetched_jobs
136
+ expect(subject.instance_variable_get(:@prefetched_jobs).values.sum(&:length)).to eq 0
137
+ end
138
+
139
+ it "fails gracefully if the lock times out" do
140
+ allow(Delayed::Settings).to receive(:parent_process).and_return(prefetched_jobs_timeout: -1)
141
+ expect(Delayed::Job).not_to receive(:unlock)
142
+ expect(Delayed::Job).to receive(:advisory_lock).and_raise(ActiveRecord::QueryCanceled)
143
+ subject.unlock_timed_out_prefetched_jobs
144
+ expect(subject.instance_variable_get(:@prefetched_jobs).values.sum(&:length)).to eq 2
145
+ end
146
+
147
+ it "unlocks all jobs" do
148
+ expect(Delayed::Job).to receive(:unlock).with([job2, job3])
149
+ subject.unlock_all_prefetched_jobs
150
+ expect(subject.instance_variable_get(:@prefetched_jobs).values.sum(&:length)).to eq 0
151
+ end
152
+ end
153
+
107
154
  it "doesn't respond immediately if there are no jobs available" do
108
155
  client = Socket.unix(subject.listen_socket.local_address.unix_path)
109
156
  subject.run_once
data/spec/spec_helper.rb CHANGED
@@ -58,11 +58,7 @@ connection_config = {
58
58
  }
59
59
 
60
60
  def migrate(file)
61
- if ::Rails.version >= "6"
62
- ActiveRecord::MigrationContext.new(file, ActiveRecord::SchemaMigration).migrate
63
- else
64
- ActiveRecord::MigrationContext.new(file).migrate
65
- end
61
+ ActiveRecord::MigrationContext.new(file, ActiveRecord::SchemaMigration).migrate
66
62
  end
67
63
 
68
64
  # create the test db if it does not exist, to help out wwtd
@@ -73,6 +69,11 @@ rescue ActiveRecord::StatementInvalid
73
69
  nil
74
70
  end
75
71
  ActiveRecord::Base.establish_connection(connection_config)
72
+
73
+ # we need to ensure this callback is called for activerecord-pg-extensions,
74
+ # which isn't running because we're not using Rails to setup the database
75
+ ActiveRecord::PGExtensions::Railtie.run_initializers
76
+
76
77
  # TODO: reset db and migrate again, to test migrations
77
78
 
78
79
  migrate("db/migrate")
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inst-jobs
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.11
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Luetke
8
8
  - Brian Palmer
9
- autorequire:
9
+ autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2023-02-22 00:00:00.000000000 Z
12
+ date: 2021-09-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -17,28 +17,42 @@ dependencies:
17
17
  requirements:
18
18
  - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: '5.2'
20
+ version: '6.0'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - ">="
26
26
  - !ruby/object:Gem::Version
27
- version: '5.2'
27
+ version: '6.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: activerecord-pg-extensions
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '0.4'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '0.4'
28
42
  - !ruby/object:Gem::Dependency
29
43
  name: activesupport
30
44
  requirement: !ruby/object:Gem::Requirement
31
45
  requirements:
32
46
  - - ">="
33
47
  - !ruby/object:Gem::Version
34
- version: '5.2'
48
+ version: '6.0'
35
49
  type: :runtime
36
50
  prerelease: false
37
51
  version_requirements: !ruby/object:Gem::Requirement
38
52
  requirements:
39
53
  - - ">="
40
54
  - !ruby/object:Gem::Version
41
- version: '5.2'
55
+ version: '6.0'
42
56
  - !ruby/object:Gem::Dependency
43
57
  name: after_transaction_commit
44
58
  requirement: !ruby/object:Gem::Requirement
@@ -93,14 +107,14 @@ dependencies:
93
107
  requirements:
94
108
  - - ">="
95
109
  - !ruby/object:Gem::Version
96
- version: '5.2'
110
+ version: '6.0'
97
111
  type: :runtime
98
112
  prerelease: false
99
113
  version_requirements: !ruby/object:Gem::Requirement
100
114
  requirements:
101
115
  - - ">="
102
116
  - !ruby/object:Gem::Version
103
- version: '5.2'
117
+ version: '6.0'
104
118
  - !ruby/object:Gem::Dependency
105
119
  name: appraisal
106
120
  requirement: !ruby/object:Gem::Requirement
@@ -395,7 +409,7 @@ dependencies:
395
409
  - - "~>"
396
410
  - !ruby/object:Gem::Version
397
411
  version: 1.4.0
398
- description:
412
+ description:
399
413
  email:
400
414
  - brianp@instructure.com
401
415
  executables:
@@ -433,6 +447,7 @@ files:
433
447
  - db/migrate/20210812210128_add_singleton_column.rb
434
448
  - db/migrate/20210917232626_add_delete_conflicting_singletons_before_unlock_trigger.rb
435
449
  - db/migrate/20210928174754_fix_singleton_condition_in_before_insert.rb
450
+ - db/migrate/20210929204903_update_conflicting_singleton_function_to_use_index.rb
436
451
  - exe/inst_jobs
437
452
  - lib/delayed/backend/active_record.rb
438
453
  - lib/delayed/backend/base.rb
@@ -498,7 +513,7 @@ files:
498
513
  homepage: https://github.com/instructure/inst-jobs
499
514
  licenses: []
500
515
  metadata: {}
501
- post_install_message:
516
+ post_install_message:
502
517
  rdoc_options: []
503
518
  require_paths:
504
519
  - lib
@@ -513,8 +528,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
513
528
  - !ruby/object:Gem::Version
514
529
  version: '0'
515
530
  requirements: []
516
- rubygems_version: 3.1.6
517
- signing_key:
531
+ rubygems_version: 3.2.24
532
+ signing_key:
518
533
  specification_version: 4
519
534
  summary: Instructure-maintained fork of delayed_job
520
535
  test_files: