This commit is contained in:
Pilzinsel64
2025-10-22 07:39:32 +02:00
parent ef1b6bad2e
commit 75b5f7ddd5
7 changed files with 102 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
using Castle.Core.Logging;
using Pilz.Data;
using Pilz.Extensions.Reflection;
using Pilz.Jobs;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Reflection;
@@ -55,6 +56,7 @@ public class ApiServer : IApiServer
public int MaxConcurentConnections { get; set; } = 5;
public IDataManager Manager => GetManager();
public bool ThreadedDataManager { get; set; }
public JobCenter Jobs { get; } = new();
public ApiServer(string apiUrl) : this(apiUrl, null)
{

View File

@@ -1,5 +1,6 @@
using Castle.Core.Logging;
using Pilz.Data;
using Pilz.Jobs;
namespace Pilz.Net.Api;
@@ -18,6 +19,7 @@ public interface IApiServer
IDataManager Manager { get; }
string ApiUrl { get; }
bool EnableAuth { get; set; }
public JobCenter Jobs { get; }
IApiMessageSerializer Serializer { get; }
ILogger Log { get; set; }

View File

@@ -8,7 +8,7 @@
</PropertyGroup>
<PropertyGroup>
<Version>2.7.0</Version>
<Version>2.8.0</Version>
</PropertyGroup>
<ItemGroup>

34
Pilz/Jobs/Job.cs Normal file
View File

@@ -0,0 +1,34 @@
namespace Pilz.Jobs;
public class Job
{
private readonly Action<JobContext>? handler;
public virtual string Name { get; }
public virtual DateTime LastExecution { get; internal set; }
public virtual TimeSpan Interval { get; }
public Job()
{
}
public Job(string name) : this()
{
Name = name;
}
public Job(string name, TimeSpan interval) : this(name)
{
Interval = interval;
}
public Job(string name, TimeSpan interval, Action<JobContext> handler) : this(name, interval)
{
this.handler = handler;
}
public virtual void Execute(JobContext context)
{
handler?.Invoke(context);
}
}

55
Pilz/Jobs/JobCenter.cs Normal file
View File

@@ -0,0 +1,55 @@
namespace Pilz.Jobs;
public class JobCenter
{
private readonly HashSet<Job> jobs = [];
private readonly System.Timers.Timer timerRepeat = new()
{
AutoReset = false,
Interval = 5000,
};
public bool Enabled { get; protected set; }
public JobCenter()
{
timerRepeat.Elapsed += TimerRepeat_Elapsed;
}
private void TimerRepeat_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
var now = DateTime.Now;
var jobs = this.jobs.Where(n => n.LastExecution + n.Interval > now);
foreach (var job in jobs)
{
job.LastExecution = now;
job.Execute(new(this));
}
if (Enabled)
timerRepeat.Start();
}
public virtual void AddJob(Job job)
{
jobs.Add(job);
}
public virtual void RemoveJob(Job job)
{
jobs.Remove(job);
}
public virtual void Start()
{
Enabled = true;
timerRepeat.Start();
}
public virtual void Stop()
{
Enabled = false;
timerRepeat.Stop();
}
}

7
Pilz/Jobs/JobContext.cs Normal file
View File

@@ -0,0 +1,7 @@
namespace Pilz.Jobs;
public class JobContext(JobCenter jobCenter)
{
public JobCenter JobCenter { get; } = jobCenter;
public bool HasError { get; set; }
}

View File

@@ -5,7 +5,7 @@
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>annotations</Nullable>
<Version>2.4.7</Version>
<Version>2.5.0</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">