MCPcopy Create free account
hub / github.com/AS-AIGC/TranscriptHub / cancel_task_controller

Function cancel_task_controller

apps/backend/controller/task_controller.js:237–409  ·  view source on GitHub ↗

* @brief Cancel an existing transcription task * * @param req [in] Express request object containing: * - body.label Task label * - body.sso_account User account * - body.token Authorization token * - body.task_objid Task identifier * @param res [out] Express response objec

(req, res)

Source from the content-addressed store, hash-verified

235 * @note Logs all file cleanup operations
236 */
237async function cancel_task_controller(req, res) {
238 const route = '/api/v1/rest/CancelTask';
239 const { label, sso_account, token, task_objid } = req.body || {};
240 //const ip_address = req.ip;
241 const ip_address = translate_ipv4_to_ipv6(req.headers['x-forwarded-for'] || req.connection.remoteAddress);
242
243 const memo = operation_memo({
244 route: route,
245 token: token,
246 sso_account: sso_account,
247 ip_address: ip_address,
248 ref: task_objid
249 });
250 if (!sso_account || !token || !label || !task_objid) {
251 logger(LOG_LEVEL.ERROR, `The task cacellation failed because sso_account=${sso_account}, token=${token}, label=${label}, objid=${task_objid} were not provided.`, memo);
252 return res.status(400).json({
253 message: 'The task cancellation failed becasue sso_account, token, label, objid were not provided.',
254 task_objid,
255 status: NOTIFY_STATUS.ERROR
256 });
257 }
258
259 // Check if the task belongs to the SSO account
260 const is_valid = await is_authorized_to_access_task({task_objid, sso_account});
261 if (!is_valid) {
262 logger(LOG_LEVEL.WARNING, `${route}: This task ${task_objid} does not belong to the sso account ${sso_account}, or ${task_objid} is duplicated.`, memo);
263 return res.status(401).json({
264 message: 'This task objid does not belong to the sso account, or objid is duplicated.',
265 task_objid,
266 status: NOTIFY_STATUS.ERROR
267 });
268 }
269
270 try {
271 const start_time = process.hrtime();
272
273 let result = await check_task_status({
274 objid: task_objid
275 });
276
277 if (result?.recordset?.length === 1 && result.recordset[0].STATUS !== 1) {
278 result = await cancel_task({
279 objid: task_objid
280 });
281 } else {
282 /* Force to cancel the runnning process, if status == 1 kill child_process or delete from
283 if (child_process && !child_process.killed) {
284 child_process.kill('SIGINT');
285 res.status(200).send({ message: 'Cancellation initiated successfully.' });
286 } else {
287 res.status(200).send({ message: 'No active task found or task already terminated.' });
288 }
289 */
290 logger(LOG_LEVEL.WARNING, `${route}: This task ${task_objid} does not exists or is currently executing.`, memo);
291 return res.status(200).json({
292 message: 'The task does not exists or is currently executing.',
293 task_objid,
294 status: NOTIFY_STATUS.ERROR

Callers

nothing calls this directly

Calls 6

translate_ipv4_to_ipv6Function · 0.85
operation_memoFunction · 0.85
loggerFunction · 0.85
check_task_statusFunction · 0.85
cancel_taskFunction · 0.85

Tested by

no test coverage detected