コード例 #1
0
 private static void DeadTaskTimerElapsed(object o)
 {
     if (TaskDataHandler.GetDeadTaskCount() > 0)
     {
         AgentHub.BroadcastMessage(null);
     }
 }
コード例 #2
0
 public TaskController(IHubContext <AgentHub> agentHub, IHubContext <TaskMonitorHub> monitorHub,
                       ApplicationHandler appHandler, TaskDataHandler dataHandler)
 {
     _agentHub           = agentHub;
     _monitorHub         = monitorHub;
     _applicationHandler = appHandler;
     _dataHandler        = dataHandler;
 }
コード例 #3
0
        public void TaskFinished(SnTaskResult taskResult)
        {
            SnTrace.TaskManagement.Write("AgentHub TaskFinished called. Agent: {0} / {1}, taskId: {2}, code: {3}, error: {4}",
                                         taskResult.MachineName, taskResult.AgentName, taskResult.Task.Id, taskResult.ResultCode,
                                         taskResult.Error == null ? "" : taskResult.Error.Message);
            try
            {
                if (string.IsNullOrEmpty(taskResult.Task.AppId))
                {
                    SnLog.WriteWarning($"AppId is empty for task #{taskResult.Task.Id}.",
                                       EventId.TaskManagement.Lifecycle);
                    return;
                }

                var doesApplicationNeedNotification = !string.IsNullOrWhiteSpace(taskResult.Task.GetFinalizeUrl());
                // first we make sure that the app is accessible by sending a ping request
                if (doesApplicationNeedNotification && !ApplicationHandler.SendPingRequest(taskResult.Task.AppId))
                {
                    var app = ApplicationHandler.GetApplication(taskResult.Task.AppId);

                    SnLog.WriteError(string.Format("Ping request to application {0} ({1}) failed when finalizing task #{2}. Task success: {3}, error: {4}",
                                                   taskResult.Task.AppId,
                                                   app == null ? "unknown app" : app.ApplicationUrl,
                                                   taskResult.Task.Id,
                                                   taskResult.Successful,
                                                   taskResult.Error == null ? "-" : taskResult.Error.ToString()),
                                     EventId.TaskManagement.Communication);

                    doesApplicationNeedNotification = false;
                }

                // remove the task from the database first
                TaskDataHandler.FinalizeTask(taskResult);

                SnTrace.TaskManagement.Write("AgentHub TaskFinished: task {0} has been deleted.", taskResult.Task.Id);

                if (doesApplicationNeedNotification)
                {
                    // This method does not need to be awaited, because we do not want to do anything
                    // with the result, only notify the app that the task has been finished.
                    ApplicationHandler.SendFinalizeNotificationAsync(taskResult);
                }

                // notify monitors
                TaskMonitorHub.OnTaskEvent(taskResult.Successful
                    ? SnTaskEvent.CreateDoneEvent(taskResult.Task.Id, taskResult.Task.Title, taskResult.ResultData, taskResult.Task.AppId, taskResult.Task.Tag, taskResult.MachineName, taskResult.AgentName)
                    : SnTaskEvent.CreateFailedEvent(taskResult.Task.Id, taskResult.Task.Title, taskResult.ResultData, taskResult.Task.AppId, taskResult.Task.Tag, taskResult.MachineName, taskResult.AgentName));
            }
            catch (Exception ex)
            {
                SnLog.WriteException(ex, "AgentHub TaskFinished failed.", EventId.TaskManagement.General);
            }
        }
コード例 #4
0
 public void FinishSubtask(string machineName, string agentName, SnSubtask subtask, SnTask task)
 {
     SnTrace.TaskManagement.Write("AgentHub FinishSubtask. Task id:{0}, agent:{1}, title:{2}", task.Id, agentName, subtask.Title);
     try
     {
         TaskDataHandler.FinishSubtask(machineName, agentName, subtask, task);
         TaskMonitorHub.OnTaskEvent(SnTaskEvent.CreateSubtaskFinishedEvent(task.Id, subtask.Title, subtask.Details, task.AppId, task.Tag, machineName, agentName, subtask.Id));
     }
     catch (Exception ex)
     {
         SnLog.WriteException(ex, "AgentHub FinishSubtask failed.", EventId.TaskManagement.General);
     }
 }
コード例 #5
0
        public void RegisterApplication(RegisterApplicationRequest appRequest)
        {
            try
            {
                var app = TaskDataHandler.RegisterApplication(appRequest);
            }
            catch (Exception ex)
            {
                // the client app needs to be notified
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }

            // invalidate app cache
            ApplicationHandler.Reset();
        }
