Compare commits

..

No commits in common. "4156365b183551dbef7dc71588c24503ea2f6986" and "f08b9e85ec82a66959d4b83edd82a4292fc2738d" have entirely different histories.

View File

@ -27,7 +27,7 @@ public class JobBoss : GlobalBase
{ {
Log($"Added {job}"); Log($"Added {job}");
this.jobs.Add(job); this.jobs.Add(job);
UpdateJobFile(job); ExportJob(job);
} }
} }
@ -37,10 +37,6 @@ public class JobBoss : GlobalBase
AddJob(job); AddJob(job);
} }
/// <summary>
/// Compares contents of the provided job and all current jobs
/// Does not check if objects are the same
/// </summary>
public bool ContainsJobLike(Job job) public bool ContainsJobLike(Job job)
{ {
if (job is DownloadChapter dcJob) if (job is DownloadChapter dcJob)
@ -61,14 +57,13 @@ public class JobBoss : GlobalBase
this.jobs.Remove(job); this.jobs.Remove(job);
if(job.subJobs is not null && job.subJobs.Any()) if(job.subJobs is not null && job.subJobs.Any())
RemoveJobs(job.subJobs); RemoveJobs(job.subJobs);
UpdateJobFile(job); ExportJob(job);
} }
public void RemoveJobs(IEnumerable<Job?> jobsToRemove) public void RemoveJobs(IEnumerable<Job?> jobsToRemove)
{ {
List<Job?> toRemove = jobsToRemove.ToList(); //Prevent multiple enumeration Log($"Removing {jobsToRemove.Count()} jobs.");
Log($"Removing {toRemove.Count()} jobs."); foreach (Job? job in jobsToRemove)
foreach (Job? job in toRemove)
if(job is not null) if(job is not null)
RemoveJob(job); RemoveJob(job);
} }
@ -127,52 +122,47 @@ public class JobBoss : GlobalBase
private bool QueueContainsJob(Job job) private bool QueueContainsJob(Job job)
{ {
if (mangaConnectorJobQueue.TryAdd(job.mangaConnector, new Queue<Job>()))//If we can add the queue, there is certainly no job in it mangaConnectorJobQueue.TryAdd(job.mangaConnector, new Queue<Job>());
return true;
return mangaConnectorJobQueue[job.mangaConnector].Contains(job); return mangaConnectorJobQueue[job.mangaConnector].Contains(job);
} }
public void AddJobToQueue(Job job) public void AddJobToQueue(Job job)
{ {
Log($"Adding Job to Queue. {job}"); Log($"Adding Job to Queue. {job}");
if(!QueueContainsJob(job)) mangaConnectorJobQueue.TryAdd(job.mangaConnector, new Queue<Job>());
mangaConnectorJobQueue[job.mangaConnector].Enqueue(job); Queue<Job> connectorJobQueue = mangaConnectorJobQueue[job.mangaConnector];
if(!connectorJobQueue.Contains(job))
connectorJobQueue.Enqueue(job);
job.ExecutionEnqueue(); job.ExecutionEnqueue();
} }
private void AddJobsToQueue(IEnumerable<Job> newJobs) public void AddJobsToQueue(IEnumerable<Job> jobs)
{ {
foreach(Job job in newJobs) foreach(Job job in jobs)
AddJobToQueue(job); AddJobToQueue(job);
} }
private void LoadJobsList(HashSet<MangaConnector> connectors) public void LoadJobsList(HashSet<MangaConnector> connectors)
{
if (!Directory.Exists(settings.jobsFolderPath)) //No jobs to load
{ {
Directory.CreateDirectory(settings.jobsFolderPath); Directory.CreateDirectory(settings.jobsFolderPath);
return;
}
Regex idRex = new (@"(.*)\.json"); Regex idRex = new (@"(.*)\.json");
//Load json-job-files foreach (FileInfo file in new DirectoryInfo(settings.jobsFolderPath).EnumerateFiles())
foreach (FileInfo file in new DirectoryInfo(settings.jobsFolderPath).EnumerateFiles().Where(fileInfo => idRex.IsMatch(fileInfo.Name))) if (idRex.IsMatch(file.Name))
{ {
Job job = JsonConvert.DeserializeObject<Job>(File.ReadAllText(file.FullName), Job job = JsonConvert.DeserializeObject<Job>(File.ReadAllText(file.FullName),
new JobJsonConverter(this, new MangaConnectorJsonConverter(this, connectors)))!; new JobJsonConverter(this, new MangaConnectorJsonConverter(this, connectors)))!;
this.jobs.Add(job); this.jobs.Add(job);
} }
//Connect jobs to parent-jobs and add Publications to cache
foreach (Job job in this.jobs) foreach (Job job in this.jobs)
{
this.jobs.FirstOrDefault(jjob => jjob.id == job.parentJobId)?.AddSubJob(job); this.jobs.FirstOrDefault(jjob => jjob.id == job.parentJobId)?.AddSubJob(job);
if(job is DownloadNewChapters dncJob)
cachedPublications.Add(dncJob.manga); foreach (DownloadNewChapters ncJob in this.jobs.Where(job => job is DownloadNewChapters))
} cachedPublications.Add(ncJob.manga);
} }
private void UpdateJobFile(Job job) public void ExportJob(Job job)
{ {
string jobFilePath = Path.Join(settings.jobsFolderPath, $"{job.id}.json"); string jobFilePath = Path.Join(settings.jobsFolderPath, $"{job.id}.json");
@ -200,11 +190,11 @@ public class JobBoss : GlobalBase
} }
} }
private void UpdateAllJobFiles() public void ExportJobsList()
{ {
Log("Exporting Jobs"); Log("Exporting Jobs");
foreach (Job job in this.jobs) foreach (Job job in this.jobs)
UpdateJobFile(job); ExportJob(job);
//Remove files with jobs not in this.jobs-list //Remove files with jobs not in this.jobs-list
Regex idRex = new (@"(.*)\.json"); Regex idRex = new (@"(.*)\.json");
@ -238,10 +228,9 @@ public class JobBoss : GlobalBase
Job queueHead = jobQueue.Peek(); Job queueHead = jobQueue.Peek();
if (queueHead.progressToken.state is ProgressToken.State.Complete or ProgressToken.State.Cancelled) if (queueHead.progressToken.state is ProgressToken.State.Complete or ProgressToken.State.Cancelled)
{ {
queueHead.ResetProgress();
if(!queueHead.recurring) if(!queueHead.recurring)
RemoveJob(queueHead); RemoveJob(queueHead);
else
queueHead.ResetProgress();
jobQueue.Dequeue(); jobQueue.Dequeue();
Log($"Next job in {jobs.MinBy(job => job.nextExecution)?.nextExecution.Subtract(DateTime.Now)} {jobs.MinBy(job => job.nextExecution)?.id}"); Log($"Next job in {jobs.MinBy(job => job.nextExecution)?.nextExecution.Subtract(DateTime.Now)} {jobs.MinBy(job => job.nextExecution)?.id}");
}else if (queueHead.progressToken.state is ProgressToken.State.Standby) }else if (queueHead.progressToken.state is ProgressToken.State.Standby)