diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs index 66e05703..a61e70d7 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs @@ -882,7 +882,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent ConfigAction configAction, RunType runType) { - return await Task>.Run(() => + return await Task>.Run(async () => { try { @@ -895,6 +895,34 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent ExecuteAction(actions, runType); } + // Execute step actions if they exist + if (jobInfo.JobSteps != null && jobInfo.JobSteps.Length > 0) + { + foreach (AgentJobStepInfo step in jobInfo.JobSteps) + { + await ConfigureAgentJobStep(ownerUri, step, configAction, runType); + } + } + + // Execute schedule actions if they exist + if (jobInfo.JobSchedules != null && jobInfo.JobSchedules.Length > 0) + { + foreach (AgentScheduleInfo schedule in jobInfo.JobSchedules) + { + await ConfigureAgentSchedule(ownerUri, schedule, configAction, runType); + } + } + + // Execute alert actions if they exist + if (jobInfo.Alerts != null && jobInfo.Alerts.Length > 0) + { + foreach (AgentAlertInfo alert in jobInfo.Alerts) + { + alert.JobId = jobData.Job.JobID.ToString(); + await ConfigureAgentAlert(ownerUri, alert.Name, alert, configAction, runType); + } + } + return new Tuple(true, string.Empty); } catch (Exception ex) @@ -949,15 +977,25 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent { try { - ConnectionInfo connInfo; - ConnectionServiceInstance.TryFindConnection(ownerUri, out connInfo); - CDataContainer dataContainer = CDataContainer.CreateDataContainer(connInfo, databaseExists: true); + CDataContainer dataContainer; + JobData jobData = null; + // If the alert is being created outside of a job + if (string.IsNullOrWhiteSpace(alert.JobName)) + { + ConnectionInfo connInfo; + ConnectionServiceInstance.TryFindConnection(ownerUri, out connInfo); + dataContainer = CDataContainer.CreateDataContainer(connInfo, databaseExists: true); + } + else + { + // If the alert is being created inside a job + CreateJobData(ownerUri, alert.JobName, out dataContainer, out jobData); + } STParameters param = new STParameters(dataContainer.Document); param.SetParam("alert", alertName); - if (alert != null) { - using (AgentAlertActions actions = new AgentAlertActions(dataContainer, alertName, alert, configAction)) + using (AgentAlertActions actions = new AgentAlertActions(dataContainer, alertName, alert, configAction, jobData)) { ExecuteAction(actions, runType); } diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentAlertActions.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentAlertActions.cs index 927284fe..957da270 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentAlertActions.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentAlertActions.cs @@ -25,18 +25,25 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent private string originalAlertName = null; + private JobData jobData = null; + /// /// Default constructor that will be used to create dialog /// /// public AgentAlertActions( CDataContainer dataContainer, string originalAlertName, - AgentAlertInfo alertInfo, ConfigAction configAction) + AgentAlertInfo alertInfo, ConfigAction configAction, + JobData jobData = null) { this.originalAlertName = originalAlertName; this.alertInfo = alertInfo; this.DataContainer = dataContainer; this.configAction = configAction; + if (jobData != null) + { + this.jobData = jobData; + } } private static string GetAlertName(CDataContainer container) @@ -92,6 +99,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent if (alert != null) { alert.DropIfExists(); + if (this.jobData != null) + { + JobAlertData alertData = new JobAlertData(alert); + this.jobData.JobAlerts.DeleteAlert(alertData); + } } } } @@ -130,6 +142,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent if (createNewAlert) { alert.Create(); + if (this.jobData != null) + { + JobAlertData alertData = new JobAlertData(alert); + this.jobData.JobAlerts.AddAlert(alertData); + } } else { @@ -169,6 +186,10 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent { throw new ArgumentNullException("alert"); } + if (this.alertInfo.JobId != null) + { + alert.JobID = new Guid(this.alertInfo.JobId); + } alert.DatabaseName = !string.IsNullOrWhiteSpace(this.alertInfo.DatabaseName) ? this.alertInfo.DatabaseName : string.Empty;