コード例 #6
0
        //===================================================================== Hub API

        public SnTask GetTask(string machineName, string agentName, string[] capabilities)
        {
            SnTrace.TaskManagement.Write("AgentHub GetTask called. Agent: {0}, capabilities: {1}.", agentName, string.Join(", ", capabilities));

            try
            {
                var task = TaskDataHandler.GetNextAndLock(machineName, agentName, capabilities);
                SnTrace.TaskManagement.Write("AgentHub TaskDataHandler.GetNextAndLock returned with: " + (task == null ? "null" : "task " + task.Id.ToString()));

                // task details are not passed to the monitor yet
                if (task != null)
                {
                    TaskMonitorHub.OnTaskEvent(SnTaskEvent.CreateStartedEvent(task.Id, task.Title, null, task.AppId, task.Tag, machineName, agentName));
                }

                return(task);
            }
            catch (Exception ex)
            {
                SnLog.WriteException(ex, "AgentHub GetTask failed.", EventId.TaskManagement.General);
            }

            return(null);
        }
コード例 #7
0
 public TaskMonitorHub(TaskDataHandler dataHandler)
 {
     _dataHandler = dataHandler;
 }
コード例 #8
0
 public DeadTaskHostedService(IServiceProvider services, ILogger <DeadTaskHostedService> logger, TaskDataHandler dataHandler)
 {
     _services    = services;
     _logger      = logger;
     _dataHandler = dataHandler;
 }
コード例 #9
0
        public RegisterTaskResult RegisterTask(RegisterTaskRequest taskRequest)
        {
            Application app = null;

            try
            {
                // load the corresponding application to make sure the appid is valid
                app = ApplicationHandler.GetApplication(taskRequest.AppId);
            }
            catch (Exception ex)
            {
                SnLog.WriteException(ex, "Error loading app for id " + taskRequest.AppId, EventId.TaskManagement.General);
            }

            // If we do not know the appid, we must not register the task. Client applications
            // must observe this response and try to re-register the application, before
            // trying to register the task again (this can happen if the TaskManagement Web
            // was unreachable when the client application tried to register the appid before).
            if (app == null)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, RegisterTaskRequest.ERROR_UNKNOWN_APPID));
            }

            RegisterTaskResult result = null;

            try
            {
                // calculate hash with the default algrithm if not given
                var hash = taskRequest.Hash == 0
                    ? ComputeTaskHash(taskRequest.Type + taskRequest.AppId + taskRequest.Tag + taskRequest.TaskData)
                    : taskRequest.Hash;

                result = TaskDataHandler.RegisterTask(
                    taskRequest.Type,
                    taskRequest.Title,
                    taskRequest.Priority,
                    taskRequest.AppId,
                    taskRequest.Tag,
                    taskRequest.FinalizeUrl,
                    hash,
                    taskRequest.TaskData,
                    taskRequest.MachineName);
            }
            catch (Exception ex)
            {
                // the client app needs to be notified
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }

            try
            {
                // notify agents
                AgentHub.BroadcastMessage(result.Task);

                // notify monitor clients
                TaskMonitorHub.OnTaskEvent(SnTaskEvent.CreateRegisteredEvent(
                                               result.Task.Id, result.Task.Title, string.Empty, result.Task.AppId,
                                               result.Task.Tag, null, result.Task.Type, result.Task.Order,
                                               result.Task.Hash, result.Task.TaskData));
            }
            catch (Exception ex)
            {
                // The task has been created successfully, this error is only about
                // notification, so client applications should not be notified.
                SnLog.WriteException(ex, "Error during agent or monitor notification after a task was registered.", EventId.TaskManagement.Communication);
            }

            return(result);
        }
コード例 #10
0
 /// <summary>
 /// Loads all task and subtask events for a single task.
 /// </summary>
 /// <param name="appId">Application id to identify the client application.</param>
 /// <param name="tag">If a tag is provided, events will be filtered by it.</param>
 /// <param name="taskId">Id of the task to load events for.</param>
 /// <returns></returns>
 public SnTaskEvent[] GetDetailedTaskEvents(string appId, string tag, int taskId)
 {
     return(TaskDataHandler.GetDetailedTaskEvents(appId, tag, taskId));
 }
コード例 #11
0
        //===================================================================== Hub API

        /// <summary>
        /// Loads all tasks from the database that are registered, but not finished or failed. The real status of
        /// currently in progress tasks will be set with the next progress or event call.
        /// </summary>
        /// <param name="appId">Application id to identify the client application.</param>
        /// <param name="tag">If a tag is provided, events will be filtered by it.</param>
        /// <returns></returns>
        public SnTaskEvent[] GetUnfinishedTasks(string appId, string tag)
        {
            return(TaskDataHandler.GetUnfinishedTasks(appId, tag));
        }
コード例 #12
0
 public void RefreshLock(string machineName, string agentName, int taskId)
 {
     SnTrace.TaskManagement.Write("AgentHub RefreshLock. Agent: {0}, task: {1}.", agentName, taskId);
     TaskDataHandler.RefreshLock(taskId);
 }
コード例 #13
0
 public AgentHub(IHubContext <TaskMonitorHub> monitorHub, ApplicationHandler appHandler, TaskDataHandler dataHandler)
 {
     _monitorHub         = monitorHub;
     _applicationHandler = appHandler;
     _dataHandler        = dataHandler;
 }