/// <summary> /// Executes the task /// </summary> /// <param name="throwException">A value indicating whether exception should be thrown if some error happens</param> /// <param name="dispose">A value indicating whether all instances hsould be disposed after task run</param> public void Execute(bool throwException = false, bool dispose = true) { this.IsRunning = true; //background tasks has an issue with Autofac //because scope is generated each time it's requested //that's why we get one single scope here //this way we can also dispose resources once a task is completed var scheduleTaskService = new ScheduleTaskService(null); var scheduleTask = scheduleTaskService.GetTaskByType(this.Type); try { var task = this.CreateTask(); if (task != null) { this.LastStartUtc = DateTime.UtcNow; if (scheduleTask != null) { //update appropriate datetime properties scheduleTask.LastStartUtc = this.LastStartUtc; scheduleTaskService.UpdateTask(scheduleTask); } //execute task task.Execute(); this.LastEndUtc = this.LastSuccessUtc = DateTime.UtcNow; } } catch (Exception exc) { this.Enabled = !this.StopOnError; this.LastEndUtc = DateTime.UtcNow; System.Diagnostics.Debug.Write(exc.Message); //log error var logger = new TaskLog(); //logger.Error(string.Format("Error while running the '{0}' schedule task. {1}", this.Name, exc.Message), exc); if (throwException) { throw; } } if (scheduleTask != null) { //update appropriate datetime properties scheduleTask.LastEndUtc = this.LastEndUtc; scheduleTask.LastSuccessUtc = this.LastSuccessUtc; scheduleTaskService.UpdateTask(scheduleTask); } //dispose all resources if (dispose) { } this.IsRunning = false; }
/// <summary> /// Executes the task /// </summary> /// <param name="throwException">A value indicating whether exception should be thrown if some error happens</param> /// <param name="dispose">A value indicating whether all instances hsould be disposed after task run</param> public void Execute(bool throwException = false, bool dispose = true) { this.IsRunning = true; //background tasks has an issue with Autofac //because scope is generated each time it's requested //that's why we get one single scope here //this way we can also dispose resources once a task is completed var scheduleTaskService = new ScheduleTaskService(null); var scheduleTask = scheduleTaskService.GetTaskByType(this.Type); try { var task = this.CreateTask(); if (task != null) { this.LastStartUtc = DateTime.UtcNow; if (scheduleTask != null) { //update appropriate datetime properties scheduleTask.LastStartUtc = this.LastStartUtc; scheduleTaskService.UpdateTask(scheduleTask); } //execute task task.Execute(); this.LastEndUtc = this.LastSuccessUtc = DateTime.UtcNow; } } catch (Exception exc) { this.Enabled = !this.StopOnError; this.LastEndUtc = DateTime.UtcNow; System.Diagnostics.Debug.Write(exc.Message); //log error var logger = new TaskLog(); //logger.Error(string.Format("Error while running the '{0}' schedule task. {1}", this.Name, exc.Message), exc); if (throwException) throw; } if (scheduleTask != null) { //update appropriate datetime properties scheduleTask.LastEndUtc = this.LastEndUtc; scheduleTask.LastSuccessUtc = this.LastSuccessUtc; scheduleTaskService.UpdateTask(scheduleTask); } //dispose all resources if (dispose) { } this.IsRunning = false; }