Issue: Background Task Cancellation and Multiple Task Prevention

SwastikC
Tera Contributor

So I'm creating a background task which will be long running and the users will be seeing a progress bar which clicking on sync. It runs in the background perfectly however I'm unable to find a workaround to cancel the task.

This behavior is problematic because it can result in:

  • Multiple background tasks consuming system resources unnecessarily.

  • Inconsistent states and potential conflicts between tasks.

  • A poor user experience, where tasks may be duplicated or fail to cancel gracefully.

Sharing the code snippet below

var SyncWorker = Class.create();
SyncWorker.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
start: function() {
const TAG = `[SyncWorker | start]`;
try {
var worker = new GlideScriptedHierarchicalWorker();

worker.setProgressName(this.getParameter('sysparm_progressWorkerName'));
worker.setScriptIncludeName(this.getParameter('sysparm_scriptIncludeName'));
worker.setScriptIncludeMethod(this.getParameter('sysparm_scriptIncludeMethod'));
worker.setBackground(true);
worker.start();

var progressID = worker.getProgressID();
gs.info(`${TAG} | ProgressID ${progressID}`);

// Track this progress id

// Create a entry in separate table

return progressID;
} catch (e) {
gs.error(`${TAG} | Error starting syncworker: ${e}`);
return;
}
},

cancel: function() {
const TAG = `[SyncWorker | cancel]`;

var trackerId = this.getParameter("sysparm_tracker_id");
if (!trackerId) {
gs.error(`${TAG} | Tracker ID is missing for cancellation.`);
return false;
}

gs.info(`${TAG} | Attempting to cancel trackerId: ${trackerId}`);

try {

} catch (e) {
gs.error(`${TAG} | Exception while calling cancellation utility: ${e.message}`);
return false;
}
},

type: 'SyncWorker'
});

I need to understand how do i cancel the ongoing background task. Hence the try block in cancel is empty. Looking forward to further guidance.

 

6 REPLIES 6

Bert_c1
Kilo Patron

Where is 'GlideScriptedHierarchicalWorker' documented?

Bert_c1
Kilo Patron

I've done some testing, and I see a record is created in the sys_trigger table. And I've found a solution for that here:

 

script-for-kill-schedule-jobs-running

 

You can try finding the record for your job there and then call the API mentioned to kill the job.

 

Maybe you'll get a response to your post on the 10 year old thread.