Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion demo/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ function App() {
.then(response => response.json());
};

const cancelProcessInstance = async (processInstanceKey) => {
return fetch(`/api/cancelProcessInstance/${processInstanceKey}`, { method: 'POST' });
};

const { current: onConfigChanged } = useRef(debounce(config => setConfig(config), 300));

// eslint-disable-next-line no-undef
Expand Down Expand Up @@ -246,7 +250,8 @@ function App() {
startInstance,
getProcessInstance,
getProcessInstanceVariables,
getProcessInstanceIncident
getProcessInstanceIncident,
cancelProcessInstance
} }
config={ config }
onConfigChanged={ onConfigChanged }
Expand Down
20 changes: 20 additions & 0 deletions demo/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,26 @@ app.get('/api/getProcessInstanceIncident/:processInstanceKey', async (req, res)
}
});

app.post('/api/cancelProcessInstance/:processInstanceKey', async (req, res) => {
try {
if (!camunda) {
return res.json({ success: false, error: 'Camunda environment not configured' });
}

const { processInstanceKey } = req.params;

const client = camunda.getCamundaRestClient();

const response = await client.cancelProcessInstance({
processInstanceKey
});

res.json({ success: true, response });
} catch (err) {
res.status(500).json({ success: false, error: err.message, source: err.source });
}
});

const PORT = process.env.PORT || 3000;

app.listen(PORT, () => {
Expand Down
11 changes: 10 additions & 1 deletion lib/TaskExecution.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export default class TaskExecution extends EventEmitter {
/** @type {TaskExecutionStatus} */
this._status = 'idle';

this._processInstanceKey = null;

const eventBus = injector.get('eventBus');

eventBus.on([ 'selection.changed', 'commandStack.changed' ], () => {
Expand Down Expand Up @@ -99,7 +101,7 @@ export default class TaskExecution extends EventEmitter {
return;
}

const processInstanceKey = getProcessInstanceKey(startInstanceResult.response);
const processInstanceKey = this._processInstanceKey = getProcessInstanceKey(startInstanceResult.response);

if (!processInstanceKey) {
this._emitError('Failed to retrieve process instance key from start instance response');
Expand Down Expand Up @@ -157,6 +159,8 @@ export default class TaskExecution extends EventEmitter {
const isCompleted = [ 'COMPLETED', 'TERMINATED', 'CANCELED' ].includes(state);

if (isCompleted || hasIncident) {
this._processInstanceKey = null;

const getProcessInstanceVariablesResult = await this._api.getProcessInstanceVariables(processInstanceKey);

if (/** @type {TaskExecutionStatus} */ (this._status) === 'idle') {
Expand Down Expand Up @@ -199,6 +203,11 @@ export default class TaskExecution extends EventEmitter {
clearInterval(this._interval);
}

if (this._processInstanceKey) {
this._api.cancelProcessInstance(this._processInstanceKey);
this._processInstanceKey = null;
}

this._changeStatus('idle');
}

Expand Down
1 change: 1 addition & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export type TaskExecutionApi = {
getProcessInstance: (processInstanceKey: string) => Promise<GetProcessInstanceResult>;
getProcessInstanceVariables: (processInstanceKey: string) => Promise<GetProcessInstanceVariablesResult>;
getProcessInstanceIncident: (processInstanceKey: string) => Promise<GetProcessInstanceIncidentResult>;
cancelProcessInstance: (processInstanceKey: string) => Promise<any>;
};

export type TaskExecutionStatus =
Expand Down
6 changes: 5 additions & 1 deletion test/TaskExecution.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ describe('TaskExecution', function() {
startInstance: sinon.stub().resolves({ success: false, error: 'Not implemented' }),
getProcessInstance: sinon.stub().resolves({ success: false, error: 'Not implemented' }),
getProcessInstanceVariables: sinon.stub().resolves({ success: false, error: 'Not implemented' }),
getProcessInstanceIncident: sinon.stub().resolves({ success: false, error: 'Not implemented' })
getProcessInstanceIncident: sinon.stub().resolves({ success: false, error: 'Not implemented' }),
cancelProcessInstance: sinon.stub().resolves({ success: false, error: 'Not implemented' })
};

taskExecution = new TaskExecution(injector, api);
Expand Down Expand Up @@ -73,6 +74,7 @@ describe('TaskExecution', function() {
expect(api.getProcessInstance).to.have.been.calledOnce;
expect(api.getProcessInstanceVariables).to.have.been.calledOnce;
expect(api.getProcessInstanceIncident).to.not.have.been.called;
expect(api.cancelProcessInstance).to.not.have.been.called;

expect(finishedSpy).to.have.been.calledWithMatch({
'incident': null,
Expand Down Expand Up @@ -107,6 +109,7 @@ describe('TaskExecution', function() {
expect(api.getProcessInstance).to.have.been.calledTwice;
expect(api.getProcessInstanceVariables).to.have.been.calledOnce;
expect(api.getProcessInstanceIncident).to.not.have.been.called;
expect(api.cancelProcessInstance).to.not.have.been.called;

expect(finishedSpy).to.have.been.calledWithMatch({
'incident': null,
Expand Down Expand Up @@ -417,6 +420,7 @@ describe('TaskExecution', function() {
expect(errorSpy).to.not.have.been.called;
expect(api.deploy).to.have.been.calledOnce;
expect(api.startInstance).to.have.been.calledOnce;
expect(api.cancelProcessInstance).to.have.been.calledOnce;
expect(api.getProcessInstance).to.not.have.been.called;
expect(api.getProcessInstanceVariables).to.not.have.been.called;
expect(api.getProcessInstanceIncident).to.not.have.been.called;
Expand Down