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

SwastikC
Tera Contributor

@Bert_c1 
This didnt helped. My issue is not starting a background progress worker. Rather terminating it. 

google that API, seems you didn't or you would have found:

 

https://www.linkedin.com/pulse/run-script-include-background-asynchronously-ujjawal-vishnoi/ 

 

plus several other posts on the API.

SwastikC
Tera Contributor

I googled all the other things before posting this question here. Seems like you didn't had the chance to see if its working or not otherwise you would know that this isnt working. None the aless let me share you the logs of the current approach you are suggesting.

Sample log:
[SyncWorker | cancel] | Exception while cancelling tracker: "SNC" is not defined.

This is arising because of im in a scoped application and SNC if i know well is only available in global scope and hence I wanted know a workaround this situation.