2023-02-02 17:15:17 -08:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2021-05-06 14:54:10 -07:00
|
|
|
package job
|
|
|
|
|
|
|
|
|
|
const runJobsQuery = `
|
|
|
|
|
insert into job_run (
|
2022-05-18 17:03:24 -07:00
|
|
|
job_plugin_id, job_name, controller_id
|
2021-05-06 14:54:10 -07:00
|
|
|
)
|
2024-11-06 16:15:49 +00:00
|
|
|
select
|
|
|
|
|
j.plugin_id, j."name", ?
|
|
|
|
|
from job j
|
|
|
|
|
where next_scheduled_run <= current_timestamp
|
2021-05-06 14:54:10 -07:00
|
|
|
order by next_scheduled_run asc
|
2024-11-06 16:15:49 +00:00
|
|
|
on conflict
|
|
|
|
|
(job_plugin_id, job_name)
|
2022-03-11 15:11:14 -08:00
|
|
|
where status = 'running'
|
|
|
|
|
do nothing
|
2021-05-06 14:54:10 -07:00
|
|
|
returning *;
|
|
|
|
|
`
|
|
|
|
|
|
2022-02-11 17:20:19 -08:00
|
|
|
const upsertJobQuery = `
|
2021-05-06 14:54:10 -07:00
|
|
|
insert into job (
|
2021-09-30 08:47:34 -04:00
|
|
|
plugin_id,
|
|
|
|
|
name,
|
|
|
|
|
description,
|
|
|
|
|
next_scheduled_run
|
2021-05-06 14:54:10 -07:00
|
|
|
) values (
|
2021-09-30 08:47:34 -04:00
|
|
|
@plugin_id, -- plugin_id
|
|
|
|
|
@name, -- name
|
|
|
|
|
@description, -- description
|
|
|
|
|
wt_add_seconds_to_now(@next_scheduled_run) -- next_scheduled_run
|
2021-05-06 14:54:10 -07:00
|
|
|
)
|
2022-02-11 17:20:19 -08:00
|
|
|
on conflict on constraint
|
|
|
|
|
job_pkey
|
|
|
|
|
do update set
|
|
|
|
|
description = @description
|
2021-05-06 14:54:10 -07:00
|
|
|
returning *;
|
|
|
|
|
`
|
|
|
|
|
|
2021-06-11 11:36:14 -07:00
|
|
|
const setNextScheduledRunIfSoonerQuery = `
|
|
|
|
|
update
|
|
|
|
|
job
|
|
|
|
|
set
|
|
|
|
|
next_scheduled_run = least(wt_add_seconds_to_now(?), next_scheduled_run)
|
|
|
|
|
where
|
|
|
|
|
plugin_id = ?
|
|
|
|
|
and name = ?
|
|
|
|
|
returning *;
|
|
|
|
|
`
|
|
|
|
|
|
2021-05-06 14:54:10 -07:00
|
|
|
const setNextScheduledRunQuery = `
|
|
|
|
|
update
|
|
|
|
|
job
|
|
|
|
|
set
|
|
|
|
|
next_scheduled_run = wt_add_seconds_to_now(?)
|
|
|
|
|
where
|
|
|
|
|
plugin_id = ?
|
|
|
|
|
and name = ?
|
|
|
|
|
returning *;
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
const updateProgressQuery = `
|
|
|
|
|
update
|
|
|
|
|
job_run
|
|
|
|
|
set
|
|
|
|
|
completed_count = ?,
|
2024-09-24 09:27:24 -04:00
|
|
|
total_count = ?,
|
|
|
|
|
retries_count = ?
|
2021-05-06 14:54:10 -07:00
|
|
|
where
|
|
|
|
|
private_id = ?
|
|
|
|
|
and status = 'running'
|
|
|
|
|
returning *;
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
const completeRunQuery = `
|
2024-10-25 14:21:26 +01:00
|
|
|
delete from job_run
|
2021-05-06 14:54:10 -07:00
|
|
|
where
|
|
|
|
|
private_id = ?
|
|
|
|
|
and status = 'running'
|
|
|
|
|
returning *;
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
const failRunQuery = `
|
|
|
|
|
update
|
|
|
|
|
job_run
|
|
|
|
|
set
|
2021-07-16 18:39:43 -04:00
|
|
|
completed_count = ?,
|
|
|
|
|
total_count = ?,
|
2024-09-24 09:27:24 -04:00
|
|
|
retries_count = ?,
|
2021-07-16 18:39:43 -04:00
|
|
|
status = 'failed',
|
|
|
|
|
end_time = current_timestamp
|
2021-05-06 14:54:10 -07:00
|
|
|
where
|
|
|
|
|
private_id = ?
|
|
|
|
|
and status = 'running'
|
|
|
|
|
returning *;
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
const interruptRunsQuery = `
|
|
|
|
|
update
|
|
|
|
|
job_run
|
|
|
|
|
set
|
|
|
|
|
status = 'interrupted',
|
|
|
|
|
end_time = current_timestamp
|
|
|
|
|
where
|
|
|
|
|
update_time <= wt_add_seconds_to_now(?)
|
|
|
|
|
and status = 'running'
|
|
|
|
|
%s
|
|
|
|
|
returning *;
|
|
|
|
|
`
|
2021-09-30 08:47:34 -04:00
|
|
|
|
|
|
|
|
const deleteJobByName = `
|
|
|
|
|
delete
|
|
|
|
|
from job
|
|
|
|
|
where
|
|
|
|
|
name = ?
|
|
|
|
|
`
|