Skip to content

Commit

Permalink
feat(backend): encapsulate query lookup in trace
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurapov committed Aug 27, 2024
1 parent 2c8fc82 commit d49321c
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions packages/backend/src/open_payments/payment/outgoing/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,33 @@ const MAX_STATE_ATTEMPTS = 5
export async function processPendingPayment(
deps_: ServiceDependencies
): Promise<string | undefined> {
return deps_.knex.transaction(async (trx) => {
const payment = await getPendingPayment(trx)
if (!payment) return

await handlePaymentLifecycle(
{
...deps_,
knex: trx,
logger: deps_.logger.child({
payment: payment.id,
from_state: payment.state
})
},
payment
)
return payment.id
})
const tracer = trace.getTracer('outgoing_payment_worker')

return tracer.startActiveSpan(
'outgoingPaymentLifecycle',
async (span: Span) => {
const paymentId = await deps_.knex.transaction(async (trx) => {
const payment = await getPendingPayment(trx)
if (!payment) return

await handlePaymentLifecycle(
{
...deps_,
knex: trx,
logger: deps_.logger.child({
payment: payment.id,
from_state: payment.state
})
},
payment
)
return payment.id
})

span.end()
return paymentId
}
)
}

// Fetch (and lock) a payment for work.
Expand Down Expand Up @@ -64,24 +74,16 @@ async function handlePaymentLifecycle(
deps: ServiceDependencies,
payment: OutgoingPayment
): Promise<void> {
const tracer = trace.getTracer('outgoing_payment_worker')
await tracer.startActiveSpan(
'outgoingPaymentLifecycle',
async (span: Span) => {
if (payment.state !== OutgoingPaymentState.Sending) {
deps.logger.warn('unexpected payment in lifecycle')
span.end()
return
}

try {
await lifecycle.handleSending(deps, payment)
} catch (error) {
await onLifecycleError(deps, payment, error as Error | PaymentError)
}
span.end()
}
)
if (payment.state !== OutgoingPaymentState.Sending) {
deps.logger.warn('unexpected payment in lifecycle')
return
}

try {
await lifecycle.handleSending(deps, payment)
} catch (error) {
await onLifecycleError(deps, payment, error as Error | PaymentError)
}
}

async function onLifecycleError(
Expand Down

0 comments on commit d49321c

Please sign in to comment.