private MessageFromServerToClient HandleFailureReport([NotNull] MessageFromClientToServer req)
        {
            var task = ActiveTasks.FirstOrDefault(x => x.Guid == req.TaskGuid);

            if (task == null)
            {
                throw new DistSimException("Invalid guid: " + req.TaskGuid);
            }

            task.FinishStatusMessage = req.ClientRequest + " " + req.Message;
            SaveExecuteHelper.Get().SaveExecuteWithWait(() => ActiveTasks.Remove(task));
            SaveExecuteHelper.Get().SaveExecuteWithWait(() => FinishedTasks.Add(task));
            _errorLogger.Error(task.OriginalJsonFilePath + " - no output at all at client " + req.ClientName, _threadId);
            Logger.Error(task.OriginalJsonFilePath + " - no output at all at client " + req.ClientName, _threadId);
            return(new MessageFromServerToClient(ServerResponseEnum.JobFinishAck, req.TaskGuid));
        }
        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;
            }
        }