pgbus 0.27.2 → 1.1.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.
@@ -11,6 +11,28 @@ module Pgbus
11
11
 
12
12
  attr_reader :process_entry
13
13
 
14
+ # The pgbus_processes row this OS process heartbeats under (issue
15
+ # #683): a batch built here records it as its owner, and the sweep
16
+ # reaps the build only once that row is gone or its heartbeat is older
17
+ # than ALIVE_THRESHOLD. [pid, id], so a forked child never reports its
18
+ # parent's row; the last registration wins.
19
+ @owner_lock = Mutex.new
20
+ @owner = nil
21
+
22
+ class << self
23
+ def current_process_id
24
+ @owner_lock.synchronize { @owner[1] if @owner && @owner[0] == ::Process.pid }
25
+ end
26
+
27
+ def note_registered(id)
28
+ @owner_lock.synchronize { @owner = [::Process.pid, id].freeze }
29
+ end
30
+
31
+ def note_deregistered(id)
32
+ @owner_lock.synchronize { @owner = nil if @owner && @owner[1] == id }
33
+ end
34
+ end
35
+
14
36
  # config: the owning process's configuration, for the app revision.
15
37
  def initialize(kind:, metadata: {}, on_beat: nil, loop_tick_supplier: nil, metadata_supplier: nil,
16
38
  config: Pgbus.configuration)
@@ -93,6 +115,9 @@ module Pgbus
93
115
  return if ProcessEntry.where(id: @process_id).update_all(updates).positive? || @stopped
94
116
 
95
117
  old_id = @process_id
118
+ # A batch built while this process re-registers must not name the
119
+ # row that is gone (issue #683).
120
+ self.class.note_deregistered(old_id)
96
121
  Pgbus.logger.warn do
97
122
  "[Pgbus] Process row id=#{old_id} kind=#{@kind} pid=#{::Process.pid} is gone " \
98
123
  "(stale-process reaper, manual cleanup, or clock skew?) — re-registering"
@@ -110,6 +135,7 @@ module Pgbus
110
135
  last_heartbeat_at: Time.current
111
136
  )
112
137
  @process_id = record.id
138
+ self.class.note_registered(@process_id)
113
139
  rescue StandardError => e
114
140
  Pgbus.logger.warn { "[Pgbus] Process registration failed: #{e.message}" }
115
141
  end
@@ -117,6 +143,7 @@ module Pgbus
117
143
  def deregister_process
118
144
  return unless @process_id
119
145
 
146
+ self.class.note_deregistered(@process_id)
120
147
  ProcessEntry.where(id: @process_id).delete_all
121
148
  rescue StandardError => e
122
149
  Pgbus.logger.warn { "[Pgbus] Process deregistration failed: #{e.message}" }
data/lib/pgbus/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pgbus
4
- VERSION = "0.27.2"
4
+ VERSION = "1.1.0"
5
5
  end
@@ -20,6 +20,20 @@ module Pgbus
20
20
  # this, and a redelivery means a dead worker only while it is true.
21
21
  def self.pgbus_visibility_heartbeat_enabled = true # rubocop:disable Naming/PredicateMethod -- JobMixin's reader
22
22
 
23
+ # Sent at once inside Pgbus.coalesce (issue #682): a step enqueue that
24
+ # fails is reported and swept (WorkflowStep#enqueue_step_job), so it
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
36
+
23
37
  def perform(step_id)
24
38
  StepExecution.new(step_id).call
25
39
  end
data/lib/pgbus.rb CHANGED
@@ -317,6 +317,21 @@ module Pgbus
317
317
  Batch.start(**, &)
318
318
  end
319
319
 
320
+ # Hold the block's enqueues and send them together when it ends (issue
321
+ # #682): one send_batch per (queue, priority, delay) group instead of one send
322
+ # per perform_later. See Pgbus::ActiveJob::Coalesce for what is held, the
323
+ # optimistic return value and the failure modes. on_failure: :raise
324
+ # (default) raises Pgbus::EnqueueError for a job that could not be sent,
325
+ # :report reports it, a callable receives (jobs, error).
326
+ #
327
+ # Pgbus.coalesce do
328
+ # EventJob.perform_later("a")
329
+ # EventJob.perform_later("b")
330
+ # end # one send here
331
+ def coalesce(on_failure: :raise, &)
332
+ Pgbus::ActiveJob::Coalesce.open(on_failure: on_failure, &)
333
+ end
334
+
320
335
  # Publish an event to the bus — the top-level shortcut for
