Class: Integrations::SlackEventWorker

Inherits:
Object
  • Object
show all
Includes:
ApplicationWorker
Defined in:
app/workers/integrations/slack_event_worker.rb

Constant Summary collapse

EVENTS =
{
  'app_home_opened' => SlackEvents::AppHomeOpenedService
}.freeze

Constants included from ApplicationWorker

ApplicationWorker::LOGGING_EXTRA_KEY, ApplicationWorker::SAFE_PUSH_BULK_LIMIT

Constants included from Gitlab::Loggable

Gitlab::Loggable::ANONYMOUS

Constants included from WorkerAttributes

WorkerAttributes::DEFAULT_DATA_CONSISTENCY, WorkerAttributes::DEFAULT_DEFER_DELAY, WorkerAttributes::NAMESPACE_WEIGHTS, WorkerAttributes::VALID_DATA_CONSISTENCIES, WorkerAttributes::VALID_RESOURCE_BOUNDARIES, WorkerAttributes::VALID_URGENCIES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Loggable

#build_structured_payload

Methods included from Gitlab::SidekiqVersioning::Worker

#job_version

Methods included from WorkerContext

#with_context

Class Method Details

.event?(slack_event) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'app/workers/integrations/slack_event_worker.rb', line 18

def self.event?(slack_event)
  EVENTS.key?(slack_event)
end

Instance Method Details

#perform(args) ⇒ Object



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
# File 'app/workers/integrations/slack_event_worker.rb', line 22

def perform(args)
  args = args.with_indifferent_access

  (:slack_event, args[:slack_event])
  (:slack_user_id, args.dig(:params, :event, :user))
  (:slack_workspace_id, args.dig(:params, :team_id))

  unless self.class.event?(args[:slack_event])
    Sidekiq.logger.error(
      message: 'Unknown slack_event',
      slack_event: args[:slack_event]
    )

    return
  end

  # Ensure idempotency by taking out an exclusive lease keyed to `params.event_id`.
  # The `event_id` is "a unique identifier for this specific event, globally unique
  # across all workspaces" and guaranteed to be present as part of the Slack event JSON schema.
  # See https://api.slack.com/types/event.
  lease = Gitlab::ExclusiveLease.new("slack_event:#{args[:params][:event_id]}", timeout: 1.hour.to_i)
  return unless lease.try_obtain

  service_class = EVENTS[args[:slack_event]]
  response = service_class.new(args[:params]).execute

  lease.cancel if response.error?
rescue StandardError => e
  lease.cancel
  raise e
end