Good morning,
I've not found a good way to do this, so thought I'd as on here as there are often people with much more experience than me.
We are just implementing SQL Server AlwaysOn Groups on our SQL estate and I'm wondering how people handle SQL Jobs.
I've found documentation around how to handle jobs starting execution by ensuring that we are running on the PRIMARY in job step 1, before moving on to the main meat of the SQL job. I assume that this works in most failovers which have happened due to an automated failover (and therefore the actual SQL engine has failed for some reason)?
What I dont fully understand however, is how to handle the below situation:
A job is scheduled to run every 5 seconds, but has a standard execution time of 10 minutes. This starts on the PRIMARY.
After 2 minutes, a manual failover of the job is performed.
The NEW primary schedule fires off, and the job starts to execute AGAIN.
We then have 2 instances of the same job executing at the same time. This in not desirable.
What solutions have people discovered to work around this?
My current idea is to have a control table which has job name and a flag of executing (1 or 0). When the job starts, flick this flag to 1, when it ends, flick it to zero. When a job starts, it checks this table for the executing flag, and if
its already running, then do nothing, otherwise, perform the "am i primary server" check, and then start the job?
Does anybody have experience with this, and do they know a better way of doing this?
Regards,
Andy