diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-05-02 21:18:39 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-05-02 21:18:39 +0300 |
commit | b9ce0fe1e6311105b7a748126621f9bfbe37fb2e (patch) | |
tree | c73b711a72de036cf3f48be9365038fea171c8c6 /app/workers | |
parent | 6f991190fe4dbb93070b090a9a31d71b25e8101d (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/workers')
-rw-r--r-- | app/workers/all_queues.yml | 9 | ||||
-rw-r--r-- | app/workers/integrations/slack_event_worker.rb | 54 |
2 files changed, 63 insertions, 0 deletions
diff --git a/app/workers/all_queues.yml b/app/workers/all_queues.yml index 0143cb4c93d..78ab6b5909b 100644 --- a/app/workers/all_queues.yml +++ b/app/workers/all_queues.yml @@ -2757,6 +2757,15 @@ :weight: 1 :idempotent: false :tags: [] +- :name: integrations_slack_event + :worker_name: Integrations::SlackEventWorker + :feature_category: :integrations + :has_external_dependencies: true + :urgency: :low + :resource_boundary: :unknown + :weight: 1 + :idempotent: true + :tags: [] - :name: invalid_gpg_signature_update :worker_name: InvalidGpgSignatureUpdateWorker :feature_category: :source_code_management diff --git a/app/workers/integrations/slack_event_worker.rb b/app/workers/integrations/slack_event_worker.rb new file mode 100644 index 00000000000..e5cdae1beea --- /dev/null +++ b/app/workers/integrations/slack_event_worker.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +module Integrations + class SlackEventWorker + include ApplicationWorker + + EVENTS = { + 'app_home_opened' => SlackEvents::AppHomeOpenedService + }.freeze + + feature_category :integrations + data_consistency :delayed + urgency :low + deduplicate :until_executed + idempotent! + worker_has_external_dependencies! + + def self.event?(slack_event) + EVENTS.key?(slack_event) + end + + def perform(args) + args = args.with_indifferent_access + + log_extra_metadata_on_done(:slack_event, args[:slack_event]) + log_extra_metadata_on_done(:slack_user_id, args.dig(:params, :event, :user)) + log_extra_metadata_on_done(: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 + end +end |