Class: CollavreSlack::SlackInboundMessageJob
- Inherits:
-
ApplicationJob
- Object
- ApplicationJob
- ApplicationJob
- CollavreSlack::SlackInboundMessageJob
- Defined in:
- app/jobs/collavre_slack/slack_inbound_message_job.rb
Instance Method Summary collapse
Instance Method Details
#perform(payload) ⇒ Object
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 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 |
# File 'app/jobs/collavre_slack/slack_inbound_message_job.rb', line 17 def perform(payload) data = payload.with_indifferent_access creative = Collavre::Creative.find(data[:creative_id]) user = Collavre.user_class.find_by(id: data[:user_id]) channel_link = SlackChannelLink.find_by(id: data[:slack_channel_link_id]) return unless creative && channel_link comment_user = user.presence || channel_link.created_by # Idempotent permission grant / invite writes. Run BEFORE the exactly-once claim so # a transient failure here (e.g. a Solid Queue enqueue deadlock that exhausts the # in-place retries) commits no reservation — a failed-job retry can still reprocess # the message. Each re-checks the existing share/invitation, so a sequential re-run # is a no-op. Retried in place on transient contention — otherwise a Deadlocked here # loses the acked message. with_transient_retry do # Case 1: User exists in Collavre but lacks permission if user && !creative.(user, :feedback) (creative: creative, user: user, granter: channel_link.created_by) Rails.logger.info("[CollavreSlack] Granted feedback permission to user #{user.id} on creative #{creative.id}") end # Case 2: User not in Collavre - invite by email if user.nil? && data[:slack_email].present? invite_user_by_email( creative: creative, email: data[:slack_email], inviter: channel_link.created_by ) Rails.logger.info("[CollavreSlack] Sent invitation to #{data[:slack_email]} for creative #{creative.id}") end end # Exactly-once guard, claimed here: after the idempotent writes above, immediately # before the non-idempotent CommandProcessor (/calendar, /work, MCP). Slack is acked # before this job runs, so a message can arrive twice (sequentially or concurrently); # a read-only "already applied?" check can't cover concurrency — both jobs read "not # applied" and both run the commands. We atomically claim (channel_link, message_ts) # via a unique index, so only the winner runs CommandProcessor and persists. # # Placed after the reads and the idempotent writes so no recoverable failure leaves a # reservation behind: every pre-claim error is either a deterministic RecordNotFound # (deleted creative) or a transient grant/invite failure a retry can redo. Only past # the claim do we reach the non-idempotent commands, so the sole loss window is a # discard/crash between the claim and the comment persisting — intentional, since a # retry there would duplicate the commands. No time-lease (expiry reclaim is fragile). # channel_link is verified present, so a RecordInvalid can only mean "already claimed". return unless (data) # Create comment with appropriate user comment = Collavre::Comment.new( creative: creative, user: comment_user, content: format_comment_content(data[:content], user, data[:slack_display_name]) ) # Mark this comment as coming from Slack to prevent loop comment.instance_variable_set(:@from_slack, true) # Non-idempotent side effects: run once, outside the retryable write below. response = Collavre::Comments::CommandProcessor.new(comment: comment, user: user).call if response.present? comment.content = "#{comment.content}\n\n#{response}" comment.skip_dispatch = true # slash command responses should not trigger AI end persist_comment_with_link!(comment, data) end |