Class: Pgbus::ActiveJob::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/pgbus/active_job/executor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: Pgbus.client, config: Pgbus.configuration, stat_buffer: nil) ⇒ Executor

Returns a new instance of Executor.



10
11
12
13
14
# File 'lib/pgbus/active_job/executor.rb', line 10

def initialize(client: Pgbus.client, config: Pgbus.configuration, stat_buffer: nil)
  @client = client
  @config = config
  @stat_buffer = stat_buffer
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/pgbus/active_job/executor.rb', line 8

def client
  @client
end

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/pgbus/active_job/executor.rb', line 8

def config
  @config
end

Instance Method Details

#execute(message, queue_name, source_queue: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/pgbus/active_job/executor.rb', line 20

def execute(message, queue_name, source_queue: nil)
  execution_start = monotonic_now
  tag = "msg_id=#{message.msg_id} queue=#{queue_name} read_ct=#{message.read_ct}"
  Pgbus.logger.debug { "[Pgbus::Executor] start #{tag}" }

  payload = JSON.parse(message.message)
  job_class = payload["job_class"]
  read_count = message.read_ct.to_i

  if read_count > config.max_retries
    handle_dead_letter(message, queue_name, payload, source_queue: source_queue)
    FailedEventRecorder.clear!(queue_name: queue_name, msg_id: message.msg_id.to_i)
    signal_concurrency(payload)
    signal_batch_discarded(payload)
    Uniqueness.release_lock(Uniqueness.extract_key(payload))
    record_stat(payload, queue_name, "dead_lettered", execution_start, message: message)
    instrument(
      "pgbus.job_dead_lettered",
      queue: queue_name,
      job_class: job_class,
      job_id: payload["job_id"],
      provider_job_id: payload["provider_job_id"],
      read_ct: read_count,
      msg_id: message.msg_id.to_i
    )
    Pgbus.logger.debug { "[Pgbus::Executor] dead_lettered #{tag} job_class=#{job_class}" }
    return :dead_lettered
  end
  uniqueness_key = Uniqueness.extract_key(payload)
  uniqueness_strategy = Uniqueness.extract_strategy(payload)

  if uniqueness_key
    case uniqueness_strategy
    when :until_executed
      # No claim step needed — PGMQ's visibility timeout is the execution lock.
      # The uniqueness key row was inserted at enqueue time and will be
      # released on completion or DLQ.
      nil
    when :while_executing
      # Acquire the lock now, bound to this message. If another worker is
      # already executing this job, skip it — VT will expire and it'll be
      # retried. A row left by a crashed attempt of THIS message is
      # re-acquired, not treated as a duplicate.
      acquired = Uniqueness.acquire_execution_lock(
        uniqueness_key, payload, msg_id: message.msg_id.to_i, queue_name: queue_name
      )
      unless acquired
        Pgbus.logger.info { "[Pgbus] Skipping duplicate execution for #{job_class}" }
        return :skipped
      end
    end
  end

  Pgbus.logger.debug { "[Pgbus::Executor] deserialized #{tag} job_class=#{job_class}" }
  job_succeeded = false
  retried = false

  msg_id = message.msg_id.to_i
  instrument_payload = {
    queue: queue_name,
    job_class: job_class,
    job_id: payload["job_id"],
    provider_job_id: payload["provider_job_id"],
    arguments: payload["arguments"],
    enqueued_at: payload["enqueued_at"],
    read_ct: read_count,
    msg_id: msg_id
  }
  Instrumentation.instrument("pgbus.executor.execute", instrument_payload) do
    # Route through Serializer so allowed_global_id_models gates job
    # arguments the same way it gates EventBus `_global_id` payloads
    # (issue #368). Pass this executor's config so an injected allowlist
    # is not silently ignored in favour of Pgbus.configuration.
    job = Serializer.deserialize_job_data(payload, configuration: config)
    # Batch membership rides the pgbus metadata key, not the serialized
    # job data, so hand it to the job before perform — that is what makes
    # `batch` (and `batch.enqueue` for open batches) work inside a job.
    assign_batch_id(job, payload)
    Pgbus.logger.debug { "[Pgbus::Executor] running #{tag} job_class=#{job_class}" }
    execute_job(job)
    # retry_on re-enqueues from inside perform_now and returns normally:
    # this attempt is done (archive it) but the job is not — the retry
    # message carries the batch tag and signals on its own outcome.
    retried = Batch.retry_reenqueued?(payload["job_id"])
    Pgbus.logger.debug { "[Pgbus::Executor] perform_returned #{tag} job_class=#{job_class}" }
    archive_from(queue_name, msg_id, source_queue: source_queue)
    Pgbus.logger.debug { "[Pgbus::Executor] archived #{tag} job_class=#{job_class}" }
    job_succeeded = true
    release_uniqueness_lock(uniqueness_key)
    FailedEventRecorder.clear!(queue_name: queue_name, msg_id: msg_id)
  end

  instrument("pgbus.job_completed", queue: queue_name, job_class: job_class)
  record_stat(payload, queue_name, "success", execution_start, message: message)
  Pgbus.logger.debug { "[Pgbus::Executor] done #{tag} job_class=#{job_class}" }
  :success
rescue *FATAL_EXCEPTIONS
  # Process-fatal: propagate so the supervisor/OS can react.
  raise
rescue Exception => e # rubocop:disable Lint/RescueException
  # Widened from StandardError to catch Async::Stop / Async::Cancel
  # (both inherit from Exception, not StandardError) under execution_mode: :async.
  # Before this, a fiber interruption between perform_now and archive_from
  # silently lost control flow — no failed event row, no job_failed
  # notification, uniqueness lock held until VT expired. See issue #126.
  handle_failure(message, queue_name, e, payload: payload)
  # A failed :while_executing attempt is no longer executing — release
  # so the retry (same message, after VT) can acquire. :until_executed
  # keeps its lock until success/DLQ by design (#126, #333).
  release_uniqueness_lock(uniqueness_key) if uniqueness_strategy == :while_executing
  instrument(
    "pgbus.job_failed",
    queue: queue_name,
    job_class: payload&.dig("job_class"),
    job_id: payload&.dig("job_id"),
    provider_job_id: payload&.dig("provider_job_id"),
    read_ct: message.read_ct.to_i,
    msg_id: message.msg_id.to_i,
    error: e.class.name,
    exception_object: e
  )
  record_stat(payload, queue_name, "failed", execution_start, message: message)
  Pgbus.logger.debug { "[Pgbus::Executor] failed #{tag} job_class=#{payload&.dig("job_class")} error=#{e.class}" }
  # Don't signal concurrency on transient failure — the job will be retried.
  # Semaphore is released only on success or dead-lettering.
  :failed
ensure
  # Signal concurrency and batch only when the job was archived successfully.
  # job_succeeded is set AFTER archive_message, so if archive fails the
  # semaphore slot stays held until VT expires and the job is retried.
  if job_succeeded
    # Uniqueness is released once, immediately after archive. A second
    # key-only DELETE here can drop a successor that acquired the same
    # key if the first DELETE committed but the client raised afterward.
    signal_concurrency(payload)
    signal_batch_completed(payload) unless retried
  end
  Batch.clear_retry_reenqueued
end