321
336
  # `Pgbus::EventBus::Publisher.publish`, symmetric with `Pgbus.stream`.
322
337
  #
@@ -5,6 +5,8 @@ module Pgbus
5
5
  def enqueue: (untyped active_job) -> untyped
6
6
  def enqueue_at: (untyped active_job, Numeric timestamp) -> untyped
7
7
  def enqueue_all: (Array[untyped] active_jobs) -> Integer
8
+ # A Coalesce scope's buffer, as [job, payload serialized at perform_later, due_at] entries (issues #682, #686).
9
+ def deliver_coalesced: (Array[[untyped, Hash[String, untyped], Numeric?]] entries, ?counted: bool) -> Coalesce::Result
8
10
  end
9
11
  end
10
12
 
@@ -41,6 +43,18 @@ module Pgbus
41
43
  end
42
44
  end
43
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
44
58
  end
45
59
 
46
60
  module ActiveJob
@@ -0,0 +1,54 @@
1
+ module Pgbus
2
+ module ActiveJob
3
+ # A unit of work's enqueues, held at the adapter and sent together when
4
+ # the unit ends (issue #682). Opened by Pgbus.coalesce and Middleware.
5
+ class Coalesce
6
+ interface _OnFailure
7
+ def call: (Array[untyped] jobs, Pgbus::EnqueueError error) -> void
8
+ end
9
+
10
+ type on_failure = :raise | :report | _OnFailure
11
+
12
+ interface _CountIn
13
+ def call: (Array[Hash[String, untyped]] payloads, final: bool) -> void
14
+ end
15
+
16
+ interface _OnFailed
17
+ def call: (Array[untyped] jobs) -> void
18
+ end
19
+
20
+ KEY: Symbol
21
+ FAILURE_MODES: Array[Symbol]
22
+
23
+ class Result
24
+ attr_reader buffered: Integer
25
+ attr_reader sent: Integer
26
+ attr_reader retried: Integer
27
+ attr_reader failed: Array[untyped]
28
+ attr_reader error: StandardError?
29
+
30
+ def initialize: (buffered: Integer, sent: Integer, retried: Integer, failed: Array[untyped], error: StandardError?) -> void
31
+ end
32
+
33
+ def self.current: () -> Coalesce?
34
+ # batch_id: (Pgbus.batch, issue #683) a scope of the batch's own that holds only its members;
35
+ # count_in.call(payloads, final:) counts each chunk in before it is sent, the final one closing the build.
36
+ def self.open: [T] (?on_failure: on_failure, ?batch_id: String?, ?count_in: _CountIn?, ?on_failed: _OnFailed?) { () -> T } -> T
37
+ def self.validate_on_failure!: (untyped on_failure) -> void
38
+
39
+ attr_reader on_failure: on_failure
40
+ attr_reader batch_id: String?
41
+
42
+ def initialize: (?on_failure: on_failure, ?batch_id: String?, ?count_in: _CountIn?, ?on_failed: _OnFailed?, ?chunk_size: Integer) -> void
43
+ def push: (Adapter adapter, untyped job, Hash[String, untyped] payload, ?Numeric? due_at) -> bool
44
+ def finish!: (?block_error: Exception?) -> Result
45
+
46
+ # Runs the request inside Pgbus.coalesce; the buffer is sent before the
47
+ # response tuple is returned.
48
+ class Middleware
49
+ def initialize: (untyped app, ?on_failure: Coalesce::on_failure) -> void
50
+ def call: (Hash[String, untyped] env) -> untyped
51
+ end
52
+ end
53
+ end
54
+ end
@@ -30,6 +30,10 @@ module Pgbus
30
30
  def capsule_named: (untyped name) -> untyped
31
31
  def circuit_breaker_enabled: () -> bool
32
32
  def circuit_breaker_enabled=: (untyped) -> untyped
33
+ def coalesce_chunk_size: () -> Integer
34
+ def coalesce_chunk_size=: (untyped) -> untyped
35
+ def coalesce_default: () -> bool
36
+ def coalesce_default=: (untyped) -> untyped
33
37
  def completed_jobs: () -> (:archive | :delete)
