示例#1
0
        private void HandleJobTestLoad(string[] args)
        {
            var jobFileName = args.Length <= 0 ? "job-generated.json" : args[0];
            var job         = GridJob.Deserialize(File.ReadAllText(jobFileName));

            foreach (var task in job.JobTasks)
            {
                task.ParentJob = job;
            }

            GridIo.CreateDirectoriesForJob(job);

            foreach (var file in job.JobFiles)
            {
                if (file.Direction == EGridJobFileDirection.WorkerInput)
                {
                    var localPath = GridIo.ResolveFilePath(job, null, file);
                    file.Bytes = File.ReadAllBytes(localPath);
                    file.UpdateCheckSum();
                }
            }

            if (Server.IsAnyJobActive())
            {
                Logger.Warn($"Server has already executing job {Server.GetActiveJob()}, new job will be added to the queue");
            }

            Logger.Info($"Queue new job {job}");
            Server.RunQueueJob(job);
        }
示例#2
0
        public void HandleFileData(PacketWorkerFileData packet)
        {
            packet.UpdateCheckSum();
            var reqFile = packet.GetFile();
            var task    = _network.GetActiveTask();

            if (task == null)
            {
                Logger.Warn($"Handle file data '{packet.GetFile()}' when active task is null");
                return;
            }

            if (task.TaskId != packet.GetTaskId() || task.ParentJob.Name != packet.GetJobName())
            {
                Logger.Warn($"Received file data for unknown task [Id={packet.GetTaskId()}, JobName={packet.GetJobName()}]");
                return;
            }

            foreach (var fileRef in task.ParentJob.JobFiles)
            {
                if (fileRef.FileName == reqFile.FileName)
                {
                    fileRef.Bytes = packet.GetData();
                    fileRef.UpdateCheckSum();

                    Logger.Debug($"Store task '{task}' file '{fileRef}'");
                    GridIo.StoreJobTaskFile(task, fileRef);
                    _netSystem.WaitFileReceived(fileRef);
                    return;
                }
            }
        }
示例#3
0
        private void SendOutputFiles()
        {
            var outFiles = _activeTask.ParentJob.JobFiles.Where(x => x.Direction == EGridJobFileDirection.WorkerOutput);

            foreach (var outFile in outFiles)
            {
                outFile.Bytes    = File.ReadAllBytes(GridIo.ResolveFilePath(_activeTask, outFile));
                outFile.CheckSum = CryptoUtils.CrcOfBytes(outFile.Bytes);
                _networkSystem.SendPacket(new PacketWorkerFileData(_activeTask, outFile));
            }
        }
示例#4
0
        public override void FinishJob(GridJob job, ILog logger)
        {
            var count = 0;

            foreach (var task in job.JobTasks)
            {
                if (task.OutputFile?.Bytes != null)
                {
                    try {
                        count += int.Parse(Encoding.UTF8.GetString(task.OutputFile.Bytes));
                    } catch {
                        ;
                    }
                }
            }

            logger.Info($"Total count is '{count}'");
            File.WriteAllText(GridIo.ResolveFilePathShared(job, job.JobOutFile.FileName), $"{count}");
        }
示例#5
0
        public override void ExecuteTask(GridTaskExecutor executor, GridJobTask task, ILog logger)
        {
            var inputFilePath = GridIo.ResolveFilePathShared(task.ParentJob, task.Arguments[0]);

            if (!File.Exists(inputFilePath))
            {
                throw new Exception($"Unable to load task input file '{task.Arguments[0]}', not found by present path '{inputFilePath}'");
            }

            byte[] buffer;
            using (var fs = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read)) {
                var startPos = long.Parse(task.Arguments[1]);
                var endPos   = long.Parse(task.Arguments[2]);

                buffer = new byte[endPos - startPos];
                fs.Seek(startPos, SeekOrigin.Begin);
                fs.Read(buffer, 0, buffer.Length);
            }

            var needleCount  = 0;
            var needleString = task.Arguments[4];

            using (var sr = new StreamReader(new MemoryStream(buffer))) {
                var all = sr.ReadToEnd();

                for (var i = 0; i < all.Length; i++)
                {
                    if (SearchSubstringAt(all, needleString, i))
                    {
                        needleCount++;
                        i += needleString.Length;
                    }
                }
            }

            logger.Info($"Find '{needleCount}' in block");
            executor.WriteFileOutput(task, task.Arguments[3], $"{needleCount}");
        }
示例#6
0
        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()));
            }
        }
示例#7
0
        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;
                }
            }
        }