From d52213002ee370d09714b3455bd156e9ee6736e2 Mon Sep 17 00:00:00 2001 From: Glax Date: Thu, 25 Apr 2024 21:24:11 +0200 Subject: [PATCH] Delete old jobfiles. --- Tranga/Jobs/JobBoss.cs | 29 +++++++++++++++++++++-------- Tranga/Jobs/UpdateMetadata.cs | 9 ++++++++- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/Tranga/Jobs/JobBoss.cs b/Tranga/Jobs/JobBoss.cs index 75e01a6..637b3ae 100644 --- a/Tranga/Jobs/JobBoss.cs +++ b/Tranga/Jobs/JobBoss.cs @@ -171,18 +171,18 @@ public class JobBoss : GlobalBase } } - internal void UpdateJobFile(Job job) + internal void UpdateJobFile(Job job, string? oldFile = null) { - string jobFilePath = Path.Join(settings.jobsFolderPath, $"{job.id}.json"); + string newJobFilePath = Path.Join(settings.jobsFolderPath, $"{job.id}.json"); if (!this.jobs.Any(jjob => jjob.id == job.id)) { try { - Log($"Deleting Job-file {jobFilePath}"); - while(IsFileInUse(jobFilePath)) + Log($"Deleting Job-file {newJobFilePath}"); + while(IsFileInUse(newJobFilePath)) Thread.Sleep(10); - File.Delete(jobFilePath); + File.Delete(newJobFilePath); } catch (Exception e) { @@ -191,12 +191,25 @@ public class JobBoss : GlobalBase } else { - Log($"Exporting Job {jobFilePath}"); + Log($"Exporting Job {newJobFilePath}"); string jobStr = JsonConvert.SerializeObject(job); - while(IsFileInUse(jobFilePath)) + while(IsFileInUse(newJobFilePath)) Thread.Sleep(10); - File.WriteAllText(jobFilePath, jobStr); + File.WriteAllText(newJobFilePath, jobStr); } + + if(oldFile is not null) + try + { + Log($"Deleting old Job-file {oldFile}"); + while(IsFileInUse(oldFile)) + Thread.Sleep(10); + File.Delete(oldFile); + } + catch (Exception e) + { + Log(e.ToString()); + } } private void UpdateAllJobFiles() diff --git a/Tranga/Jobs/UpdateMetadata.cs b/Tranga/Jobs/UpdateMetadata.cs index 9fab4dc..4dc3ae7 100644 --- a/Tranga/Jobs/UpdateMetadata.cs +++ b/Tranga/Jobs/UpdateMetadata.cs @@ -38,13 +38,20 @@ public class UpdateMetadata : Job this.mangaConnector.CopyCoverFromCacheToDownloadLocation(manga); foreach (Job job in jobBoss.GetJobsLike(publication: this.manga)) { + string oldFile; if (job is DownloadNewChapters dc) + { + oldFile = dc.id; dc.manga = this.manga; + } else if (job is UpdateMetadata um) + { + oldFile = um.id; um.manga = this.manga; + } else continue; - jobBoss.UpdateJobFile(job); + jobBoss.UpdateJobFile(job, oldFile); } this.progressToken.Complete(); }