mirror of
https://github.com/C9Glax/tranga.git
synced 2025-02-23 07:40:13 +01:00
Scoped PGSql Contexts for Threads
This commit is contained in:
parent
c4fc2f436b
commit
03e89913e3
@ -128,8 +128,8 @@ using (var scope = app.Services.CreateScope())
|
|||||||
|
|
||||||
TrangaSettings.Load();
|
TrangaSettings.Load();
|
||||||
Tranga.StartLogger();
|
Tranga.StartLogger();
|
||||||
Tranga.JobStarterThread.Start(app.Services.GetService<PgsqlContext>()!);
|
Tranga.JobStarterThread.Start(app.Services.CreateScope().ServiceProvider.GetService<PgsqlContext>());
|
||||||
Tranga.NotificationSenderThread.Start(app.Services.GetService<PgsqlContext>()!);
|
Tranga.NotificationSenderThread.Start(app.Services.CreateScope().ServiceProvider.GetService<PgsqlContext>());
|
||||||
|
|
||||||
app.UseCors("AllowAll");
|
app.UseCors("AllowAll");
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ public class DownloadNewChaptersJob(ulong recurrenceMs, string mangaId, string?
|
|||||||
public string MangaId { get; init; } = mangaId;
|
public string MangaId { get; init; } = mangaId;
|
||||||
public virtual Manga Manga { get; init; }
|
public virtual Manga Manga { get; init; }
|
||||||
|
|
||||||
public override IEnumerable<Job> Run()
|
protected override IEnumerable<Job> RunInternal()
|
||||||
{
|
{
|
||||||
MangaConnector connector = Manga.MangaConnector;
|
MangaConnector connector = Manga.MangaConnector;
|
||||||
Chapter[] newChapters = connector.GetNewChapters(Manga);
|
Chapter[] newChapters = connector.GetNewChapters(Manga);
|
||||||
|
@ -18,7 +18,7 @@ public class DownloadSingleChapterJob(string chapterId, string? parentJobId = nu
|
|||||||
public string ChapterId { get; init; } = chapterId;
|
public string ChapterId { get; init; } = chapterId;
|
||||||
public virtual Chapter Chapter { get; init; }
|
public virtual Chapter Chapter { get; init; }
|
||||||
|
|
||||||
public override IEnumerable<Job> Run()
|
protected override IEnumerable<Job> RunInternal()
|
||||||
{
|
{
|
||||||
MangaConnector connector = Chapter.ParentManga.MangaConnector;
|
MangaConnector connector = Chapter.ParentManga.MangaConnector;
|
||||||
DownloadChapterImages(Chapter);
|
DownloadChapterImages(Chapter);
|
||||||
|
@ -36,5 +36,13 @@ public abstract class Job
|
|||||||
NextExecution = LastExecution.AddMilliseconds(RecurrenceMs);
|
NextExecution = LastExecution.AddMilliseconds(RecurrenceMs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract IEnumerable<Job> Run();
|
public IEnumerable<Job> Run()
|
||||||
|
{
|
||||||
|
this.state = JobState.Running;
|
||||||
|
IEnumerable<Job>? newJobs = RunInternal();
|
||||||
|
this.state = JobState.Completed;
|
||||||
|
return newJobs;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract IEnumerable<Job> RunInternal();
|
||||||
}
|
}
|
@ -6,7 +6,7 @@ public class MoveFileOrFolderJob(string fromLocation, string toLocation, string?
|
|||||||
public string FromLocation { get; init; } = fromLocation;
|
public string FromLocation { get; init; } = fromLocation;
|
||||||
public string ToLocation { get; init; } = toLocation;
|
public string ToLocation { get; init; } = toLocation;
|
||||||
|
|
||||||
public override IEnumerable<Job> Run()
|
protected override IEnumerable<Job> RunInternal()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ public class UpdateMetadataJob(ulong recurrenceMs, string mangaId, string? paren
|
|||||||
public string MangaId { get; init; } = mangaId;
|
public string MangaId { get; init; } = mangaId;
|
||||||
public virtual Manga Manga { get; init; }
|
public virtual Manga Manga { get; init; }
|
||||||
|
|
||||||
public override IEnumerable<Job> Run()
|
protected override IEnumerable<Job> RunInternal()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using API.Schema;
|
using API.Schema;
|
||||||
|
using API.Schema.Jobs;
|
||||||
using API.Schema.NotificationConnectors;
|
using API.Schema.NotificationConnectors;
|
||||||
using log4net;
|
using log4net;
|
||||||
using log4net.Config;
|
using log4net.Config;
|
||||||
@ -9,7 +10,8 @@ public static class Tranga
|
|||||||
{
|
{
|
||||||
public static Thread NotificationSenderThread { get; } = new (NotificationSender);
|
public static Thread NotificationSenderThread { get; } = new (NotificationSender);
|
||||||
public static Thread JobStarterThread { get; } = new (JobStarter);
|
public static Thread JobStarterThread { get; } = new (JobStarter);
|
||||||
private static ILog Log = LogManager.GetLogger(typeof(Tranga));
|
private static readonly List<Thread> RunningJobs = new();
|
||||||
|
private static readonly ILog Log = LogManager.GetLogger(typeof(Tranga));
|
||||||
|
|
||||||
internal static void StartLogger()
|
internal static void StartLogger()
|
||||||
{
|
{
|
||||||
@ -63,7 +65,36 @@ public static class Tranga
|
|||||||
Log.Info(TRANGA);
|
Log.Info(TRANGA);
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
List<Job> completedJobs = context.Jobs.Where(j => j.state == JobState.Completed).ToList();
|
||||||
|
foreach (Job job in completedJobs)
|
||||||
|
if(job.RecurrenceMs < 1)
|
||||||
|
context.Jobs.Remove(job);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
job.LastExecution = DateTime.UtcNow;
|
||||||
|
job.state = JobState.Waiting;
|
||||||
|
context.Jobs.Update(job);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Job> runJobs = context.Jobs.Where(j => j.state <= JobState.Running && j.NextExecution < DateTime.UtcNow).ToList();
|
||||||
|
foreach (Job job in runJobs)
|
||||||
|
{
|
||||||
|
Thread t = new (() =>
|
||||||
|
{
|
||||||
|
IEnumerable<Job> newJobs = job.Run();
|
||||||
|
context.Jobs.AddRange(newJobs);
|
||||||
|
});
|
||||||
|
RunningJobs.Add(t);
|
||||||
|
t.Start();
|
||||||
|
context.Jobs.Update(job);
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread[] removeFromThreadsList = RunningJobs.Where(t => !t.IsAlive).ToArray();
|
||||||
|
foreach (Thread thread in removeFromThreadsList)
|
||||||
|
RunningJobs.Remove(thread);
|
||||||
|
|
||||||
|
context.SaveChanges();
|
||||||
|
Thread.Sleep(2000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user