Dear all,
I am just wondering which would be the best and more accurate way to execute off-peak hours jobs (maintenance) in an Always On
My infrastructure is deployed throughout Always On Availability Groups (several ones), synchronous commit, manual failover.
On weekly/daily basis some jobs are launched and I need to make sure to have the same jobs in both nodes (just in case).
I am using some conditional query stuff for knowing who is the primary and who is the secondary and act consequently.
First step of any of my Agent jobs always starts with querying some DMVs (any suggestions would be very welcomed):
use MASTER; declare @primaryreplica as varchar(7); set @primaryreplica = (select InA.role_desc from sys.dm_hadr_availability_replica_states INA /*AG.name, RE.replica_server_name, */ inner join sys.availability_groups_cluster AG on inA.group_id = AG.group_id inner join [sys].[dm_hadr_availability_replica_cluster_states] RE on re.replica_id = INA.replica_id where Ag.name = 'AlwaysOnUS' and role_desc = 'PRIMARY') IF (@primaryreplica = 'PRIMARY') --EXECUTE THE CODE HERE ELSE RAISERROR('We are in secondary replica.', 11, 1);
Because many jobs have enabled notifications (to me) by using a relay email server it is always annoying to receive an email with “Job Failed” when it is not really necessary.
How can I change the flow in order the following:
-When the job starts in read-only replica, go to RAISERROR line but don’t send any email to me.
-When the job stats in primary replica, just send the email ALWAYS.
Now, have got this:
I don't know if I am very clear with my explanations.
Thanks for your comments!