Skip to content

Commit

Permalink
added more props to completion jobs from original
Browse files Browse the repository at this point in the history
  • Loading branch information
timgit committed Jun 21, 2018
1 parent b21edd8 commit 69cd4e9
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/plans.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@ function fetchNextJob(schema) {
`;
}

function buildJsonCompletionObject(withResponse) {
// job completion contract
return `jsonb_build_object(
'request', jsonb_build_object('id', id, 'name', name, 'data', data),
'response', ${withResponse ? '$2::jsonb' : 'null'},
'state', state,
'retryCount', retryCount,
'createdOn', createdOn,
'startedOn', startedOn,
'completedOn', completedOn
)`;
}

function completeJobs(schema){
return `
WITH results AS (
Expand All @@ -209,7 +222,7 @@ function completeJobs(schema){
INSERT INTO ${schema}.job (name, data)
SELECT
name || '${completedJobSuffix}',
jsonb_build_object('request', jsonb_build_object('id', id, 'name', name, 'data', data), 'response', $2::jsonb, 'state', state)
${buildJsonCompletionObject(true)}
FROM results
WHERE name NOT LIKE '%${completedJobSuffix}'
RETURNING 1
Expand Down Expand Up @@ -254,9 +267,10 @@ function failJobs(schema){
INSERT INTO ${schema}.job (name, data)
SELECT
name || '${completedJobSuffix}',
jsonb_build_object('request', jsonb_build_object('id', id, 'name', name, 'data', data), 'response', $2::jsonb, 'state', state)
${buildJsonCompletionObject(true)}
FROM results
WHERE state = '${states.failed}'
AND NOT name LIKE '%${completedJobSuffix}'
RETURNING 1
`; // returning 1 here just to count results against input array
}
Expand All @@ -278,9 +292,10 @@ function expire(schema) {
INSERT INTO ${schema}.job (name, data)
SELECT
name || '${completedJobSuffix}',
jsonb_build_object('request', jsonb_build_object('id', id, 'name', name, 'data', data), 'response', null, 'state', state)
${buildJsonCompletionObject()}
FROM results
WHERE state = '${states.expired}'
AND NOT name LIKE '%${completedJobSuffix}'
`;
}

Expand Down

0 comments on commit 69cd4e9

Please sign in to comment.