Skip to content

Commit

Permalink
sql: rework SHOW JOBS WHEN COMPLETE delegate
Browse files Browse the repository at this point in the history
The previous pattern was fragile because the filter could be re-arranged.

Release note: None
  • Loading branch information
ajwerner committed Aug 9, 2021
1 parent aa9701d commit 070a799
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions pkg/sql/delegate/show_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,21 @@ SHOW JOBS SELECT id FROM system.jobs WHERE created_by_type='%s' and created_by_i
sqlStmt := fmt.Sprintf("%s %s %s", selectClause, whereClause, orderbyClause)
if n.Block {
sqlStmt = fmt.Sprintf(
`SELECT * FROM [%s]
WHERE
IF(finished IS NULL,
IF(pg_sleep(1), crdb_internal.force_retry('24h'), 0),
0
) = 0`, sqlStmt)
`
WITH jobs AS (SELECT * FROM [%s]),
sleep_and_restart_if_unfinished AS (
SELECT IF(pg_sleep(1), crdb_internal.force_retry('24h'), 1)
= 0 AS timed_out
FROM (SELECT job_id FROM jobs WHERE finished IS NULL LIMIT 1)
),
fail_if_slept_too_long AS (
SELECT crdb_internal.force_error('55000', 'timed out waiting for jobs')
FROM sleep_and_restart_if_unfinished
WHERE timed_out
)
SELECT *
FROM jobs
WHERE NOT EXISTS(SELECT * FROM fail_if_slept_too_long)`, sqlStmt)
}
return parse(sqlStmt)
}

0 comments on commit 070a799

Please sign in to comment.