public void HandleTaskRequest(PacketWorkerTaskRequest packet) { var isAccepting = _network.GetWorker().IsStandbye(); if (!isAccepting) { Logger.Info($"Server give a new task {packet.GetTask()}, decline, we are busy"); _network.SendPacket(new PacketWorkerTaskResponse(packet.GetTask(), false)); return; } Logger.Info($"Server give a new task {packet.GetTask()}, accepting"); _network.SendPacket(new PacketWorkerTaskResponse(packet.GetTask(), true)); var task = packet.GetTask(); var job = task.ParentJob; foreach (var jobFile in job.JobFiles) { if (jobFile.Direction == EGridJobFileDirection.WorkerOutput) { continue; } if (!GridIo.IsJobTaskFileExistsAndValid(task, jobFile)) { Logger.Debug($"Request file '{jobFile}' from server for task '{task}' (not exists locally)"); _network.SendPacket(new PacketWorkerFileRequest(task.TaskId, job.Name, jobFile)); } } if (!_network.GetWorker().RunNewTask(packet.GetTask())) { Logger.Info("Worker is already running a task (or receive packet twice), refuse"); _network.SendPacket(new PacketWorkerTaskCancel(packet.GetTask())); } }
public void TickMain() { _taskWaitEvent.WaitOne(); if (_activeTask == null) { _taskWaitEvent.Reset(); return; } if (_activeTask.State == EGridJobTaskState.Created) { GridIo.CleanupTasksDirectoriesOnly(_activeTask.ParentJob); _networkSystem.CleanupWaitFiles(); Logger.Info($"New task {_activeTask} is received from server"); _activeTask.State = EGridJobTaskState.Running; foreach (var refFile in _activeTask.ReferencedFiles) { var fileOriginal = refFile.ResolveLink(_activeTask.ParentJob); if (fileOriginal == null) { Logger.Error($"Unable to resolve referenced file {refFile}"); _activeTask.State = EGridJobTaskState.RunningFailed; return; } var reqFiles = 0; if (!GridIo.IsJobTaskFileExistsAndValid(_activeTask, fileOriginal)) { _networkSystem.AddWaitFile(fileOriginal); Logger.Info($"Requesting {fileOriginal} from server (not exists locally, or checksum invalid)"); _networkSystem.SendPacket(new PacketWorkerFileRequest(_activeTask.TaskId, _activeTask.ParentJob.Name, fileOriginal)); reqFiles++; } if (reqFiles > 0) { Logger.Info($"Wait {reqFiles} files to download from server"); } } while (true) { if (_networkSystem.AllWaitFilesReceived()) { Logger.Info("All files for task are received"); break; } } try { Logger.Info($"Processing task {_activeTask}"); _activeTask.ExecuteModuleTask(new GridTaskExecutor(), Logger); _activeTask.State = EGridJobTaskState.RunningFinished; Logger.Info($"Task {_activeTask} is finished"); } catch (GridJobTaskCommandException e) { _activeTask.State = EGridJobTaskState.RunningFailed; Logger.Error("Task execute internal error", e); } catch (Exception e) { _activeTask.State = EGridJobTaskState.RunningFailed; Logger.Error("Task execute unexpected error", e); } } if (_activeTask.State == EGridJobTaskState.RunningFinished || _activeTask.State == EGridJobTaskState.RunningFailed) { try { if (_activeTask.State == EGridJobTaskState.RunningFinished) { Logger.Info($"Sending task {_activeTask} output files"); SendOutputFiles(); } _networkSystem.SendPacket(new PacketWorkerTaskFinish(_activeTask)); } catch (Exception e) { Logger.Error("Unable to send task finished packet", e); } finally { _activeTask = null; } } }