---
title: "Ecs Task Stopped"
method: POST
path: "/api/v1/pipeline/internal/ecs-task-stopped"
tags: ["pipeline"]
---

# Ecs Task Stopped

`POST /api/v1/pipeline/internal/ecs-task-stopped`

## Request

- Base URL: `https://app.goodleads.club` (the document declares no server; this is the origin it was published from)
- URL: `https://app.goodleads.club/api/v1/pipeline/internal/ecs-task-stopped`
- Auth: API key in header `X-API-Key`

EventBridge target — invoked when an ECS task in our cluster transitions
to STOPPED. Marks any matching pipeline.job row as 'failed' with ECS's
actual stoppedReason as the error message, and — whether the task exited
cleanly or died — hands the job to settlement so a paid order whose check
runs just finished settles in seconds instead of waiting for the sweep.

Replaces the polling-cron approach that PR #179 proposed (and closed). ECS
publishes Task State Change events to EventBridge automatically; listening
is strictly better than polling — seconds of latency vs. minutes, zero
Fargate cost, and we get ECS's structured failure reason ("OutOfMemory",
exit codes, etc.) instead of "ECS task stopped unexpectedly."

Idempotent and safe by construction:
- Tasks not matching any pipeline.job row → no-op (State GMs, Platform EM
  sessions live in the same cluster but have no row here).
- Jobs already in a terminal state → the row is left exactly as it is; only
  the settlement check runs, and it is itself idempotent.
- Events with lastStatus != STOPPED → ignored (the rule filters these out
  at EventBridge; this is defense-in-depth).
- EventBridge is at-least-once, so a replayed delivery re-runs both halves;
  neither bills or mutates twice.

The trigger-path sweep in has_running_job remains as a backstop for the
rare case where an EventBridge delivery is lost, and the two-hourly
stuck-order sweep remains the backstop for settlement.

## Request body

- object

## Response `200`

Successful Response

- unknown

## Other responses

- `422`: Validation Error

---

[API](https://skmtc.dev/goodleads/apis/formation-gtm-api.md) · [All operations](https://skmtc.dev/goodleads/apis/formation-gtm-api/llms.txt) · [OpenAPI document](https://skmtc.dev/goodleads/apis/formation-gtm-api/revisions/2068d13a82cf?raw)
