mirror of
https://github.com/C9Glax/tranga.git
synced 2025-02-22 23:30:13 +01:00
[postgres-Server-V2] feat(Jobs): Instead of passing context to each job, pass service provider and let the job create its own context
This commit is contained in:
parent
a69e12179b
commit
725813c2f3
@ -124,7 +124,7 @@ using (var scope = app.Services.CreateScope())
|
||||
|
||||
TrangaSettings.Load();
|
||||
Tranga.StartLogger();
|
||||
Tranga.JobStarterThread.Start(app.Services.CreateScope().ServiceProvider.GetService<PgsqlContext>());
|
||||
Tranga.JobStarterThread.Start(app.Services);
|
||||
Tranga.NotificationSenderThread.Start(app.Services.CreateScope().ServiceProvider.GetService<PgsqlContext>());
|
||||
|
||||
app.UseCors("AllowAll");
|
||||
|
@ -43,8 +43,11 @@ public abstract class Job
|
||||
RecurrenceMs = recurrenceMs;
|
||||
}
|
||||
|
||||
public IEnumerable<Job> Run(PgsqlContext context)
|
||||
public IEnumerable<Job> Run(IServiceProvider serviceProvider)
|
||||
{
|
||||
using IServiceScope scope = serviceProvider.CreateScope();
|
||||
PgsqlContext context = scope.ServiceProvider.GetRequiredService<PgsqlContext>();
|
||||
|
||||
this.state = JobState.Running;
|
||||
IEnumerable<Job> newJobs = RunInternal(context);
|
||||
this.state = JobState.Completed;
|
||||
|
@ -56,18 +56,22 @@ public static class Tranga
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
private static void JobStarter(object? pgsqlContext)
|
||||
private static void JobStarter(object? serviceProviderObj)
|
||||
{
|
||||
if(pgsqlContext is null) return;
|
||||
PgsqlContext context = (PgsqlContext)pgsqlContext;
|
||||
if(serviceProviderObj is null) return;
|
||||
IServiceProvider serviceProvider = (IServiceProvider)serviceProviderObj;
|
||||
using IServiceScope scope = serviceProvider.CreateScope();
|
||||
PgsqlContext? context = scope.ServiceProvider.GetService<PgsqlContext>();
|
||||
if (context is null) return;
|
||||
|
||||
string TRANGA = "\n\n _______ \n|_ _|.----..---.-..-----..-----..---.-.\n | | | _|| _ || || _ || _ |\n |___| |__| |___._||__|__||___ ||___._|\n |_____| \n\n";
|
||||
string TRANGA =
|
||||
"\n\n _______ \n|_ _|.----..---.-..-----..-----..---.-.\n | | | _|| _ || || _ || _ |\n |___| |__| |___._||__|__||___ ||___._|\n |_____| \n\n";
|
||||
Log.Info(TRANGA);
|
||||
while (true)
|
||||
{
|
||||
List<Job> completedJobs = context.Jobs.Where(j => j.state == JobState.Completed).ToList();
|
||||
foreach (Job job in completedJobs)
|
||||
if(job.RecurrenceMs <= 0)
|
||||
if (job.RecurrenceMs <= 0)
|
||||
context.Jobs.Remove(job);
|
||||
else
|
||||
{
|
||||
@ -76,15 +80,16 @@ public static class Tranga
|
||||
context.Jobs.Update(job);
|
||||
}
|
||||
|
||||
List<Job> runJobs = context.Jobs.Where(j => j.state <= JobState.Running).ToList().Where(j => j.NextExecution < DateTime.UtcNow).ToList();
|
||||
List<Job> runJobs = context.Jobs.Where(j => j.state <= JobState.Running).ToList()
|
||||
.Where(j => j.NextExecution < DateTime.UtcNow).ToList();
|
||||
foreach (Job job in runJobs)
|
||||
{
|
||||
// If the job is already running, skip it
|
||||
if (RunningJobs.Values.Any(j => j.JobId == job.JobId)) continue;
|
||||
|
||||
Thread t = new (() =>
|
||||
Thread t = new(() =>
|
||||
{
|
||||
IEnumerable<Job> newJobs = job.Run(context);
|
||||
IEnumerable<Job> newJobs = job.Run(serviceProvider);
|
||||
context.Jobs.AddRange(newJobs);
|
||||
});
|
||||
RunningJobs.Add(t, job);
|
||||
|
Loading…
x
Reference in New Issue
Block a user