I'm looking for best practice in dealing with scheduled SQL Server Agent jobs in SQL Server 2012 availability groups. Maybe I missed something, however at the current state I feel that SQL Server Agent is not really integrated with this great SQL2012 feature.
How can I make a scheduled SQL agent job aware of a node switch? For example I have a job running on the primary node which loads data each hour. Now if the primary goes down, how can I activate the job on the secondary which now becomes primary?
If I schedule the job always on the secondary it fails because then the secondary is read-only.
Best Answer
Within your SQL Server Agent job, have some conditional logic to test for if the current instance is serving the particular role you are looking for on you availability group:
All this does is pull the current role of the local replica, and if it's in the
PRIMARY
role, you can do whatever it is that your job needs to do if it is the primary replica. TheELSE
block is optional, but it's to handle possible logic if your local replica isn't primary.Of course, change
'YourAvailabilityGroupName'
in the above query to your actual availability group name.Don't confuse availability groups with failover cluster instances. Whether the instance is the primary or secondary replica for a given availability group doesn't affect server-level objects, like SQL Server Agent jobs and so on.