private static void ArchiveJsonFile([NotNull] ServerExecutionTask set, bool isFailure) { FileInfo jsonFi = new FileInfo(set.OriginalJsonFilePath ?? throw new DistSimException("Json path was null")); string path = "Finished"; if (isFailure) { path = "FailedCalculations"; } string jsonArchive = Path.Combine(jsonFi.DirectoryName ?? throw new DistSimException("no directory"), path); DirectoryInfo ja = new DirectoryInfo(jsonArchive); if (!ja.Exists) { ja.Create(); } string jsonArchiveFn = Path.Combine(ja.FullName, jsonFi.Name); set.ArchivedJsonFilePath = jsonArchiveFn; FileInfo jsonArchiveFi = new FileInfo(jsonArchiveFn); if (jsonArchiveFi.Exists) { jsonArchiveFi.Delete(); } jsonFi.MoveTo(jsonArchiveFn); }
public void RefreshOpenTasks() { if (_settings.ServerSettings.JsonDirectory == null) { throw new DistSimException("Jsondirectory was null"); } if (_settings.ServerSettings.JsonDirectory.Count == 0) { throw new DistSimException("Jsondirectory was empty"); } foreach (string singledir in _settings.ServerSettings.JsonDirectory) { if (OpenTasks.Count > 1000) { break; } DirectoryInfo di = new DirectoryInfo(singledir); if (!di.Exists) { di.Create(); Thread.Sleep(100); } var files = di.GetFiles("*.json"); var activeFiles = ActiveTasks.Select(x => x.OriginalJsonFilePath).ToList(); var openTaskFiles = OpenTasks.Select(x => x.OriginalJsonFilePath).ToList(); foreach (var fileInfo in files) { if (activeFiles.Contains(fileInfo.FullName)) { _logger.Info("Currently processing " + fileInfo.Name, _threadId); continue; } if (openTaskFiles.Contains(fileInfo.FullName)) { _logger.Info("Currently in queue " + fileInfo.Name, _threadId); continue; } ServerExecutionTask set = new ServerExecutionTask(fileInfo.FullName, fileInfo.Name, Guid.NewGuid().ToString()); OpenTasks.Enqueue(set); _logger.Info("Created a job for " + fileInfo.Name, _threadId); if (OpenTasks.Count > 1000) { break; } } } }
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; } }