pgbus 1.0.0 → 1.1.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.
@@ -34,12 +34,13 @@ module Pgbus
34
34
  Entry = Struct.new(:payload, :opts)
35
35
 
36
36
  # scheduler: responds to `schedule(delay_seconds) { ... }`. Defaults
37
- # to a Concurrent::ScheduledTask-backed scheduler.
37
+ # to `config.streams_coalesce_scheduler`, read when a window's flush
38
+ # is scheduled, else a Concurrent::ScheduledTask-backed timer.
38
39
  # flush: ->(stream_name:, target:, payload:, opts:) called once per
39
40
  # window per key with the latest buffered frame.
40
41
  def initialize(flush:, scheduler: nil)
41
42
  @flush = flush
42
- @scheduler = scheduler || ScheduledTaskScheduler.new
43
+ @scheduler = scheduler || ConfiguredScheduler.new
43
44
  @mutex = Mutex.new
44
45
  @buffer = {} # key => Entry (latest)
45
46
  @pending = {} # key => true while a flush is scheduled
@@ -60,7 +61,19 @@ module Pgbus
60
61
  end
61
62
  end
62
63
 
63
- @scheduler.schedule(window_ms / 1000.0) { flush_key(key, stream_name, target) } if schedule
64
+ return unless schedule
65
+
66
+ begin
67
+ @scheduler.schedule(window_ms / 1000.0) { flush_key(key, stream_name, target) }
68
+ rescue StandardError
69
+ # Nothing will flush this window: free the key, or no later submit
70
+ # would schedule it again.
71
+ @mutex.synchronize do
72
+ @pending.delete(key)
73
+ @buffer.delete(key)
74
+ end
75
+ raise
76
+ end
64
77
  end
65
78
 
66
79
  private
@@ -81,14 +94,37 @@ module Pgbus
81
94
  ErrorReporter.report(e, { component: "streams.coalescer", stream: stream_name, target: target })
82
95
  end
83
96
 
84
- # Default scheduler backed by Concurrent::ScheduledTask. Kept as a
85
- # tiny adapter so the Coalescer can be unit-tested with a synchronous
86
- # fake scheduler (no real timers, no sleeps).
97
+ # The timer: a Concurrent::ScheduledTask on the global IO executor.
98
+ # Kept as a tiny adapter so the Coalescer can be unit-tested with a
99
+ # synchronous fake scheduler (no real timers, no sleeps).
87
100
  class ScheduledTaskScheduler
88
101
  def schedule(delay_seconds, &)
89
102
  Concurrent::ScheduledTask.execute(delay_seconds, &)
90
103
  end
91
104
  end
105
+
106
+ # Runs the flush at once, on the thread that submitted it (issue #694).
107
+ # For tests: the flush then writes on that thread's own connection, with
108
+ # no IO thread sharing a pinned connection across the test boundary.
109
+ # Pgbus::Testing.fake! / inline! select it. It removes the coalescing
110
+ # itself: every submit is broadcast.
111
+ class InlineScheduler
112
+ def schedule(_delay_seconds) = yield
113
+ end
114
+
115
+ # The default: `config.streams_coalesce_scheduler`, read when a window's
116
+ # flush is scheduled (its first submit) so a scheduler set after boot (an
117
+ # initializer, a test mode) takes effect on the process-wide coalescer,
118
+ # else the timer.
119
+ class ConfiguredScheduler
120
+ def initialize
121
+ @timer = ScheduledTaskScheduler.new
122
+ end
123
+
124
+ def schedule(delay_seconds, &)
125
+ (Pgbus.configuration.streams_coalesce_scheduler || @timer).schedule(delay_seconds, &)
126
+ end
127
+ end
92
128
  end
93
129
  end
94
130
  end
data/lib/pgbus/testing.rb CHANGED
@@ -92,6 +92,9 @@ module Pgbus
92
92
  @mode_lock = Mutex.new
93
93
  @default_mode = nil
94
94
 
95
+ # The scheduler fake/inline select for the coalescer (issue #694).
96
+ INLINE_COALESCE_SCHEDULER = Pgbus::Streams::Coalescer::InlineScheduler.new.freeze
97
+
95
98
  class << self
96
99
  def mode!(mode, &block)
97
100
  raise ArgumentError, "Unknown mode: #{mode}. Valid modes: #{MODES.join(", ")}" unless MODES.include?(mode)
@@ -169,6 +172,7 @@ module Pgbus
169
172
  def sync_streams_test_mode!(mode)
170
173
  return unless defined?(Pgbus.configuration)
171
174
 
175
+ sync_coalesce_scheduler!(mode)
172
176
  if mode == :disabled
173
177
  Pgbus.configuration.streams_test_mode = false
174
178
  reset_streamer!
@@ -177,6 +181,21 @@ module Pgbus
177
181
  end
178
182
  end
179
183
 
