Skip to content

Commit f3c04ee

Browse files
committed
feat: cancel process instance if task testing is canceled
Related to camunda/camunda-modeler#5296
1 parent 2929e12 commit f3c04ee

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

demo/App.jsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ function App() {
218218
.then(response => response.json());
219219
};
220220

221+
const cancelProcessInstance = async (processInstanceKey) => {
222+
return fetch(`/api/cancelProcessInstance/${processInstanceKey}`, { method: 'POST' });
223+
};
224+
221225
const { current: onConfigChanged } = useRef(debounce(config => setConfig(config), 300));
222226

223227
// eslint-disable-next-line no-undef
@@ -246,7 +250,8 @@ function App() {
246250
startInstance,
247251
getProcessInstance,
248252
getProcessInstanceVariables,
249-
getProcessInstanceIncident
253+
getProcessInstanceIncident,
254+
cancelProcessInstance
250255
} }
251256
config={ config }
252257
onConfigChanged={ onConfigChanged }

demo/server.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,26 @@ app.get('/api/getProcessInstanceIncident/:processInstanceKey', async (req, res)
165165
}
166166
});
167167

168+
app.post('/api/cancelProcessInstance/:processInstanceKey', async (req, res) => {
169+
try {
170+
if (!camunda) {
171+
return res.json({ success: false, error: 'Camunda environment not configured' });
172+
}
173+
174+
const { processInstanceKey } = req.params;
175+
176+
const client = camunda.getCamundaRestClient();
177+
178+
const response = await client.cancelProcessInstance({
179+
processInstanceKey
180+
});
181+
182+
res.json({ success: true, response });
183+
} catch (err) {
184+
res.status(500).json({ success: false, error: err.message, source: err.source });
185+
}
186+
});
187+
168188
const PORT = process.env.PORT || 3000;
169189

170190
app.listen(PORT, () => {

lib/TaskExecution.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export default class TaskExecution extends EventEmitter {
3939
/** @type {TaskExecutionStatus} */
4040
this._status = 'idle';
4141

42+
this._processInstanceKey = null;
43+
4244
const eventBus = injector.get('eventBus');
4345

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

102-
const processInstanceKey = getProcessInstanceKey(startInstanceResult.response);
104+
const processInstanceKey = this._processInstanceKey = getProcessInstanceKey(startInstanceResult.response);
103105

104106
if (!processInstanceKey) {
105107
this._emitError('Failed to retrieve process instance key from start instance response');
@@ -199,6 +201,11 @@ export default class TaskExecution extends EventEmitter {
199201
clearInterval(this._interval);
200202
}
201203

204+
if (this._processInstanceKey) {
205+
this._api.cancelProcessInstance(this._processInstanceKey);
206+
this._processInstanceKey = null;
207+
}
208+
202209
this._changeStatus('idle');
203210
}
204211

lib/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export type TaskExecutionApi = {
7878
getProcessInstance: (processInstanceKey: string) => Promise<GetProcessInstanceResult>;
7979
getProcessInstanceVariables: (processInstanceKey: string) => Promise<GetProcessInstanceVariablesResult>;
8080
getProcessInstanceIncident: (processInstanceKey: string) => Promise<GetProcessInstanceIncidentResult>;
81+
cancelProcessInstance: (processInstanceKey: string) => Promise<any>;
8182
};
8283

8384
export type TaskExecutionStatus =

0 commit comments

Comments
 (0)