From e72f5fb28f72e56a056fdff55cc3ef01e08eeb82 Mon Sep 17 00:00:00 2001 From: Brett Hewitson Date: Sat, 18 Dec 2021 10:06:41 +1000 Subject: [PATCH] Wrapped the scheduled task creation in a try..catch --- .../Utils/TaskSchedulerUtils.cs | 68 ++++++++++++++++--- 1 file changed, 60 insertions(+), 8 deletions(-) diff --git a/src/ServerManager.Common/Utils/TaskSchedulerUtils.cs b/src/ServerManager.Common/Utils/TaskSchedulerUtils.cs index 9114452c..087d8e2b 100644 --- a/src/ServerManager.Common/Utils/TaskSchedulerUtils.cs +++ b/src/ServerManager.Common/Utils/TaskSchedulerUtils.cs @@ -183,8 +183,21 @@ namespace ServerManagerTool.Common.Utils if (taskFolder == null) return false; - var task = taskFolder.Tasks.Exists(taskName) ? taskFolder.Tasks[taskName] : null; - var taskDefinition = task?.Definition ?? TaskService.Instance.NewTask(); + Task task = null; + TaskDefinition taskDefinition = null; + + try + { + task = taskFolder.Tasks.Exists(taskName) ? taskFolder.Tasks[taskName] : null; + taskDefinition = task?.Definition ?? TaskService.Instance.NewTask(); + } + catch (Exception ex) + { + _logger.Error($"{nameof(ScheduleAutoBackup)} - Unable to fetch the task definition. {ex.Message}\r\n{ex.StackTrace}"); + + // just ignore + taskDefinition = TaskService.Instance.NewTask(); + } if (taskDefinition == null) return false; @@ -294,8 +307,21 @@ namespace ServerManagerTool.Common.Utils if (taskFolder == null) return false; - var task = taskFolder.Tasks.Exists(taskName) ? taskFolder.Tasks[taskName] : null; - var taskDefinition = task?.Definition ?? TaskService.Instance.NewTask(); + Task task = null; + TaskDefinition taskDefinition = null; + + try + { + task = taskFolder.Tasks.Exists(taskName) ? taskFolder.Tasks[taskName] : null; + taskDefinition = task?.Definition ?? TaskService.Instance.NewTask(); + } + catch (Exception ex) + { + _logger.Error($"{nameof(ScheduleAutoBackup)} - Unable to fetch the task definition. {ex.Message}\r\n{ex.StackTrace}"); + + // just ignore + taskDefinition = TaskService.Instance.NewTask(); + } if (taskDefinition == null) return false; @@ -424,8 +450,21 @@ namespace ServerManagerTool.Common.Utils if (taskFolder == null) return false; - var task = taskFolder.Tasks.Exists(taskName) ? taskFolder.Tasks[taskName] : null; - var taskDefinition = task?.Definition ?? TaskService.Instance.NewTask(); + Task task = null; + TaskDefinition taskDefinition = null; + + try + { + task = taskFolder.Tasks.Exists(taskName) ? taskFolder.Tasks[taskName] : null; + taskDefinition = task?.Definition ?? TaskService.Instance.NewTask(); + } + catch (Exception ex) + { + _logger.Error($"{nameof(ScheduleAutoBackup)} - Unable to fetch the task definition. {ex.Message}\r\n{ex.StackTrace}"); + + // just ignore + taskDefinition = TaskService.Instance.NewTask(); + } if (taskDefinition == null) return false; @@ -553,8 +592,21 @@ namespace ServerManagerTool.Common.Utils if (taskFolder == null) return false; - var task = taskFolder.Tasks.Exists(taskName) ? taskFolder.Tasks[taskName] : null; - var taskDefinition = task?.Definition ?? TaskService.Instance.NewTask(); + Task task = null; + TaskDefinition taskDefinition = null; + + try + { + task = taskFolder.Tasks.Exists(taskName) ? taskFolder.Tasks[taskName] : null; + taskDefinition = task?.Definition ?? TaskService.Instance.NewTask(); + } + catch (Exception ex) + { + _logger.Error($"{nameof(ScheduleAutoBackup)} - Unable to fetch the task definition. {ex.Message}\r\n{ex.StackTrace}"); + + // just ignore + taskDefinition = TaskService.Instance.NewTask(); + } if (taskDefinition == null) return false;