crono_trigger 0.7.1 → 0.8.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: 9713612dde3158cacbad3f58c85b0a0935d4e67b687d372c2c5537fcd4407b0c
4
- data.tar.gz: b2f053b4fe4f268cccb40c7dddbca0c4c2deeba8b119c656d50548a541a5e812
3
+ metadata.gz: 734da2ad0480e79b440d8ef73f9b83fb88f13e6f6510513340c62b70c3750c39
4
+ data.tar.gz: b338b9c86a9fdf01d0ba739fa9f53c9536393cec05d7fe5c8943fe05782ad7b4
5
5
  SHA512:
6
- metadata.gz: 66a6ff6c554f59ea9448c72d735f872c6ead66da77bbaf7377baadc91c4c233c38ca0ec906dcbb4589678ce676c121fe6e31b6517f836417c1e82d61e67a012f
7
- data.tar.gz: 2583529dca847b25bc79c3778c1130e9fd4078ebd8734b8019e04618c341dd1febb0f457a17554a4d41bde0dcd4e3ac9ccdd95fcad90f34b3bfc063ebab76d39
6
+ metadata.gz: 6e73b97cece87a4b887e4cec1c5cbd153cc9ad51c3de1956c38ef2a5000fa74c0d3e52c07401c0aa033f0921852a2f6be85533aea050d425dc220c591681a210
7
+ data.tar.gz: dfbec13afaa581a544b50481fa54509ace24de39f8ecb6d99788e508bd680da1e5afd89abeb0cde7be8ad8db3c7c4b56ae67dbdf0bd087775eaeed62b92d8cac
@@ -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
- ids = executables(limit: limit).pluck(:id)
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
@@ -1,3 +1,3 @@
1
1
  module CronoTrigger
2
- VERSION = "0.7.1"
2
+ VERSION = "0.8.0"
3
3
  end
@@ -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
@@ -37,6 +38,7 @@ module CronoTrigger
37
38
  @heartbeat_thread = run_heartbeat_thread
38
39
  @signal_fetcn_thread = run_signal_fetch_thread
39
40
  @monitor_thread = run_monitor_thread
41
+ @worker_count_updater_thread = run_worker_count_updater_thread
40
42
 
41
43
  polling_thread_count = CronoTrigger.config.polling_thread || [@model_names.size, Concurrent.processor_count].min
42
44
  # Assign local variable for Signal handling
@@ -58,6 +60,7 @@ module CronoTrigger
58
60
  @executor.wait_for_termination(EXECUTOR_SHUTDOWN_TIMELIMIT)
59
61
  @heartbeat_thread.join(OTHER_THREAD_SHUTDOWN_TIMELIMIT)
60
62
  @signal_fetcn_thread.join(OTHER_THREAD_SHUTDOWN_TIMELIMIT)
63
+ @worker_count_updater_thread.join(OTHER_THREAD_SHUTDOWN_TIMELIMIT)
61
64
 
62
65
  unregister
63
66
  end
@@ -104,6 +107,15 @@ module CronoTrigger
104
107
  end
105
108
  end
106
109
 
110
+ def run_worker_count_updater_thread
111
+ update_worker_count
112
+ Thread.start do
113
+ until @stop_flag.wait_for_set(WORKER_COUNT_UPDATE_INTERVAL)
114
+ update_worker_count
115
+ end
116
+ end
117
+ end
118
+
107
119
  def heartbeat
108
120
  CronoTrigger::Models::Worker.connection_pool.with_connection do
109
121
  begin
@@ -162,7 +174,7 @@ module CronoTrigger
162
174
  return unless ActiveSupport::Notifications.notifier.listening?(CronoTrigger::Events::MONITOR)
163
175
 
164
176
  CronoTrigger::Models::Worker.connection_pool.with_connection do
165
- if CronoTrigger.workers.where("polling_model_names = ?", @model_names.to_json).order(:worker_id).limit(1).pluck(:worker_id).first != @crono_trigger_worker_id
177
+ if workers_processing_same_models.order(:worker_id).limit(1).pluck(:worker_id).first != @crono_trigger_worker_id
166
178
  # Return immediately to avoid redundant instruments
167
179
  return
168
180
  end
@@ -189,5 +201,19 @@ module CronoTrigger
189
201
  rescue => ex
190
202
  CronoTrigger::GlobalExceptionHandler.handle_global_exception(ex)
191
203
  end
204
+
205
+ def update_worker_count
206
+ CronoTrigger::Models::Worker.connection_pool.with_connection do
207
+ worker_count = workers_processing_same_models.count
208
+ return if worker_count.zero?
209
+ @polling_threads.each { |th| th.worker_count = worker_count }
210
+ end
211
+ rescue => ex
212
+ CronoTrigger::GlobalExceptionHandler.handle_global_exception(ex)
213
+ end
214
+
215
+ def workers_processing_same_models
216
+ CronoTrigger.workers.where("polling_model_names = ?", @model_names.to_json)
217
+ end
192
218
  end
193
219
  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.7.1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - joker1007
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-16 00:00:00.000000000 Z
11
+ date: 2024-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chrono