Hi! I want to schedule a map/reduce script automat...
# suitescript
s
Hi! I want to schedule a map/reduce script automatically when a user approves a record in NetSuite. However, if they approve more than one record in close time range I get the error "MAP_REDUCE_ALREADY_RUNNING" since by approval script tries to start the same deployment again. Is it possible to check in script if a deployment is currently running?
b
Depending on your concurrency requirements, make multiple deployments of your map/reduce and dont specify the deployment id when you create your task
s
@battk I'll try that. Thanks!
b
Netsuite will choose an open deployment (using what is probably a non thread safe algorithm)
n
Another approach to handle this is to create a queue record (custom record) and insert the information which can then be picked up by Map Reduce. You can then schedule the MR script to run after 30 minutes and look through the queue record type and process the unprocessed records.
s
@NickSuite I don't want to wait for a scheduled deployment to run but want it to process right away. It is a queue record sort of I am using already that is approved and should then be processed by the script 🙂
n
What if the number of users who are approving the records increases?
r
you can check at what stage your map-reduce is, using task.checkStatus(scriptTaskId)
if it's at the summarize stage, it's because it's almost done
👍 1
s
@NickSuite It is only one person responsible and allowed to approve so don't think it should be a risk.
n
But you are getting the error already. I am just suggesting a long-term solution. I once made this same assumption. Later on, the users increased and the script did not process all the records which caused some problem.
b
Dilbert's solution is a good one, especially if you want to build in error handling
👍 1
Its actually one of the serious options for building a realtime integration
s
@battk @NickSuite Right now the solution has a queue record: 1. User makes change to a transaction which creates a custom record containing the details about the change. 2. The above custom record is then approved and should be picked up by the mapreduce script. I set error messages etc on this record if the script fails for some reason to update the transaction e,g. If I create a second custom record for the approved record. I still want the change to be processed as quick as possible after the update meaning that if the mapreduce is not running I want to schedule it right away. If it however already is running, that's when I get the error
n
That, IMO, is not a queue record. That is a "Changes Waiting to be Approved" record type.
b
No deployment id when submitting tasks lets netsuite choose a free deployment to run
n
What if there is no free deployment?
b
More deployments
😄 1
n
A queue record addresses this problem exactly.
b
The usual problem from what i see is that netsuite does not choose deployments in a thread safe way. So it can choose a deployment even if a concurrent script has created a task for it
Generally only a problem in very high activity records
💯 1
s
But even if I create a queue record, how can I solve the problem with the immediate scheduling of mapreduce if it's not running and not schedule if it already is?
b
You can create and submit tasks in a loop if you are lazy, or do something clever like create your own deployment to create a task for
If you want to handle the scheduling yourself, do a search on scheduled script instances to tell if your map reduce deployment is already in the queue
👍 1
expect to learn how to handle concurrency from the SuiteApp Architectural Fundamentals & Examples guide if you want to handle high concurrency
s
Great! Thank you all for all the help! Appreciate it! 🙂
m
If you want to go the route of multiple deployments and are worried about running out of usage increases, I've heard of people creating deployments on the fly if needed.
d
^^^^ This is exactly what I do. Submit the task and if you get the already running error you just create a new deployment on the fly and submit again.
b
@dcrsmith could you please share a snippet of creating a new deployment on the fly?