Can Two CopyStorm Jobs Run on the Same Database at the Same Time?
Having multiple CopyStorm jobs run on the same database at the same time is perfectly OK if the two jobs are processing different Salesforce tables. However, what if two CopyStorm jobs are running at the same time on the same set of tables? How can this happen?
- CopyStorm is scheduled to run every 30 minutes.
- A CopyStorm job starts at 12:00.
- At 12:30 the first CopyStorm job is still running because there were a LOT of updates in Salesforce.
- The scheduler starts a second CopyStorm job at 12:30.
In practice this is unlikely to cause an issue because the second job will not catch up with the first job (e.g. the two jobs will never be operating on the same table at the same time by happenstance). However, this is a condition that is easily AND SHOULD BE avoided.
How to Avoid a Job Running Twice on Linux and Mac
To prevent the same job running twice (or more times) is the flock command. The following script ensures that the CopyStorm job never runs twice at the same time:
( flock -x 9 sh /home/greg/bin/CopyStorm/CopyStorm.sh -run /home/greg/copyStormConfig/MyJob.copyStorm ) 9>/tmp/MyJob.lock
If the script is run twice, the second script will wait (at the flock) command until the first script completes. Once the first invocation of the script completes, the flock command will return and the second invocation will continue.
How to Avoid a Job Running Twice on Windows
The easiest way to prevent a job from running twice on Windows is to use the Windows scheduler. By default, the Windows Scheduler will not start a scheduled task again if a previous invocation is still running.