Skip to content

Commit

Permalink
chore: move to new method
Browse files Browse the repository at this point in the history
  • Loading branch information
jwulf committed Jan 6, 2025
1 parent edaddd8 commit 2220b4b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ test('Can complete a task with job corrections', (done) => {
taskType: 'job-correction',
taskHandler: async (job) => {
expect(job.processInstanceKey).toBe(wf?.processInstanceKey)
const res1 = await job.complete({
// @TODO: correction interface
const res1 = await job.completeWithJobResult({
variables: {},
result: {},
})
// @TODO: correction interface
done(null)
return res1
},
Expand Down
22 changes: 21 additions & 1 deletion src/zeebe/lib/ZBWorkerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import * as ZB from './interfaces-1.0'
import {
ActivateJobsRequest,
ActivateJobsResponse,
CompleteJobRequest,
JobResult,
} from './interfaces-grpc-1.0'
import { ZBClientOptions } from './interfaces-published-contract'

Expand Down Expand Up @@ -340,6 +342,14 @@ You should call only one job action method in the worker handler. This is a bug
(completedVariables?: T) =>
this.completeJob(job.key, completedVariables ?? {})

const correctJob =
(job: ZB.Job<WorkerInputVariables, CustomHeaderShape>) =>
(req: Pick<CompleteJobRequest, 'result' | 'variables'>) =>
this.completeJob(job.key, {
result: req.result,
variables: req.variables,
})

const errorJob =
(job: ZB.Job<WorkerInputVariables, CustomHeaderShape>) =>
(e: string | ZB.ErrorJobWithVariables, errorMessage: string = '') => {
Expand All @@ -362,9 +372,14 @@ You should call only one job action method in the worker handler. This is a bug

const fail = failJob(thisJob)
const succeed = succeedJob(thisJob)
const completeWithResult = correctJob(thisJob)
return {
cancelWorkflow: cancelWorkflow(thisJob),
complete: errorMsgOnPriorMessageCall('job.complete', succeed),
completeWithJobResult: errorMsgOnPriorMessageCall(
'job.completeWithJobResult',
completeWithResult
),
error: errorMsgOnPriorMessageCall('error', errorJob(thisJob)),
fail: errorMsgOnPriorMessageCall('job.fail', fail),
forward: errorMsgOnPriorMessageCall('job.forward', () => {
Expand Down Expand Up @@ -402,11 +417,16 @@ You should call only one job action method in the worker handler. This is a bug
})
}

private completeJob(jobKey: string, completedVariables = {}) {
private completeJob(
jobKey: string,
completedVariables = {},
result: JobResult = {}
) {
return this.zbClient
.completeJob({
jobKey,
variables: completedVariables,
result,
})
.then((res) => {
this.logger.logDebug(`Completed job ${jobKey} for ${this.taskType}`)
Expand Down
6 changes: 6 additions & 0 deletions src/zeebe/lib/interfaces-1.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ export interface JobCompletionInterface<WorkerOutputVariables> {
complete: (
updatedVariables?: WorkerOutputVariables
) => Promise<JOB_ACTION_ACKNOWLEDGEMENT>
/**
* Complete the job with success, and a job result that may contain corrections. Since Camunda 8.7.
*/
completeWithJobResult: (
req: Pick<CompleteJobRequest, 'result' | 'variables'>
) => Promise<JOB_ACTION_ACKNOWLEDGEMENT>
/**
* Fail the job with an informative message as to the cause. Optionally, pass in a
* value remaining retries. If no value is passed for retries then the current retry
Expand Down

0 comments on commit 2220b4b

Please sign in to comment.