Class: Jobs::RedeliverWebHookEvents

Inherits:
Scheduled show all
Defined in:
app/jobs/scheduled/redeliver_web_hook_events.rb

Constant Summary collapse

REDELIVERED =
"redelivered"
REDELIVERY_FAILED =
"redelivery_failed"
LIMIT =
20

Instance Method Summary collapse

Methods inherited from Scheduled

#perform

Methods inherited from Base

acquire_cluster_concurrency_lock!, clear_cluster_concurrency_lock!, cluster_concurrency, cluster_concurrency_redis_key, delayed_perform, #error_context, get_cluster_concurrency, #last_db_duration, #log, #perform, #perform_immediately

Instance Method Details

#execute(args) ⇒ Object



16
17
18
19
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
# File 'app/jobs/scheduled/redeliver_web_hook_events.rb', line 16

def execute(args)
  redelivery_events =
    RedeliveringWebhookEvent
      .where(processing: false)
      .includes(web_hook_event: :web_hook)
      .limit(LIMIT)
  event_ids = redelivery_events.pluck(:id)
  redelivery_events.update_all(processing: true)
  updated_redelivery_events = RedeliveringWebhookEvent.where(id: event_ids)

  updated_redelivery_events.each do |redelivery_event|
    begin
      web_hook_event = redelivery_event.web_hook_event
      web_hook = web_hook_event.web_hook

      emitter = WebHookEmitter.new(web_hook, web_hook_event)
      emitter.emit!(
        headers: MultiJson.load(web_hook_event.headers),
        body: web_hook_event.payload,
      )

      publish_webhook_event(web_hook_event, web_hook, REDELIVERED)
      RedeliveringWebhookEvent.delete(redelivery_event)
    rescue => e
      Discourse.warn_exception(
        e,
        message: "Error redelivering web_hook_event #{web_hook_event.id}",
      )
      publish_webhook_event(web_hook_event, web_hook, REDELIVERY_FAILED)
      RedeliveringWebhookEvent.delete(redelivery_event)
    end

    sleep 2
  end
end