4
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
job_id="${1:?usage: replay-failed.sh JOB_UUID [REQUESTED_BY] [ENV_FILE]}"
|
||||
requested_by="${2:-operator}"
|
||||
env_file="${3:-.env}"
|
||||
|
||||
case "$job_id" in
|
||||
????????-????-????-????-????????????) ;;
|
||||
*) echo "JobId must be a UUID" >&2; exit 2 ;;
|
||||
esac
|
||||
case "$requested_by" in
|
||||
*[!A-Za-z0-9_.@-]*|'') echo "RequestedBy contains unsupported characters" >&2; exit 2 ;;
|
||||
esac
|
||||
|
||||
sql="WITH candidate AS (
|
||||
SELECT j.id, j.attachment_id, j.attempt FROM attachment_jobs j
|
||||
JOIN attachments a ON a.id = j.attachment_id
|
||||
WHERE j.id = '$job_id'::uuid AND j.status = 'failed' AND a.status = 'failed'
|
||||
FOR UPDATE OF j, a
|
||||
), audited AS (
|
||||
INSERT INTO attachment_job_replays (job_id, requested_by, previous_attempt)
|
||||
SELECT id, '$requested_by', attempt FROM candidate RETURNING job_id
|
||||
), reset_job AS (
|
||||
UPDATE attachment_jobs j SET status = 'queued', attempt = 1, published = false,
|
||||
last_error_code = NULL, next_attempt_at = clock_timestamp(),
|
||||
lease_token = NULL, lease_until = NULL, updated_at = clock_timestamp()
|
||||
FROM candidate c, audited a WHERE j.id = c.id AND a.job_id = c.id
|
||||
RETURNING j.attachment_id
|
||||
), reset_attachment AS (
|
||||
UPDATE attachments a SET status = 'queued', error_code = NULL,
|
||||
updated_at = clock_timestamp()
|
||||
FROM reset_job r WHERE a.id = r.attachment_id AND a.status = 'failed'
|
||||
RETURNING a.id
|
||||
)
|
||||
SELECT CASE WHEN EXISTS (SELECT 1 FROM reset_attachment)
|
||||
THEN 'replayed' ELSE 'no-op' END;"
|
||||
|
||||
result="$(docker compose --env-file "$env_file" exec -T postgres \
|
||||
psql -v ON_ERROR_STOP=1 -U messenger -d messenger -At -c "$sql")"
|
||||
if [ "$result" = "replayed" ]; then
|
||||
echo "Replay requested for failed job $job_id by $requested_by"
|
||||
else
|
||||
echo "No-op: job $job_id is missing, not failed, or already recovered"
|
||||
fi
|
||||
Reference in New Issue
Block a user