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); }
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); }