crono_trigger 0.7.1 → 0.8.1
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 +4 -4
- data/lib/crono_trigger/polling_thread.rb +7 -1
- data/lib/crono_trigger/schedulable.rb +7 -2
- data/lib/crono_trigger/version.rb +1 -1
- data/lib/crono_trigger/worker.rb +28 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa5ce10c898d430d072b1a8617b35388228cb76db4a988ab8de6aa53f8315985
|
4
|
+
data.tar.gz: 94fd599e1a3771fe4e04395a4ebffcf35c8d1df67d8c1d40762403a2203089ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d8f22e3e9a869943f18fa8309fe66a886713092026fe6655076a6962e039216c3142572f444a84e0f3fb504dce1e9921e2299508d803eaf4cd1ff447092b831b
|
7
|
+
data.tar.gz: 2ebbdd8c5da440e162a81780cae9adad698871f7b79c6028b6083a55e64bd0df2ecb36317afd88b741166591c99828e1bc23ee1a80cae47021f527edf3b7d029
|
@@ -10,6 +10,7 @@ module CronoTrigger
|
|
10
10
|
end
|
11
11
|
@execution_counter = execution_counter
|
12
12
|
@quiet = Concurrent::AtomicBoolean.new(false)
|
13
|
+
@worker_count = 1
|
13
14
|
end
|
14
15
|
|
15
16
|
def run
|
@@ -58,7 +59,7 @@ module CronoTrigger
|
|
58
59
|
maybe_has_next = true
|
59
60
|
while maybe_has_next && !@stop_flag.set?
|
60
61
|
records, maybe_has_next = model.connection_pool.with_connection do
|
61
|
-
model.executables_with_lock(limit: CronoTrigger.config.fetch_records || CronoTrigger.config.executor_thread * 3)
|
62
|
+
model.executables_with_lock(limit: CronoTrigger.config.fetch_records || CronoTrigger.config.executor_thread * 3, worker_count: @worker_count)
|
62
63
|
end
|
63
64
|
|
64
65
|
records.each do |record|
|
@@ -74,6 +75,11 @@ module CronoTrigger
|
|
74
75
|
end
|
75
76
|
end
|
76
77
|
|
78
|
+
def worker_count=(n)
|
79
|
+
raise ArgumentError, "worker_count must be greater than 0" if n <= 0
|
80
|
+
@worker_count = n
|
81
|
+
end
|
82
|
+
|
77
83
|
private
|
78
84
|
|
79
85
|
def process_record(record)
|
@@ -63,8 +63,11 @@ module CronoTrigger
|
|
63
63
|
end
|
64
64
|
|
65
65
|
module ClassMethods
|
66
|
-
def executables_with_lock(limit: CronoTrigger.config.executor_thread * 3)
|
67
|
-
|
66
|
+
def executables_with_lock(limit: CronoTrigger.config.executor_thread * 3, worker_count: 1)
|
67
|
+
# Fetch more than `limit` records because other workers might have acquired the lock
|
68
|
+
# and this method might not be able to return enough records for the executor to
|
69
|
+
# make the best use of the CPU.
|
70
|
+
ids = executables(limit: limit * worker_count).pluck(:id)
|
68
71
|
maybe_has_next = !ids.empty?
|
69
72
|
records = []
|
70
73
|
ids.each do |id|
|
@@ -75,6 +78,8 @@ module CronoTrigger
|
|
75
78
|
records << r
|
76
79
|
end
|
77
80
|
end
|
81
|
+
|
82
|
+
return [records, maybe_has_next] if records.size == limit
|
78
83
|
end
|
79
84
|
[records, maybe_has_next]
|
80
85
|
end
|
data/lib/crono_trigger/worker.rb
CHANGED
@@ -7,6 +7,7 @@ module CronoTrigger
|
|
7
7
|
HEARTBEAT_INTERVAL = 60
|
8
8
|
SIGNAL_FETCH_INTERVAL = 10
|
9
9
|
MONITOR_INTERVAL = 20
|
10
|
+
WORKER_COUNT_UPDATE_INTERVAL = 60
|
10
11
|
EXECUTOR_SHUTDOWN_TIMELIMIT = 300
|
11
12
|
OTHER_THREAD_SHUTDOWN_TIMELIMIT = 120
|
12
13
|
attr_reader :polling_threads
|
@@ -44,6 +45,8 @@ module CronoTrigger
|
|
44
45
|
@polling_threads = polling_threads
|
45
46
|
@polling_threads.each(&:run)
|
46
47
|
|
48
|
+
@worker_count_updater_thread = run_worker_count_updater_thread
|
49
|
+
|
47
50
|
ServerEngine::SignalThread.new do |st|
|
48
51
|
st.trap(:TSTP) do
|
49
52
|
@logger.info("[worker_id:#{@crono_trigger_worker_id}] Transit to quiet mode")
|
@@ -58,6 +61,7 @@ module CronoTrigger
|
|
58
61
|
@executor.wait_for_termination(EXECUTOR_SHUTDOWN_TIMELIMIT)
|
59
62
|
@heartbeat_thread.join(OTHER_THREAD_SHUTDOWN_TIMELIMIT)
|
60
63
|
@signal_fetcn_thread.join(OTHER_THREAD_SHUTDOWN_TIMELIMIT)
|
64
|
+
@worker_count_updater_thread.join(OTHER_THREAD_SHUTDOWN_TIMELIMIT)
|
61
65
|
|
62
66
|
unregister
|
63
67
|
end
|
@@ -104,6 +108,15 @@ module CronoTrigger
|
|
104
108
|
end
|
105
109
|
end
|
106
110
|
|
111
|
+
def run_worker_count_updater_thread
|
112
|
+
update_worker_count
|
113
|
+
Thread.start do
|
114
|
+
until @stop_flag.wait_for_set(WORKER_COUNT_UPDATE_INTERVAL)
|
115
|
+
update_worker_count
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
107
120
|
def heartbeat
|
108
121
|
CronoTrigger::Models::Worker.connection_pool.with_connection do
|
109
122
|
begin
|
@@ -162,7 +175,7 @@ module CronoTrigger
|
|
162
175
|
return unless ActiveSupport::Notifications.notifier.listening?(CronoTrigger::Events::MONITOR)
|
163
176
|
|
164
177
|
CronoTrigger::Models::Worker.connection_pool.with_connection do
|
165
|
-
if
|
178
|
+
if workers_processing_same_models.order(:worker_id).limit(1).pluck(:worker_id).first != @crono_trigger_worker_id
|
166
179
|
# Return immediately to avoid redundant instruments
|
167
180
|
return
|
168
181
|
end
|
@@ -189,5 +202,19 @@ module CronoTrigger
|
|
189
202
|
rescue => ex
|
190
203
|
CronoTrigger::GlobalExceptionHandler.handle_global_exception(ex)
|
191
204
|
end
|
205
|
+
|
206
|
+
def update_worker_count
|
207
|
+
CronoTrigger::Models::Worker.connection_pool.with_connection do
|
208
|
+
worker_count = workers_processing_same_models.count
|
209
|
+
return if worker_count.zero?
|
210
|
+
@polling_threads.each { |th| th.worker_count = worker_count }
|
211
|
+
end
|
212
|
+
rescue => ex
|
213
|
+
CronoTrigger::GlobalExceptionHandler.handle_global_exception(ex)
|
214
|
+
end
|
215
|
+
|
216
|
+
def workers_processing_same_models
|
217
|
+
CronoTrigger.workers.where("polling_model_names = ?", @model_names.to_json)
|
218
|
+
end
|
192
219
|
end
|
193
220
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: crono_trigger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- joker1007
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chrono
|