delayed_job_master 3.0.1 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +1 -1
- data/lib/delayed/master/config.rb +1 -1
- data/lib/delayed/master/job.rb +15 -0
- data/lib/delayed/master/job_checker.rb +45 -30
- data/lib/delayed/master/job_finder.rb +26 -8
- data/lib/delayed_job_master/version.rb +1 -1
- data/lib/generators/delayed_job_master/templates/config.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6683f368856e5c22a45b1a58bc11073761ee2214d28b520d5006b3a3c5d573e2
|
4
|
+
data.tar.gz: 19b671f335157e6a4a3ff4ea0c2dde4a0fdb9029f9e8cda013a4d8c27d4b1923
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7e5076e40a1637698209c5980d877fa1f5e25b1bf1913e22944ac9ed2667f9877236716e0c4d15cf2427bf6aecdfa919cc75a2163c1e1ab5901c16f6329b4c1
|
7
|
+
data.tar.gz: f723aaf03678034ac3309f05bb8f5aa88f780aa9b6bc5e829bc2f127d9b249026546409b33354df3de81860ff005cc16e7771a10591989436525de0da736343e
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -16,7 +16,9 @@ module Delayed
|
|
16
16
|
@databases = master.databases
|
17
17
|
@callbacks = master.callbacks
|
18
18
|
@queues = @databases.map { |database| [database, Queue.new] }.to_h
|
19
|
-
@threads =
|
19
|
+
@threads = SafeArray.new
|
20
|
+
@timer_threads = SafeArray.new
|
21
|
+
@job_finder = JobFinder.new(master)
|
20
22
|
end
|
21
23
|
|
22
24
|
def start
|
@@ -53,6 +55,15 @@ module Delayed
|
|
53
55
|
end
|
54
56
|
end
|
55
57
|
|
58
|
+
def start_timer_thread(database, run_at)
|
59
|
+
@timer_threads << Thread.new(database, run_at) do |database, run_at|
|
60
|
+
interval = run_at.to_f - Time.zone.now.to_f
|
61
|
+
sleep interval if interval > 0
|
62
|
+
schedule(database)
|
63
|
+
@timer_threads.delete(Thread.current)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
56
67
|
def stop
|
57
68
|
@databases.each do |database|
|
58
69
|
queue = @queues[database]
|
@@ -70,56 +81,60 @@ module Delayed
|
|
70
81
|
|
71
82
|
def wait
|
72
83
|
@threads.each(&:join)
|
84
|
+
@timer_threads.each(&:join)
|
73
85
|
end
|
74
86
|
|
75
87
|
def shutdown
|
76
88
|
@threads.each(&:kill)
|
89
|
+
@timer_threads.each(&:kill)
|
77
90
|
end
|
78
91
|
|
79
92
|
private
|
80
93
|
|
81
94
|
def check(database)
|
82
|
-
free_settings = detect_free_settings(database)
|
83
|
-
return if free_settings.blank?
|
84
|
-
|
85
95
|
@master.logger.debug { "checking jobs @#{database.spec_name}..." }
|
86
|
-
|
87
|
-
|
88
|
-
@master.logger.info { "found jobs for #{settings.uniq.map(&:worker_info).join(', ')}" }
|
89
|
-
fork_workers(database, settings)
|
90
|
-
end
|
96
|
+
check_jobs(database)
|
97
|
+
check_recent_jobs(database)
|
91
98
|
rescue => e
|
92
99
|
@master.logger.warn { "#{e.class}: #{e.message}" }
|
93
100
|
@master.logger.debug { e.backtrace.join("\n") }
|
94
101
|
end
|
95
102
|
|
96
|
-
def
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
103
|
+
def check_jobs(database)
|
104
|
+
jobs = find_jobs(database)
|
105
|
+
jobs.each do |job|
|
106
|
+
@master.logger.info { "found jobs @#{job.database.spec_name} for #{job.setting.worker_info}" }
|
107
|
+
fork_worker(job.database, job.setting)
|
101
108
|
end
|
102
109
|
end
|
103
110
|
|
104
|
-
def
|
105
|
-
|
111
|
+
def find_jobs(database)
|
112
|
+
free_settings = count_free_settings
|
113
|
+
free_settings.map do |setting, free_count|
|
114
|
+
@job_finder.ready_jobs(database, setting, free_count)
|
115
|
+
end.flatten
|
116
|
+
end
|
106
117
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
118
|
+
def count_free_settings
|
119
|
+
@config.worker_settings.map do |setting|
|
120
|
+
used_count = @master.workers.count { |worker| worker.setting == setting }
|
121
|
+
free_count = setting.max_processes - used_count
|
122
|
+
[setting, free_count] if free_count > 0
|
123
|
+
end.compact.to_h
|
124
|
+
end
|
125
|
+
|
126
|
+
def fork_worker(database, setting)
|
127
|
+
worker = Worker.new(database: database, setting: setting)
|
128
|
+
Forker.new(@master).call(worker)
|
129
|
+
@master.workers << worker
|
130
|
+
@master.monitoring.schedule(worker)
|
115
131
|
end
|
116
132
|
|
117
|
-
def
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
@master.monitoring.schedule(worker)
|
133
|
+
def check_recent_jobs(database)
|
134
|
+
jobs = @job_finder.recent_jobs(database)
|
135
|
+
jobs.each do |job|
|
136
|
+
@master.logger.info { "set timer to #{job.run_at.iso8601(6)} @#{job.database.spec_name}" }
|
137
|
+
start_timer_thread(job.database, job.run_at)
|
123
138
|
end
|
124
139
|
end
|
125
140
|
end
|
@@ -1,31 +1,49 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'job'
|
4
|
+
|
3
5
|
# JobFinder runs SQL query which is almost same as delayed_job_active_record.
|
4
6
|
# See https://github.com/collectiveidea/delayed_job_active_record/blob/master/lib/delayed/backend/active_record.rb
|
5
7
|
module Delayed
|
6
8
|
module Master
|
7
9
|
class JobFinder
|
8
|
-
def initialize(
|
9
|
-
@
|
10
|
+
def initialize(master)
|
11
|
+
@config = master.config
|
10
12
|
end
|
11
13
|
|
12
|
-
def
|
13
|
-
|
14
|
+
def ready_jobs(database, setting, limit)
|
15
|
+
ready_scope(database, setting).limit(limit).pluck(:id, :run_at).map do |id, run_at|
|
16
|
+
Job.new(database: database, setting: setting, id: id, run_at: run_at)
|
17
|
+
end
|
14
18
|
end
|
15
19
|
|
16
|
-
def
|
17
|
-
|
20
|
+
def recent_jobs(database)
|
21
|
+
recent_scope(database).order(:run_at).limit(1).pluck(:id, :run_at).map do |id, run_at|
|
22
|
+
Job.new(database: database, id: id, run_at: run_at)
|
23
|
+
end
|
18
24
|
end
|
19
25
|
|
20
26
|
private
|
21
27
|
|
22
|
-
def
|
23
|
-
|
28
|
+
def ready_scope(database, setting)
|
29
|
+
model = database.model
|
30
|
+
model.where("(run_at <= ? AND (locked_at IS NULL OR locked_at < ?)) AND failed_at IS NULL",
|
31
|
+
model.db_time_now,
|
32
|
+
model.db_time_now - (setting.max_run_time || Delayed::Worker::DEFAULT_MAX_RUN_TIME)
|
33
|
+
).tap do |jobs|
|
24
34
|
jobs.where!("priority >= ?", setting.min_priority) if setting.min_priority
|
25
35
|
jobs.where!("priority <= ?", setting.max_priority) if setting.max_priority
|
26
36
|
jobs.where!(queue: setting.queues) if setting.queues.present?
|
27
37
|
end
|
28
38
|
end
|
39
|
+
|
40
|
+
def recent_scope(database)
|
41
|
+
model = database.model
|
42
|
+
model.where("run_at > ? AND run_at < ? AND locked_at IS NULL AND failed_at IS NULL",
|
43
|
+
model.db_time_now,
|
44
|
+
model.db_time_now + @config.polling_interval,
|
45
|
+
)
|
46
|
+
end
|
29
47
|
end
|
30
48
|
end
|
31
49
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delayed_job_master
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yoshikazu Kaneta
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-06-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: delayed_job
|
@@ -192,6 +192,7 @@ files:
|
|
192
192
|
- lib/delayed/master/database.rb
|
193
193
|
- lib/delayed/master/file_reopener.rb
|
194
194
|
- lib/delayed/master/forker.rb
|
195
|
+
- lib/delayed/master/job.rb
|
195
196
|
- lib/delayed/master/job_checker.rb
|
196
197
|
- lib/delayed/master/job_finder.rb
|
197
198
|
- lib/delayed/master/job_listener.rb
|