Agent/edit job logic (#3023)

* lumped stepdata with jobdata in job dialog

* fix bug with empty steps

* added clumped and update steps and schedules from job dialog

* edit data sends one call instead of multiple

* cleaned code
This commit is contained in:
Aditya Bist
2018-10-31 16:40:36 -07:00
committed by GitHub
parent f7dcaa38ff
commit ce7893c2e5
10 changed files with 129 additions and 111 deletions

View File

@@ -9,6 +9,7 @@ import * as vscode from 'vscode';
import * as sqlops from 'sqlops';
import { AgentUtils } from '../agentUtils';
import { IAgentDialogData, AgentDialogMode } from '../interfaces';
import { JobData } from './jobData';
const localize = nls.loadMessageBundle();
@@ -45,8 +46,19 @@ export class AlertData implements IAgentDialogData {
wmiEventNamespace: string;
wmiEventQuery: string;
constructor(ownerUri:string, alertInfo: sqlops.AgentAlertInfo) {
private viaJobDialog: boolean;
private jobModel: JobData;
constructor(
ownerUri:string,
alertInfo: sqlops.AgentAlertInfo,
jobModel?: JobData,
viaJobDialog: boolean = false
) {
this.ownerUri = ownerUri;
this.viaJobDialog = viaJobDialog;
this.jobModel = jobModel;
this.jobName = this.jobName ? this.jobName : this.jobModel.name;
if (alertInfo) {
this.dialogMode = AgentDialogMode.EDIT;
@@ -60,7 +72,6 @@ export class AlertData implements IAgentDialogData {
this.includeEventDescription = alertInfo.includeEventDescription.toString();
this.isEnabled = alertInfo.isEnabled;
this.jobId = alertInfo.jobId;
this.jobName = alertInfo.jobName;
this.lastOccurrenceDate = alertInfo.lastOccurrenceDate;
this.lastResponseDate = alertInfo.lastResponseDate;
this.messageId = alertInfo.messageId;
@@ -82,10 +93,18 @@ export class AlertData implements IAgentDialogData {
public async save() {
let agentService = await AgentUtils.getAgentService();
let result = this.dialogMode === AgentDialogMode.CREATE
? await agentService.createAlert(this.ownerUri, this.toAgentAlertInfo())
: await agentService.updateAlert(this.ownerUri, this.originalName, this.toAgentAlertInfo());
let result: any;
// if it's called via the job dialog, add it to the
// job model
if (this.viaJobDialog) {
if (this.jobModel) {
Promise.resolve(this);
return;
}
} else {
// has to be a create alert
result = await agentService.createAlert(this.ownerUri, this.toAgentAlertInfo());
}
if (!result || !result.success) {
vscode.window.showErrorMessage(
localize('alertData.saveErrorMessage', "Alert update failed '{0}'", result.errorMessage ? result.errorMessage : 'Unknown'));

View File

@@ -44,6 +44,7 @@ export class JobData implements IAgentDialogData {
public jobSteps: sqlops.AgentJobStepInfo[];
public jobSchedules: sqlops.AgentJobScheduleInfo[];
public alerts: sqlops.AgentAlertInfo[];
public jobId: string;
constructor(
ownerUri: string,
@@ -62,6 +63,7 @@ export class JobData implements IAgentDialogData {
this.jobSteps = jobInfo.JobSteps;
this.jobSchedules = jobInfo.JobSchedules;
this.alerts = jobInfo.Alerts;
this.jobId = jobInfo.jobId;
}
}
@@ -115,7 +117,6 @@ export class JobData implements IAgentDialogData {
let result = this.dialogMode === AgentDialogMode.CREATE
? await this._agentService.createJob(this.ownerUri, jobInfo)
: await this._agentService.updateJob(this.ownerUri, this.originalName, jobInfo);
if (!result || !result.success) {
vscode.window.showErrorMessage(
localize('jobData.saveErrorMessage', "Job update failed '{0}'", result.errorMessage ? result.errorMessage : 'Unknown'));
@@ -135,18 +136,6 @@ export class JobData implements IAgentDialogData {
};
}
public addJobSchedule(schedule: sqlops.AgentJobScheduleInfo) {
if (this.jobSchedules) {
let existingSchedule = this.jobSchedules.find(item => item.name === schedule.name);
if (!existingSchedule) {
this.jobSchedules.push(schedule);
}
} else {
this.jobSchedules = [];
this.jobSchedules.push(schedule);
}
}
public toAgentJobInfo(): sqlops.AgentJobInfo {
return {
name: this.name,
@@ -177,7 +166,7 @@ export class JobData implements IAgentDialogData {
categoryType: 1, // LocalJob, hard-coding the value, corresponds to the target tab in SSMS
lastRun: '',
nextRun: '',
jobId: ''
jobId: this.jobId
};
}
}

View File

@@ -46,11 +46,13 @@ export class JobStepData implements IAgentDialogData {
public retryInterval: number;
public proxyName: string;
private jobModel: JobData;
private viaJobDialog: boolean;
constructor(ownerUri:string, jobModel?: JobData) {
constructor(ownerUri:string, jobModel?: JobData, viaJobDialog: boolean = false) {
this.ownerUri = ownerUri;
this.jobName = jobModel.name;
this.jobModel = jobModel;
this.viaJobDialog = viaJobDialog;
}
public async initialize() {
@@ -59,18 +61,16 @@ export class JobStepData implements IAgentDialogData {
public async save() {
let agentService = await AgentUtils.getAgentService();
let result: any;
if (this.dialogMode === AgentDialogMode.CREATE) {
if (this.jobModel && this.jobModel.dialogMode === AgentDialogMode.CREATE) {
// create job -> create step
// if it's called via the job dialog, add it to the
// job model
if (this.viaJobDialog) {
if (this.jobModel) {
Promise.resolve(this);
return;
} else {
// edit job -> create step
result = await agentService.createJobStep(this.ownerUri, JobStepData.convertToAgentJobStepInfo(this));
}
} else if (this.jobModel && this.jobModel.dialogMode === AgentDialogMode.EDIT) {
// edit job -> edit step
result = await agentService.updateJobStep(this.ownerUri, this.stepName, JobStepData.convertToAgentJobStepInfo(this));
} else {
// has to be a create step
result = await agentService.createJobStep(this.ownerUri, JobStepData.convertToAgentJobStepInfo(this));
}
if (!result || !result.success) {
vscode.window.showErrorMessage(

View File

@@ -29,8 +29,6 @@ export class PickScheduleData implements IAgentDialogData {
}
public async save() {
let agentService = await AgentUtils.getAgentService();
this.selectedSchedule.jobName = this.jobName;
let result = await agentService.createJobSchedule(this.ownerUri, this.selectedSchedule);
}
}