Is it possible to trigger the same Map/Reduce scri...
# suitescript
m
Is it possible to trigger the same Map/Reduce script more than once from User Event at the same time or while another one has not finished executing without getting this error?
Copy code
type: "error.SuiteScriptError",
   name: "MAP_REDUCE_ALREADY_RUNNING",
   message: "Map/Reduce Script 2234 with Deployment 1 is already running and cannot be started until it has completed."
b
this is a surprisingly complicated question
😬 1
the most basic answer to the question is yes, just dont specify the deployment id
and netsuite will choose from among the available deployments, meaning you just make a bunch of deployments for your map/reduce script
the sad truth that makes it complicated is that N/task is not actually thread safe, if 2 scripts submit a task at the same time, they may choose the same deployment and one of them will throw the already running error
m
Oh nooooo
Thanks for your clear response....So I should hope the task isn't submitted at the same time.
b
there are various ways around it
the easiest of which is a try catch loop that just tries it again
will work fairly well if you dont actually have high concurrency all the tiem
the overengineered solution is to make a new deployment of your map/reduce in your use event and specify that deployment while creating the task
typically requires you to eventually delete the deployment later
the solution that sacrifices performance is to implement a lock via a custom record to make sure that only one script can submit a task at a time, the SAFE guide has a section on dealing with concurrency issues
m
Thanks @battk....Let me try these solutions.
w
We usually create some form of queue of records that need processing. Could be as simple as setting a checkbox on the record that you are submitting. Then keep only a single deployment of the map/reduce and let other attempts of starting it fail silently. The summarize of the map/reduce would then check if there are other records to process and resubmit itself if that is the case. You may not get instant processing of the Nth record that is submitted.
👍 2
t
@Martin Guga I was typing it out and saw that I do basically 95% the same thing as Watz. Would recommend making a queue
👍 1