Exemplo n.º 1
0
        //===================================================================== Hub API

        public async Task <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 = await _dataHandler.GetNextAndLock(machineName, agentName, capabilities, Context.ConnectionAborted)
                           .ConfigureAwait(false);

                SnTrace.TaskManagement.Write("AgentHub TaskDataHandler.GetNextAndLock returned: " + (task == null ? "null" : "task " + task.Id));

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

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

            return(null);
        }
Exemplo n.º 2
0
        public async Task <RegisterTaskResult> RegisterTask([FromBody] 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)
            {
                return(new RegisterTaskResult {
                    Error = RegisterTaskRequest.ERROR_UNKNOWN_APPID
                });
            }

            RegisterTaskResult result;

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

                result = await _dataHandler.RegisterTaskAsync(
                    taskRequest.Type,
                    taskRequest.Title,
                    taskRequest.Priority,
                    taskRequest.AppId,
                    taskRequest.Tag,
                    taskRequest.FinalizeUrl,
                    hash,
                    taskRequest.TaskData,
                    taskRequest.MachineName,
                    HttpContext.RequestAborted).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                var msg = $"Task registration failed. {ex.Message} AppId: {app.AppId}, " +
                          $"Task: {taskRequest.Type}, Title: {taskRequest.Title}";

                SnLog.WriteException(ex, msg);

                return(new RegisterTaskResult {
                    Error = RegisterTaskResult.ErrorTaskRegistrationFailed
                });
            }

            try
            {
                // notify agents
                await _agentHub.BroadcastNewTask(result.Task).ConfigureAwait(false);

                // notify monitor clients
                await _monitorHub.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)).ConfigureAwait(false);
            }
            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);
        }