Class: CommandTower::Workflows::Messaging::Execution::DeliverWorkflow

Inherits:
ApplicationWorkflow show all
Defined in:
app/workflows/command_tower/workflows/messaging/execution/deliver_workflow.rb

Overview

Job-entry orchestration for a single channel delivery attempt.

Constant Summary

Constants inherited from ApplicationWorkflow

ApplicationWorkflow::ALLOWED_RETRY_STRATEGIES, ApplicationWorkflow::InvalidTransactionResult, ApplicationWorkflow::TransactionFailure

Instance Method Summary collapse

Methods inherited from ApplicationWorkflow

call, call_from_job, continuation_max_attempts, declared_retry_strategy, mark_impersonation_activity!, retry_strategy, validate_retry_strategy!

Methods included from Impersonation::ActivityDeclaration

included

Methods included from Logging::LifecycleDeclaration

included

Methods included from Execution::ContextAccess

#audit, #execution_context, #log_debug, #log_error, #log_info, #log_warn, #publish_event

Methods included from Transactional

#fail_transaction!, #transaction

Instance Method Details

#call(channel_delivery_id:, executor: nil) ⇒ Object



11
12
13
14
15
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
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
# File 'app/workflows/command_tower/workflows/messaging/execution/deliver_workflow.rb', line 11

def call(channel_delivery_id:, executor: nil)
  delivery = load_delivery(channel_delivery_id)
  if delivery.nil?
    return success(payload: { outcome: :noop, reason: :missing }, http_status: :ok)
  end

  if delivery.execution_terminal?
    return success(
      payload: { outcome: :noop, reason: :terminal, channel_delivery_id: delivery.id },
      http_status: :ok,
    )
  end

  unless CommandTower::Messaging::Execution::Claim.call(delivery:)
    return success(
      payload: { outcome: :noop, reason: :claim_lost, channel_delivery_id: delivery.id },
      http_status: :ok,
    )
  end

  delivery.reload
  ledger = CommandTower::Messaging::Execution::AttemptLedger.new
  CommandTower::Messaging::Execution::OperationLogger.claimed(channel_delivery: delivery)
  CommandTower::Messaging::Execution::OperationLogger.started(channel_delivery: delivery)

  attempt = ledger.start(delivery:)
  if attempt.nil?
    return success(
      payload: { outcome: :retryable, reason: :attempt_create_failed, channel_delivery_id: delivery.id },
      http_status: :ok,
    )
  end

  begin
    communication = delivery.communication
    readiness = CommandTower::Messaging::Execution::RevalidateReadiness.call(
      delivery:,
      attempt:,
      communication:,
    )
    if readiness.is_a?(String)
      ledger.finalize_terminal(delivery:, attempt:, error_code: readiness)
      return success(
        payload: { outcome: :terminal, channel_delivery_id: delivery.id, error_code: readiness },
        http_status: :ok,
      )
    end

    resolution = CommandTower::Messaging::Execution::ResolveRecipientAddress.call(
      communication:,
      channel_key: delivery.channel_key,
      readiness_result: readiness,
    )

    if resolution[:error_code]
      ledger.finalize_terminal(delivery:, attempt:, error_code: resolution[:error_code])
      return success(
        payload: {
          outcome: :terminal,
          channel_delivery_id: delivery.id,
          error_code: resolution[:error_code],
        },
        http_status: :ok,
      )
    end

    recipient_address = resolution[:address]

    unless CommandTower::Messaging::Rendering::ChannelRenderer.supported_channel?(delivery.channel_key)
      ledger.finalize_terminal(delivery:, attempt:, error_code: "adapter_unconfigured")
      return success(
        payload: {
          outcome: :terminal,
          channel_delivery_id: delivery.id,
          error_code: "adapter_unconfigured",
        },
        http_status: :ok,
      )
    end

    if blank_recipient?(recipient_address)
      ledger.finalize_terminal(delivery:, attempt:, error_code: "recipient_missing")
      return success(
        payload: {
          outcome: :terminal,
          channel_delivery_id: delivery.id,
          error_code: "recipient_missing",
        },
        http_status: :ok,
      )
    end

    rendered = render_payload!(delivery, attempt, communication, recipient_address, ledger)
    if rendered.nil?
      return success(
        payload: { outcome: :terminal, channel_delivery_id: delivery.id, error_code: :render_failed },
        http_status: :ok,
      )
    end

    request = CommandTower::Messaging::Execution::AdapterRequest.build(
      channel_delivery_id: delivery.id,
      communication_id: delivery.communication_id,
      channel_key: delivery.channel_key,
      attempt_id: attempt.id,
      rendered:,
    )
    adapter = CommandTower::Messaging::Execution::SelectAdapter.call(delivery:, executor:)
    CommandTower::Messaging::Execution::OperationLogger.adapter_started(
      channel_delivery: delivery,
      delivery_attempt: attempt,
    )
    result = adapter.call(request:)
    unless result.is_a?(CommandTower::Messaging::Execution::AdapterResult)
      ledger.finalize_unexpected(delivery:, attempt:, error_class: result.class.name)
      return success(
        payload: { outcome: :retryable, channel_delivery_id: delivery.id },
        http_status: :ok,
      )
    end

    log_adapter_outcome!(delivery, attempt, result)
    ledger.finalize_from_adapter(delivery:, attempt:, result:)
    delivery.reload
    success(
      payload: {
        outcome: outcome_for(result),
        channel_delivery_id: delivery.id,
        status: delivery.status,
      },
      http_status: :ok,
    )
  rescue StandardError => error
    ledger.finalize_unexpected(delivery:, attempt:, error:)
    success(
      payload: { outcome: :retryable, channel_delivery_id: delivery.id },
      http_status: :ok,
    )
  end
end