SuiteScript 2.0 – Queuing Background Tasks

I have tasks that use too many resources to run in the foreground. So I had previously pushed them to the background and expected them to queue. However, they didn’t. If a map/reduce script was already running and another was submitted, the second instance simply failed.

Here’s where I went wrong. When kicking off my background map/reduce script, I got too specific. If you submit your background job like this, you can force the job/task to queue. If you add the optional deploymentId you cannot.

var myTask = task.create({
     taskType: task.TaskType.MAP_REDUCE,
     scriptId: 'customscript_map_recalc',
     params: { custscript_customer: customer_id }
 });
 var task_id = myTask.submit();

To force a background job to queue, you need to create multiple deployments for the same script. Then when you submit the job, leave off the deploymentId and the system will select a deployment ID for you.

Of course you have the option of purchasing additional NetSuite processors, which will allow you to move beyond the 2 you get by default. But most of us are limited to 2. And jobs can be configured to run on one or multiple processors. In my case, I need operations to occur in a specific order, so I limit my jobs to one.

In either case, if you don’t setup your jobs to queue and one is already running, the next one fails.

The easiest way I found to create multiple deployments is to “Save As” your initial deployment you create when you save your script for the first time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s