184
+ # Fake/inline flush coalesced broadcasts on the calling thread (issue
185
+ # #694): the coalescer's timer runs them on a Concurrent IO thread,
186
+ # which could take the test's pinned connection and keep it past the
187
+ # test. Only Testing's own inline scheduler is swapped in and out, so a
188
+ # scheduler the application configured stays as it is.
189
+ def sync_coalesce_scheduler!(mode)
190
+ config = Pgbus.configuration
191
+ current = config.streams_coalesce_scheduler
192
+ if mode == :disabled
193
+ config.streams_coalesce_scheduler = nil if current.equal?(INLINE_COALESCE_SCHEDULER)
194
+ elsif current.nil?
195
+ config.streams_coalesce_scheduler = INLINE_COALESCE_SCHEDULER
196
+ end
197
+ end
198
+
180
199
  # A live streamer at this point is legitimate inside a
181
200
  # `disabled! do ... end` real-stream test, so a clean teardown only
182
201
  # warns. Threads that outlived shutdown! are a different matter: left
data/lib/pgbus/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pgbus
4
- VERSION = "1.0.0"
4
+ VERSION = "1.1.1"
5
5
  end
@@ -22,9 +22,17 @@ module Pgbus
22
22
 
23
23
  # Sent at once inside Pgbus.coalesce (issue #682): a step enqueue that
24
24
  # fails is reported and swept (WorkflowStep#enqueue_step_job), so it
25
- # must not become the scope's on_failure. Internal to pgbus, not an
26
- # opt-out for application jobs.
27
- def self.pgbus_coalesce? = false
25
+ # must not become the scope's on_failure. Included here as well as on
26
+ # ActiveJob::Base, so the setting holds without the engine (issue #689).
27
+ include Pgbus::ActiveJob::Coalesce::JobMixin
28
+
29
+ pgbus_coalesce false
30
+
31
+ # The adapter reads pgbus_coalesce?. Defined on this class, like
32
+ # pgbus_visibility_heartbeat_enabled above, so an app's own
33
+ # pgbus_coalesce? on ActiveJob::Base (the hook before #689) cannot
34
+ # shadow the mixin's reader; it answers what the macro set.
35
+ def self.pgbus_coalesce? = pgbus_coalesce_enabled == true
28
36
 
29
37
  def perform(step_id)
30
38
  StepExecution.new(step_id).call
@@ -43,6 +43,18 @@ module Pgbus
43
43
  end
44
44
  end
45
45
  end
46
+
47
+ module ActiveJob
48
+ class Coalesce
49
+ # `pgbus_coalesce false/true`, included on ActiveJob::Base by the engine (issue #689).
50
+ module JobMixin
51
+ module ClassMethods
52
+ def pgbus_coalesce: (?boolish enabled) -> bool
53
+ def pgbus_coalesce?: () -> bool
54
+ end
55
+ end
56
+ end
57
+ end
46
58
  end
47
59
 
48
60
  module ActiveJob
@@ -32,6 +32,8 @@ module Pgbus
32
32
  def circuit_breaker_enabled=: (untyped) -> untyped
33
33
  def coalesce_chunk_size: () -> Integer
34
34
  def coalesce_chunk_size=: (untyped) -> untyped
35
+ def coalesce_default: () -> bool
36
+ def coalesce_default=: (untyped) -> untyped
35
37
  def completed_jobs: () -> (:archive | :delete)
36
38
  def completed_jobs=: (untyped value) -> (:archive | :delete)
37
39
  def completion_batch_size: () -> Integer
@@ -216,6 +218,8 @@ module Pgbus
216
218
  def streams_authorize=: (untyped) -> untyped
217
219
  def streams_broadcast_queue: () -> untyped
218
220
  def streams_broadcast_queue=: (untyped) -> untyped
221
+ def streams_coalesce_scheduler: () -> untyped
222
+ def streams_coalesce_scheduler=: (untyped) -> untyped
219
223
  def streams_connection_options: () -> untyped
220
224
  def streams_database_url: () -> untyped
221
225
  def streams_database_url=: (untyped) -> untyped
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgbus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikael Henriksson
@@ -347,6 +347,7 @@ files:
347
347
  - lib/pgbus/active_job/adapter.rb
348
348
  - lib/pgbus/active_job/batch_id.rb
349
349
  - lib/pgbus/active_job/coalesce.rb
350
+ - lib/pgbus/active_job/coalesce/job_mixin.rb
350
351
  - lib/pgbus/active_job/coalesce/middleware.rb
351
352
  - lib/pgbus/active_job/current_attributes.rb
352
353
  - lib/pgbus/active_job/delivery.rb
@@ -478,6 +479,7 @@ files:
478
479
  - lib/pgbus/pgmq_schema/pgmq_v1.12.0.sql
479
480
  - lib/pgbus/pgmq_schema/pgmq_v1.13.0.sql
480
481
  - lib/pgbus/pgmq_schema/pgmq_v1.13.1.sql
482
+ - lib/pgbus/pgmq_schema/pgmq_v1.13.2.sql
481
483
  - lib/pgbus/process/claim_buffer.rb
482
484
  - lib/pgbus/process/completer.rb
483
485
  - lib/pgbus/process/consumer.rb