mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 18:47:57 -05:00
Agent: Edit Job backend (#700)
* added stepInfo for editing jobs/steps * added schedules to history requests * added alerts and schedules to history * formatting * code review comments * removed smo import
This commit is contained in:
@@ -186,6 +186,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
|
|||||||
out connInfo);
|
out connInfo);
|
||||||
if (connInfo != null)
|
if (connInfo != null)
|
||||||
{
|
{
|
||||||
|
ConnectionServiceInstance.TryFindConnection(parameters.OwnerUri, out connInfo);
|
||||||
|
CDataContainer dataContainer = CDataContainer.CreateDataContainer(connInfo, databaseExists: true);
|
||||||
|
var jobs = dataContainer.Server.JobServer.Jobs;
|
||||||
Tuple<SqlConnectionInfo, DataTable, ServerConnection> tuple = CreateSqlConnection(connInfo, parameters.JobId);
|
Tuple<SqlConnectionInfo, DataTable, ServerConnection> tuple = CreateSqlConnection(connInfo, parameters.JobId);
|
||||||
SqlConnectionInfo sqlConnInfo = tuple.Item1;
|
SqlConnectionInfo sqlConnInfo = tuple.Item1;
|
||||||
DataTable dt = tuple.Item2;
|
DataTable dt = tuple.Item2;
|
||||||
@@ -202,7 +205,19 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
|
|||||||
var tlog = t as ILogSource;
|
var tlog = t as ILogSource;
|
||||||
tlog.Initialize();
|
tlog.Initialize();
|
||||||
var logEntries = t.LogEntries;
|
var logEntries = t.LogEntries;
|
||||||
jobHistories = AgentUtilities.ConvertToAgentJobHistoryInfo(logEntries, job);
|
|
||||||
|
// Send Steps, Alerts and Schedules with job history in background
|
||||||
|
JobStepCollection steps = jobs[jobName].JobSteps;
|
||||||
|
JobScheduleCollection schedules = jobs[jobName].JobSchedules;
|
||||||
|
List<Alert> alerts = new List<Alert>();
|
||||||
|
foreach (Alert alert in dataContainer.Server.JobServer.Alerts)
|
||||||
|
{
|
||||||
|
if (alert.JobID == jobId && alert.JobName == jobName)
|
||||||
|
{
|
||||||
|
alerts.Add(alert);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
jobHistories = AgentUtilities.ConvertToAgentJobHistoryInfo(logEntries, job, steps, schedules, alerts);
|
||||||
tlog.CloseReader();
|
tlog.CloseReader();
|
||||||
}
|
}
|
||||||
result.Jobs = jobHistories.ToArray();
|
result.Jobs = jobHistories.ToArray();
|
||||||
@@ -739,6 +754,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
|
|||||||
for (int i = 0; i < scheduleCount; ++i)
|
for (int i = 0; i < scheduleCount; ++i)
|
||||||
{
|
{
|
||||||
var schedule = dataContainer.Server.JobServer.SharedSchedules[i];
|
var schedule = dataContainer.Server.JobServer.SharedSchedules[i];
|
||||||
|
var scheduleData = new JobScheduleData(schedule);
|
||||||
schedules[i] = new AgentScheduleInfo();
|
schedules[i] = new AgentScheduleInfo();
|
||||||
schedules[i].Id = schedule.ID;
|
schedules[i].Id = schedule.ID;
|
||||||
schedules[i].Name = schedule.Name;
|
schedules[i].Name = schedule.Name;
|
||||||
@@ -756,6 +772,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
|
|||||||
schedules[i].JobCount = schedule.JobCount;
|
schedules[i].JobCount = schedule.JobCount;
|
||||||
schedules[i].ActiveEndDate = schedule.ActiveEndDate;
|
schedules[i].ActiveEndDate = schedule.ActiveEndDate;
|
||||||
schedules[i].ScheduleUid = schedule.ScheduleUid;
|
schedules[i].ScheduleUid = schedule.ScheduleUid;
|
||||||
|
schedules[i].Description = scheduleData.Description;
|
||||||
}
|
}
|
||||||
result.Schedules = schedules;
|
result.Schedules = schedules;
|
||||||
result.Success = true;
|
result.Success = true;
|
||||||
|
|||||||
@@ -57,24 +57,93 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static AgentJobStep ConvertToAgentJobStep(ILogEntry logEntry, DataRow jobRow)
|
internal static AgentJobStep ConvertToAgentJobStepInfo(JobStep step, LogSourceJobHistory.LogEntryJobHistory logEntry, string jobId)
|
||||||
{
|
{
|
||||||
var entry = logEntry as LogSourceJobHistory.LogEntryJobHistory;
|
AgentJobStepInfo stepInfo = new AgentJobStepInfo();
|
||||||
string stepId = entry.StepID;
|
stepInfo.JobId = jobId;
|
||||||
string stepName = entry.StepName;
|
stepInfo.JobName = logEntry.JobName;
|
||||||
string message = entry.Message;
|
stepInfo.StepName = step.Name;
|
||||||
DateTime runDate = logEntry.PointInTime;
|
stepInfo.SubSystem = step.SubSystem.ToString();
|
||||||
int runStatus = logEntry.Severity == SeverityClass.Success ? 1 : 0;
|
stepInfo.Id = step.ID;
|
||||||
AgentJobStep step = new AgentJobStep();
|
stepInfo.FailureAction = step.OnFailAction.ToString();
|
||||||
step.StepId = stepId;
|
stepInfo.SuccessAction = step.OnSuccessAction.ToString();
|
||||||
step.StepName = stepName;
|
stepInfo.FailStepId = step.OnFailStep;
|
||||||
step.Message = message;
|
stepInfo.SuccessStepId = step.OnSuccessStep;
|
||||||
step.RunDate = runDate;
|
stepInfo.Command = step.Command;
|
||||||
step.RunStatus = runStatus;
|
stepInfo.CommandExecutionSuccessCode = step.CommandExecutionSuccessCode;
|
||||||
return step;
|
stepInfo.DatabaseName = step.DatabaseName;
|
||||||
|
stepInfo.DatabaseUserName = step.DatabaseUserName;
|
||||||
|
stepInfo.Server = step.Server;
|
||||||
|
stepInfo.OutputFileName = step.OutputFileName;
|
||||||
|
stepInfo.RetryAttempts = step.RetryAttempts;
|
||||||
|
stepInfo.RetryInterval = step.RetryInterval;
|
||||||
|
stepInfo.ProxyName = step.ProxyName;
|
||||||
|
AgentJobStep jobStep = new AgentJobStep();
|
||||||
|
jobStep.stepId = logEntry.StepID;
|
||||||
|
jobStep.stepName = logEntry.StepName;
|
||||||
|
jobStep.stepDetails = stepInfo;
|
||||||
|
jobStep.message = logEntry.Message;
|
||||||
|
jobStep.runDate = step.LastRunDate.ToString();
|
||||||
|
jobStep.runStatus = (Contracts.CompletionResult) step.LastRunOutcome;
|
||||||
|
return jobStep;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<AgentJobHistoryInfo> ConvertToAgentJobHistoryInfo(List<ILogEntry> logEntries, DataRow jobRow)
|
internal static AgentScheduleInfo ConvertToAgentScheduleInfo(JobSchedule schedule)
|
||||||
|
{
|
||||||
|
AgentScheduleInfo scheduleInfo = new AgentScheduleInfo();
|
||||||
|
scheduleInfo.Id = schedule.ID;
|
||||||
|
scheduleInfo.Name = schedule.Name;
|
||||||
|
scheduleInfo.JobName = " ";
|
||||||
|
scheduleInfo.IsEnabled = schedule.IsEnabled;
|
||||||
|
scheduleInfo.FrequencyTypes = (Contracts.FrequencyTypes) schedule.FrequencyTypes;
|
||||||
|
scheduleInfo.FrequencySubDayTypes = (Contracts.FrequencySubDayTypes) schedule.FrequencySubDayTypes;
|
||||||
|
scheduleInfo.FrequencySubDayInterval = schedule.FrequencySubDayInterval;
|
||||||
|
scheduleInfo.FrequencyRelativeIntervals = (Contracts.FrequencyRelativeIntervals) schedule.FrequencyRelativeIntervals;
|
||||||
|
scheduleInfo.FrequencyRecurrenceFactor = schedule.FrequencyRecurrenceFactor;
|
||||||
|
scheduleInfo.FrequencyInterval = schedule.FrequencyInterval;
|
||||||
|
scheduleInfo.DateCreated = schedule.DateCreated;
|
||||||
|
scheduleInfo.ActiveStartTimeOfDay = schedule.ActiveStartTimeOfDay;
|
||||||
|
scheduleInfo.ActiveStartDate = schedule.ActiveStartDate;
|
||||||
|
scheduleInfo.ActiveEndTimeOfDay = schedule.ActiveEndTimeOfDay;
|
||||||
|
scheduleInfo.ActiveEndDate = schedule.ActiveEndDate;
|
||||||
|
scheduleInfo.JobCount = schedule.JobCount;
|
||||||
|
scheduleInfo.ScheduleUid = schedule.ScheduleUid;
|
||||||
|
var scheduleData = new JobScheduleData(schedule);
|
||||||
|
scheduleInfo.Description = scheduleData.Description;
|
||||||
|
return scheduleInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static AgentAlertInfo ConvertToAgentAlertInfo(Alert alert)
|
||||||
|
{
|
||||||
|
AgentAlertInfo alertInfo = new AgentAlertInfo();
|
||||||
|
alertInfo.Id = alert.ID;
|
||||||
|
alertInfo.Name = alert.Name;
|
||||||
|
alertInfo.DelayBetweenResponses = alert.DelayBetweenResponses;
|
||||||
|
alertInfo.EventDescriptionKeyword = alert.EventDescriptionKeyword;
|
||||||
|
alertInfo.EventSource = alert.EventSource;
|
||||||
|
alertInfo.HasNotification = alert.HasNotification;
|
||||||
|
alertInfo.IncludeEventDescription = (Contracts.NotifyMethods) alert.IncludeEventDescription;
|
||||||
|
alertInfo.IsEnabled = alert.IsEnabled;
|
||||||
|
alertInfo.JobId = alert.JobID.ToString();
|
||||||
|
alertInfo.JobName = alert.JobName;
|
||||||
|
alertInfo.LastOccurrenceDate = alert.LastOccurrenceDate.ToString();
|
||||||
|
alertInfo.LastResponseDate = alert.LastResponseDate.ToString();
|
||||||
|
alertInfo.MessageId = alert.MessageID;
|
||||||
|
alertInfo.NotificationMessage = alert.NotificationMessage;
|
||||||
|
alertInfo.OccurrenceCount = alert.OccurrenceCount;
|
||||||
|
alertInfo.PerformanceCondition = alert.PerformanceCondition;
|
||||||
|
alertInfo.Severity = alert.Severity;
|
||||||
|
alertInfo.DatabaseName = alert.DatabaseName;
|
||||||
|
alertInfo.CountResetDate = alert.CountResetDate.ToString();
|
||||||
|
alertInfo.CategoryName = alert.CategoryName;
|
||||||
|
alertInfo.AlertType = (Contracts.AlertType) alert.AlertType;
|
||||||
|
alertInfo.WmiEventNamespace = alert.WmiEventNamespace;
|
||||||
|
alertInfo.WmiEventQuery = alert.WmiEventQuery;
|
||||||
|
return alertInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<AgentJobHistoryInfo> ConvertToAgentJobHistoryInfo(List<ILogEntry> logEntries,
|
||||||
|
DataRow jobRow, JobStepCollection steps, JobScheduleCollection schedules, List<Alert> alerts)
|
||||||
{
|
{
|
||||||
List<AgentJobHistoryInfo> jobs = new List<AgentJobHistoryInfo>();
|
List<AgentJobHistoryInfo> jobs = new List<AgentJobHistoryInfo>();
|
||||||
// get all the values for a job history
|
// get all the values for a job history
|
||||||
@@ -102,14 +171,29 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent
|
|||||||
|
|
||||||
// Add steps to the job if any
|
// Add steps to the job if any
|
||||||
var jobSteps = new List<AgentJobStep>();
|
var jobSteps = new List<AgentJobStep>();
|
||||||
if (entry.CanLoadSubEntries)
|
foreach (JobStep step in steps)
|
||||||
{
|
{
|
||||||
foreach (ILogEntry step in entry.SubEntries)
|
var jobId = jobRow[UrnJobId].ToString();
|
||||||
{
|
jobSteps.Add(AgentUtilities.ConvertToAgentJobStepInfo(step, logEntry, jobId));
|
||||||
jobSteps.Add(AgentUtilities.ConvertToAgentJobStep(step, jobRow));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
jobHistoryInfo.Steps = jobSteps.ToArray();
|
jobHistoryInfo.Steps = jobSteps.ToArray();
|
||||||
|
|
||||||
|
// Add schedules to the job if any
|
||||||
|
var jobSchedules = new List<AgentScheduleInfo>();
|
||||||
|
foreach (JobSchedule schedule in schedules)
|
||||||
|
{
|
||||||
|
jobSchedules.Add(AgentUtilities.ConvertToAgentScheduleInfo(schedule));
|
||||||
|
}
|
||||||
|
jobHistoryInfo.Schedules = jobSchedules.ToArray();
|
||||||
|
|
||||||
|
// Add alerts to the job if any
|
||||||
|
var jobAlerts = new List<AgentAlertInfo>();
|
||||||
|
foreach (Alert alert in alerts)
|
||||||
|
{
|
||||||
|
jobAlerts.Add(AgentUtilities.ConvertToAgentAlertInfo(alert));
|
||||||
|
}
|
||||||
|
jobHistoryInfo.Alerts = jobAlerts.ToArray();
|
||||||
jobs.Add(jobHistoryInfo);
|
jobs.Add(jobHistoryInfo);
|
||||||
}
|
}
|
||||||
return jobs;
|
return jobs;
|
||||||
|
|||||||
@@ -34,16 +34,28 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent.Contracts
|
|||||||
public string RetriesAttempted { get; set; }
|
public string RetriesAttempted { get; set; }
|
||||||
public string Server { get; set; }
|
public string Server { get; set; }
|
||||||
public AgentJobStep[] Steps { get; set; }
|
public AgentJobStep[] Steps { get; set; }
|
||||||
|
public AgentScheduleInfo[] Schedules { get; set; }
|
||||||
|
public AgentAlertInfo[] Alerts { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum CompletionResult
|
||||||
|
{
|
||||||
|
Failed = 0,
|
||||||
|
Succeeded = 1,
|
||||||
|
Retry = 2,
|
||||||
|
Cancelled = 3,
|
||||||
|
InProgress = 4,
|
||||||
|
Unknown = 5
|
||||||
}
|
}
|
||||||
|
|
||||||
public class AgentJobStep
|
public class AgentJobStep
|
||||||
{
|
{
|
||||||
public string StepId { get; set; }
|
public string jobId;
|
||||||
public string StepName { get; set; }
|
public string stepId;
|
||||||
public string Message { get; set; }
|
public string stepName;
|
||||||
public DateTime RunDate { get; set; }
|
public string message;
|
||||||
public int RunStatus { get; set; }
|
public string runDate;
|
||||||
|
public CompletionResult runStatus;
|
||||||
|
public AgentJobStepInfo stepDetails;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,5 +64,6 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent.Contracts
|
|||||||
public int JobCount { get; set; }
|
public int JobCount { get; set; }
|
||||||
public DateTime ActiveEndDate { get; set; }
|
public DateTime ActiveEndDate { get; set; }
|
||||||
public Guid ScheduleUid { get; set; }
|
public Guid ScheduleUid { get; set; }
|
||||||
|
public string Description { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user