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();
|
TrangaSettings.Load();
|
||||||
Tranga.StartLogger();
|
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>());
|
Tranga.NotificationSenderThread.Start(app.Services.CreateScope().ServiceProvider.GetService<PgsqlContext>());
|
||||||
|
|
||||||
app.UseCors("AllowAll");
|
app.UseCors("AllowAll");
|
||||||
|
@ -43,8 +43,11 @@ public abstract class Job
|
|||||||
RecurrenceMs = recurrenceMs;
|
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;
|
this.state = JobState.Running;
|
||||||
IEnumerable<Job> newJobs = RunInternal(context);
|
IEnumerable<Job> newJobs = RunInternal(context);
|
||||||
this.state = JobState.Completed;
|
this.state = JobState.Completed;
|
||||||
|
@ -56,18 +56,22 @@ public static class Tranga
|
|||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void JobStarter(object? pgsqlContext)
|
private static void JobStarter(object? serviceProviderObj)
|
||||||
{
|
{
|
||||||
if(pgsqlContext is null) return;
|
if(serviceProviderObj is null) return;
|
||||||
PgsqlContext context = (PgsqlContext)pgsqlContext;
|
IServiceProvider serviceProvider = (IServiceProvider)serviceProviderObj;
|
||||||
|
using IServiceScope scope = serviceProvider.CreateScope();
|
||||||
string TRANGA = "\n\n _______ \n|_ _|.----..---.-..-----..-----..---.-.\n | | | _|| _ || || _ || _ |\n |___| |__| |___._||__|__||___ ||___._|\n |_____| \n\n";
|
PgsqlContext? context = scope.ServiceProvider.GetService<PgsqlContext>();
|
||||||
|
if (context is null) return;
|
||||||
|
|
||||||
|
string TRANGA =
|
||||||
|
"\n\n _______ \n|_ _|.----..---.-..-----..-----..---.-.\n | | | _|| _ || || _ || _ |\n |___| |__| |___._||__|__||___ ||___._|\n |_____| \n\n";
|
||||||
Log.Info(TRANGA);
|
Log.Info(TRANGA);
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
List<Job> completedJobs = context.Jobs.Where(j => j.state == JobState.Completed).ToList();
|
List<Job> completedJobs = context.Jobs.Where(j => j.state == JobState.Completed).ToList();
|
||||||
foreach (Job job in completedJobs)
|
foreach (Job job in completedJobs)
|
||||||
if(job.RecurrenceMs <= 0)
|
if (job.RecurrenceMs <= 0)
|
||||||
context.Jobs.Remove(job);
|
context.Jobs.Remove(job);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -75,16 +79,17 @@ public static class Tranga
|
|||||||
job.state = JobState.Waiting;
|
job.state = JobState.Waiting;
|
||||||
context.Jobs.Update(job);
|
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)
|
foreach (Job job in runJobs)
|
||||||
{
|
{
|
||||||
// If the job is already running, skip it
|
// If the job is already running, skip it
|
||||||
if (RunningJobs.Values.Any(j => j.JobId == job.JobId)) continue;
|
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);
|
context.Jobs.AddRange(newJobs);
|
||||||
});
|
});
|
||||||
RunningJobs.Add(t, job);
|
RunningJobs.Add(t, job);
|
||||||
@ -99,7 +104,7 @@ public static class Tranga
|
|||||||
RunningJobs.Remove(thread.thread);
|
RunningJobs.Remove(thread.thread);
|
||||||
context.Jobs.Update(thread.job);
|
context.Jobs.Update(thread.job);
|
||||||
}
|
}
|
||||||
|
|
||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
Thread.Sleep(2000);
|
Thread.Sleep(2000);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user