mirror of
https://github.com/C9Glax/tranga.git
synced 2025-06-19 09:37:53 +02: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:
@ -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;
|
||||
|
||||
string TRANGA = "\n\n _______ \n|_ _|.----..---.-..-----..-----..---.-.\n | | | _|| _ || || _ || _ |\n |___| |__| |___._||__|__||___ ||___._|\n |_____| \n\n";
|
||||
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";
|
||||
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
|
||||
{
|
||||
@ -75,16 +79,17 @@ public static class Tranga
|
||||
job.state = JobState.Waiting;
|
||||
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);
|
||||
@ -99,7 +104,7 @@ public static class Tranga
|
||||
RunningJobs.Remove(thread.thread);
|
||||
context.Jobs.Update(thread.job);
|
||||
}
|
||||
|
||||
|
||||
context.SaveChanges();
|
||||
Thread.Sleep(2000);
|
||||
}
|
||||
|
Reference in New Issue
Block a user