How do I make sure only one instance of my scheduled job is running at any given point of time?

prabhasgupte
Kilo Expert

I have a requirement: My scoped application have a scheduled job which executes a script include whenever it runs. I need to implement singleton for this scheduled job - meaning, only one instance of that job should be running at any point of time.

How can I achieve that with servicenow?

What I had on my mind is:

Job runs and checks some place to see if another instance of it (or of that script include) is already running.

If yes, terminate yourself. If not, go ahead and do the job.

7 REPLIES 7

Chuck Tomasi
Tera Patron

You could do this with a very simple table to serve as a "lock". One table, one field (perhaps a date field so you know when the script starts).



The script flow would go something like this:


  • Check if the record exists, if so, abort
  • If not, create a new record in the table
  • Run the remainder of your script
  • Delete the record from the table


GlideRecord - ServiceNow Wiki


Thanks, Chuck!



This does make some sense, but since records in table can be manipulated externally, I would like to avoid this approach as much as possible.


I was hoping for something similar to properties but which is not accessible otherwise.



nonetheless, the alternative you suggested is worth a try.


This would be a very unique case where you would use a display business rule to create a record in another table and an after business rule to delete it if it exists.



Even if someone is manipulating the table externally, you can still enforce it with the business rule. There may be rare exceptions where someone is using a transform map with business rules disabled or a script with setWorkflow(false), but since those are maintained by admin, you know when those are going to skip your locking mechanism.


Hi Chuck.   This doesn't appear to handle a race condition where if multiple workflows were executing at the same time where it would prevent them from reading this table simultaneously.   Is there a way to "lock" the table that stores this condition to keep this race condition from running?   What about the "Lock" activity in the workflow utility activities?