update some stuff for db

This commit is contained in:
honzapatCZ 2025-01-05 23:17:07 +01:00
parent 48c603c359
commit c0d49b8e24
2 changed files with 29 additions and 0 deletions

View File

@ -1,3 +1,7 @@
using Microsoft.EntityFrameworkCore;
using NejAccountingAPI.Models;
using Quartz;
namespace NejCommon; namespace NejCommon;
public interface IScopedService public interface IScopedService
@ -15,6 +19,11 @@ public interface ITransientService
public interface IBackgroundService public interface IBackgroundService
{ {
}
public interface IRecurringService : IJob
{
public static virtual JobKey? JobKey { get; }
public static abstract string Cron { get; }
} }
public interface ISettings public interface ISettings
{ {
@ -23,6 +32,25 @@ public interface ISettings
public static class AutoScan public static class AutoScan
{ {
public static void RegisterJobs(this IServiceCollection collection, IServiceCollectionQuartzConfigurator conf)
{
var jobs = typeof(IRecurringService).Assembly.GetTypes().Where(x => x.IsAssignableTo(typeof(IRecurringService)) && x != typeof(IRecurringService)).ToList();
foreach (var job in jobs)
{
Console.WriteLine("Testing job: " + job.Name);
var jobKey = job.GetProperty("JobKey")?.GetValue(null) as JobKey ?? new JobKey(job.FullName);
var cron = job.GetProperty("Cron")!.GetValue(null) as string;
if (string.IsNullOrEmpty(cron) || jobKey == null)
{
throw new Exception("Could not register job: " + job.Name);
}
Console.WriteLine("Adding job: " + jobKey.Name + " with cron: " + cron);
conf.AddJob(job, jobKey).AddTrigger(x => x.ForJob(jobKey).WithCronSchedule(cron).StartNow());
}
}
public static void RegisterServices(this IServiceCollection collection) public static void RegisterServices(this IServiceCollection collection)
{ {
collection.Scan(scan => scan collection.Scan(scan => scan

View File

@ -157,6 +157,7 @@ namespace NejCommon.Models;
config?.Invoke(entity); config?.Invoke(entity);
this.Add(entity); this.Add(entity);
this.ChangeTracker.DetectChanges();
return entity; return entity;
} }