34
38
  def completed_jobs=: (untyped value) -> (:archive | :delete)
35
39
  def completion_batch_size: () -> Integer
data/sig/pgbus.rbs CHANGED
@@ -119,6 +119,9 @@ module Pgbus
119
119
  def self.stream_name_budget: () -> Integer
120
120
  # Keywords go to Pgbus::Batch.new (description:, properties:, on_complete:, uniqueness_key:, …).
121
121
  def self.batch: (**untyped options) { (Batch::Builder) -> void } -> Batch
122
+ # Holds the block's bulk-eligible enqueues and sends them together when it
123
+ # ends (issue #682); on_failure: :raise | :report | ^(jobs, error) -> void.
124
+ def self.coalesce: [T] (?on_failure: ActiveJob::Coalesce::on_failure) { () -> T } -> T
122
125
  # delay: seconds, as a Numeric or an ActiveSupport::Duration (1.hour).
123
126
  def self.publish: (String routing_key, untyped payload, ?headers: Hash[String, untyped]?, ?delay: Numeric | _ToI) -> untyped
124
127
  def self.publish_later: (String routing_key, untyped payload, delay: Numeric | _ToI, ?headers: Hash[String, 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: 0.27.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikael Henriksson
@@ -285,6 +285,7 @@ files:
285
285
  - lib/active_job/queue_adapters/pgbus_adapter.rb
286
286
  - lib/generators/pgbus/add_batch_callback_jobs_generator.rb
287
287
  - lib/generators/pgbus/add_batch_executions_generator.rb
288
+ - lib/generators/pgbus/add_batch_owner_generator.rb
288
289
  - lib/generators/pgbus/add_deploy_task_run_outcomes_generator.rb
289
290
  - lib/generators/pgbus/add_deploy_tasks_generator.rb
290
291
  - lib/generators/pgbus/add_failed_events_index_generator.rb
@@ -309,6 +310,7 @@ files:
309
310
  - lib/generators/pgbus/migration_path.rb
310
311
  - lib/generators/pgbus/templates/add_batch_callback_jobs.rb.erb
311
312
  - lib/generators/pgbus/templates/add_batch_executions.rb.erb
313
+ - lib/generators/pgbus/templates/add_batch_owner.rb.erb
312
314
  - lib/generators/pgbus/templates/add_deploy_task_run_outcomes.rb.erb
313
315
  - lib/generators/pgbus/templates/add_deploy_tasks_tables.rb.erb
314
316
  - lib/generators/pgbus/templates/add_failed_events_source_queue.rb.erb
@@ -344,6 +346,9 @@ files:
344
346
  - lib/pgbus.rb
345
347
  - lib/pgbus/active_job/adapter.rb
346
348
  - lib/pgbus/active_job/batch_id.rb
349
+ - lib/pgbus/active_job/coalesce.rb
350
+ - lib/pgbus/active_job/coalesce/job_mixin.rb
351
+ - lib/pgbus/active_job/coalesce/middleware.rb
347
352
  - lib/pgbus/active_job/current_attributes.rb
348
353
  - lib/pgbus/active_job/delivery.rb
349
354
  - lib/pgbus/active_job/deploy_task_run_id.rb
@@ -474,6 +479,7 @@ files:
474
479
  - lib/pgbus/pgmq_schema/pgmq_v1.12.0.sql
475
480
  - lib/pgbus/pgmq_schema/pgmq_v1.13.0.sql
476
481
  - lib/pgbus/pgmq_schema/pgmq_v1.13.1.sql
482
+ - lib/pgbus/pgmq_schema/pgmq_v1.13.2.sql
477
483
  - lib/pgbus/process/claim_buffer.rb
478
484
  - lib/pgbus/process/completer.rb
479
485
  - lib/pgbus/process/consumer.rb
@@ -632,6 +638,7 @@ files:
632
638
  - lib/tasks/pgbus_streams.rake
633
639
  - sig/pgbus.rbs
634
640
  - sig/pgbus/active_job/adapter.rbs
641
+ - sig/pgbus/active_job/coalesce.rbs
635
642
  - sig/pgbus/active_job/quiet_logs.rbs
636
643
  - sig/pgbus/client.rbs
637
644
  - sig/pgbus/configuration.rbs