private MessageFromServerToClient HandleRequestForJob([NotNull] MessageFromClientToServer req)
        {
            if (OpenTasks.IsEmpty)
            {
                return(new MessageFromServerToClient(ServerResponseEnum.NothingToDo, req.TaskGuid));
            }

            bool success = OpenTasks.TryDequeue(out var task);

            if (success)
            {
                SaveExecuteHelper.Get().SaveExecuteWithWait(() => ActiveTasks.Add(task));
                task.Client         = req.ClientName;
                task.ExecutionStart = DateTime.Now;
                var answer = new MessageFromServerToClient(ServerResponseEnum.ServeCalcJob, task.Guid);
                answer.HouseJobStr = File.ReadAllText(task.OriginalJsonFilePath ??
                                                      throw new DistSimException("Jsonpath was not found: " + task.OriginalJsonFilePath));
                FileInfo fi = new FileInfo(task.OriginalJsonFilePath);
                answer.OriginalFileName = fi.Name;
                answer.LpgFiles         = InitalizeLpgFiles(false);
                return(answer);
            }

            return(new MessageFromServerToClient(ServerResponseEnum.NothingToDo, req.TaskGuid));
        }
Beispiel #2
0
        public void TrackLastAnswer([NotNull] string reqClientName, [NotNull] MessageFromServerToClient answer)
        {
            string cleanname = CleanClientName(reqClientName);
            var    client    = ClientStatus.FirstOrDefault(x => x.ClientName == cleanname);

            if (client == null)
            {
                client = new ServerClientStatusTracker(cleanname);
                SaveExecuteHelper.Get().SaveExecuteWithWait(() => ClientStatus.Add(client));
            }

            client.LastRequestTime = DateTime.Now;
            client.LastTask        = answer.ServerResponse.ToString();
        }
 private MessageFromServerToClient HandleRequestForLpgFiles([NotNull] MessageFromClientToServer req)
 {
     _logger.Info("trying to collect lpg files for " + req.ClientName, _threadId);
     try {
         MessageFromServerToClient answer = new MessageFromServerToClient(ServerResponseEnum.ServeLpgFiles, req.TaskGuid);
         answer.LpgFiles = InitalizeLpgFiles(true);
         _logger.Info("sending lpg files to client " + req.ClientName + " total files", _threadId);
         return(answer);
     }
     catch (Exception ex) {
         _logger.Exception(ex, "Trying to collect files");
         throw;
     }
 }
        private void AnswerRequest([NotNull] ResponseSocket socket, [NotNull] MessageFromServerToClient answer)
        {
            _logger.Info("Sent: " + answer.ServerResponse, _threadId);
            Stopwatch sw = Stopwatch.StartNew();

            byte[]   messageBytes = LZ4MessagePackSerializer.Serialize(answer);
            TimeSpan ts           = new TimeSpan(0, 1, 0);
            bool     success      = socket.TrySendFrame(ts, messageBytes);

            if (!success)
            {
                throw new DistSimException("Failed to transmit answer");
            }

            sw.Stop();
        }
        private MessageFromServerToClient HandleTaskFinishReport([NotNull] MessageFromClientToServer req)
        {
            try {
                _logger.Info("got a finish report from " + req.ClientName + " for " + req.HouseName, _threadId);
                ServerExecutionTask set = ActiveTasks.FirstOrDefault(x => x.Guid == req.TaskGuid);
                if (set == null)
                {
                    return(new MessageFromServerToClient(ServerResponseEnum.JobFinishAck, req.TaskGuid));
                    //if it is an old task guid from previous run, just ignore the finish report
                    //throw new DistSimException("No task found for guid " + req.TaskGuid);
                }

                set.ExecutionEnd = DateTime.Now;
                SaveExecuteHelper.Get().SaveExecuteWithWait(() => ActiveTasks.Remove(set));
                SaveExecuteHelper.Get().SaveExecuteWithWait(() => {
                    FinishedTasks.Add(set);
                    while (FinishedTasks.Count > 20)
                    {
                        FinishedTasks.RemoveAt(0);
                    }
                });
                set.FinishStatusMessage = req.Message;
                var           resultFileArchiveDirectory = MakeResultFileDirectory(req);
                DirectoryInfo di = new DirectoryInfo(resultFileArchiveDirectory);
                if (di.Exists)
                {
                    _logger.Info("deleting previous results from  " + di.FullName, _threadId);
                    SaveDelete(di);
                    Thread.Sleep(250);
                }

                di.Create();
                _logger.Info("created " + di.FullName, _threadId);
                bool isFailure = false;
                if (req.ResultFiles != null)
                {
                    foreach (var jsonfile in req.ResultFiles)
                    {
                        string        directory = Path.Combine(resultFileArchiveDirectory, jsonfile.RelativeDirectory);
                        DirectoryInfo subdir    = new DirectoryInfo(directory);
                        if (!subdir.Exists)
                        {
                            subdir.Create();
                        }

                        string dstPath = Path.Combine(directory, jsonfile.FileName);
                        _logger.Info("writing " + dstPath + "," + "\nresult archive dir: " + resultFileArchiveDirectory + "\n relative dir: " +
                                     jsonfile.RelativeDirectory,
                                     _threadId);
                        jsonfile.WriteBytesFromJson(dstPath, Logger);
                        if (jsonfile.FileName.ToLower(CultureInfo.InvariantCulture) == "calculationexceptions.txt")
                        {
                            string fileContent = File.ReadAllText(dstPath);
                            _errorLogger.Error(set.OriginalJsonFilePath + " - error during calc " + fileContent, _threadId);
                            _logger.Error(set.OriginalJsonFilePath + " - error during calc " + fileContent, _threadId);
                            isFailure = true;
                        }
                    }
                }
                else
                {
                    _logger.Error("No result files were delivered", _threadId);
                }

                ArchiveJsonFile(set, isFailure);
                var answer = new MessageFromServerToClient(ServerResponseEnum.JobFinishAck, req.TaskGuid);
                return(answer);
            }
            catch (Exception ex) {
                _logger.Exception(ex, "Error while handling a finish report  " + ex.Message);
                throw;
            }
        }