jobs
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using Castle.Core.Logging;
|
using Castle.Core.Logging;
|
||||||
using Pilz.Data;
|
using Pilz.Data;
|
||||||
using Pilz.Extensions.Reflection;
|
using Pilz.Extensions.Reflection;
|
||||||
|
using Pilz.Jobs;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
@@ -55,6 +56,7 @@ public class ApiServer : IApiServer
|
|||||||
public int MaxConcurentConnections { get; set; } = 5;
|
public int MaxConcurentConnections { get; set; } = 5;
|
||||||
public IDataManager Manager => GetManager();
|
public IDataManager Manager => GetManager();
|
||||||
public bool ThreadedDataManager { get; set; }
|
public bool ThreadedDataManager { get; set; }
|
||||||
|
public JobCenter Jobs { get; } = new();
|
||||||
|
|
||||||
public ApiServer(string apiUrl) : this(apiUrl, null)
|
public ApiServer(string apiUrl) : this(apiUrl, null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Castle.Core.Logging;
|
using Castle.Core.Logging;
|
||||||
using Pilz.Data;
|
using Pilz.Data;
|
||||||
|
using Pilz.Jobs;
|
||||||
|
|
||||||
namespace Pilz.Net.Api;
|
namespace Pilz.Net.Api;
|
||||||
|
|
||||||
@@ -18,6 +19,7 @@ public interface IApiServer
|
|||||||
IDataManager Manager { get; }
|
IDataManager Manager { get; }
|
||||||
string ApiUrl { get; }
|
string ApiUrl { get; }
|
||||||
bool EnableAuth { get; set; }
|
bool EnableAuth { get; set; }
|
||||||
|
public JobCenter Jobs { get; }
|
||||||
|
|
||||||
IApiMessageSerializer Serializer { get; }
|
IApiMessageSerializer Serializer { get; }
|
||||||
ILogger Log { get; set; }
|
ILogger Log { get; set; }
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Version>2.7.0</Version>
|
<Version>2.8.0</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
34
Pilz/Jobs/Job.cs
Normal file
34
Pilz/Jobs/Job.cs
Normal 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
55
Pilz/Jobs/JobCenter.cs
Normal 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
7
Pilz/Jobs/JobContext.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Pilz.Jobs;
|
||||||
|
|
||||||
|
public class JobContext(JobCenter jobCenter)
|
||||||
|
{
|
||||||
|
public JobCenter JobCenter { get; } = jobCenter;
|
||||||
|
public bool HasError { get; set; }
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>annotations</Nullable>
|
<Nullable>annotations</Nullable>
|
||||||
<Version>2.4.7</Version>
|
<Version>2.5.0</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||||
|
|||||||
Reference in New Issue
Block a user