/// <summary> /// Marks any future executions of this job in this queue as cancelled. /// </summary> public void CancelFutureExecutions(string remarks = null) { try { // Cancel any tasks that are already scheduled. var tasksToCancel = TaskQueueAdministrator.FindTasks ( this.VaultApplication.PermanentVault, this.QueueId, t => t.Type == TaskQueueBackgroundOperation.TaskTypeId, new[] { MFTaskState.MFTaskStateWaiting, MFTaskState.MFTaskStateInProgress } ); foreach (var task in tasksToCancel.Cast <ApplicationTaskInfo>()) { // Mark each task as superseded. this.TaskProcessor.UpdateCancelledJobInTaskQueue ( task.ToApplicationTask(), string.Empty, remarks ); } } catch (Exception e) { SysUtils.ReportErrorToEventLog ( $"Exception cancelling tasks of type {TaskQueueBackgroundOperation.TaskTypeId} on queue {this.QueueId}.", e ); } }
/// <summary> /// Marks any future executions of this job in this queue as cancelled. /// </summary> /// <param name="backgroundOperationName">If set, cancels only future executions for the specified background operation.</param> /// <param name="remarks">Remarks to set on the cancellation.</param> public void CancelFutureExecutions(string backgroundOperationName = null, string remarks = null) { try { // Cancel any tasks that are already scheduled. var tasksToCancel = TaskQueueAdministrator.FindTasks ( this.VaultApplication.PermanentVault, this.QueueId, t => t.Type == TaskQueueBackgroundOperation.TaskTypeId, new[] { MFTaskState.MFTaskStateWaiting, MFTaskState.MFTaskStateInProgress } ); foreach (var task in tasksToCancel.Cast <ApplicationTaskInfo>()) { var applicationTask = task.ToApplicationTask(); // Skip any that are not for this background operation. if (false == string.IsNullOrWhiteSpace(backgroundOperationName)) { var backgroundOperationDirective = TaskQueueDirective.Parse <BackgroundOperationTaskQueueDirective>(applicationTask); if (null == backgroundOperationDirective?.BackgroundOperationName) { continue; } if (!backgroundOperationDirective.BackgroundOperationName.Equals(backgroundOperationName)) { continue; } } try { // Mark each task as superseded. this.TaskProcessor.UpdateCancelledJobInTaskQueue ( applicationTask, string.Empty, remarks ); } catch (Exception e) { SysUtils.ReportErrorToEventLog ( $"Exception cancelling task {task.TaskID} of type {TaskQueueBackgroundOperation.TaskTypeId} on queue {this.QueueId} to cancel.", e ); } } } catch (Exception e) { SysUtils.ReportErrorToEventLog ( $"Exception retrieving tasks of type {TaskQueueBackgroundOperation.TaskTypeId} on queue {this.QueueId} to cancel.", e ); } }
/// <summary> /// Cancels pre-existing hourly tasks in the waiting or in progress state /// and schedules a new task to process in one hour. /// </summary> private void ScheduleHourlyTask() { try { // Find any tasks of the appropriate type that are already scheduled. ApplicationTaskInfos tasksToCancel = TaskQueueAdministrator.FindTasks ( this.PermanentVault, VaultApplication.BackgroundOperationTaskQueueId, t => t.Type == VaultApplication.TaskTypeHourlyRecurringTask, new[] { MFTaskState.MFTaskStateWaiting, MFTaskState.MFTaskStateInProgress } ); // Cancel the pre-existing hourly tasks. foreach (ApplicationTaskInfo taskInfo in tasksToCancel) { this.TaskProcessor.UpdateCancelledJobInTaskQueue ( taskInfo.ToApplicationTask(), string.Empty, "Superseded." ); } } finally { // Schedule the task to execute in 1 hour. string nextHourlyTaskId = this.TaskProcessor.CreateApplicationTaskSafe ( true, VaultApplication.BackgroundOperationTaskQueueId, VaultApplication.TaskTypeHourlyRecurringTask, null, DateTime.Now.AddHours(1).ToUniversalTime() ); // Debug Logging. if (this.Configuration.LoggingEnabled) { Debug.WriteLine($"Hourly task scheduled with task id => {nextHourlyTaskId}."); } } }
/// <summary> /// Marks any future executions of this job in this queue as cancelled. /// </summary> /// <param name="backgroundOperationName">If set, cancels only future executions for the specified background operation.</param> /// <param name="remarks">Remarks to set on the cancellation.</param> public void CancelFutureExecutions(string backgroundOperationName = null, string remarks = null) { try { // Cancel any tasks that are already scheduled. var tasksToCancel = TaskQueueAdministrator.FindTasks ( this.VaultApplication.PermanentVault, this.QueueId, t => t.Type == TaskQueueBackgroundOperation <TSecureConfiguration> .TaskTypeId, new[] { MFTaskState.MFTaskStateWaiting } ); foreach (var task in tasksToCancel.Cast <ApplicationTaskInfo>()) { var applicationTask = task.ToApplicationTask(); // Skip any that are not for this background operation. if (false == string.IsNullOrWhiteSpace(backgroundOperationName)) { var backgroundOperationDirective = TaskQueueDirective.Parse <BackgroundOperationTaskDirective>(applicationTask); if (null == backgroundOperationDirective?.BackgroundOperationName) { continue; } if (!backgroundOperationDirective.BackgroundOperationName.Equals(backgroundOperationName)) { continue; } } try { // Mark each task as superseded. switch (task.State) { case MFTaskState.MFTaskStateInProgress: this.VaultApplication.TaskManager.CancelActiveTask ( this.VaultApplication.PermanentVault, task.TaskID ); break; case MFTaskState.MFTaskStateWaiting: this.VaultApplication.TaskManager.CancelWaitingTask ( this.VaultApplication.PermanentVault, task.TaskID ); break; default: // Cannot cancel ones in other states. break; } } catch (Exception e) { SysUtils.ReportErrorToEventLog ( $"Exception cancelling task {task.TaskID} of type {TaskQueueBackgroundOperation<TSecureConfiguration>.TaskTypeId} on queue {this.QueueId} to cancel.", e ); } } } catch (Exception e) { SysUtils.ReportErrorToEventLog ( $"Exception retrieving tasks of type {TaskQueueBackgroundOperation<TSecureConfiguration>.TaskTypeId} on queue {this.QueueId} to cancel.", e ); } }