using FreeScheduler;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;
using ZhonTai.Admin.Core.Configs;
namespace ZhonTai.Admin.Tools.TaskScheduler;
public static class TaskSchedulerServiceExtensions
{
public static IServiceProvider ServiceProvider { get; private set; }
///
/// 添加任务调度
///
///
///
public static void AddTaskScheduler(this IServiceCollection services, Action configureOptions = null)
{
ServiceProvider = services.BuildServiceProvider();
var options = new TaskSchedulerOptions()
{
FreeSql = ServiceProvider.GetService()
};
configureOptions?.Invoke(options);
var freeSql = options.FreeSql;
freeSql.CodeFirst
.ConfigEntity(a =>
{
a.Name("ad_task");
a.Property(b => b.Id).IsPrimary(true);
a.Property(b => b.Body).StringLength(-1);
a.Property(b => b.Interval).MapType(typeof(int));
a.Property(b => b.IntervalArgument).StringLength(1024);
a.Property(b => b.Status).MapType(typeof(int));
a.Property(b => b.CreateTime).ServerTime(DateTimeKind.Local);
a.Property(b => b.LastRunTime).ServerTime(DateTimeKind.Local);
})
.ConfigEntity(a =>
{
a.Name("ad_task_log");
a.Property(b => b.Exception).StringLength(-1);
a.Property(b => b.Remark).StringLength(-1);
a.Property(b => b.CreateTime).ServerTime(DateTimeKind.Local);
});
options.ConfigureFreeSql?.Invoke(freeSql);
var dbConfig = ServiceProvider.GetService();
if (dbConfig.SyncStructure)
{
freeSql.CodeFirst.SyncStructure();
freeSql.CodeFirst.SyncStructure();
}
if(options.TaskHandler != null)
{
//开启任务
var scheduler = new Scheduler(options.TaskHandler);
services.AddSingleton(scheduler);
}
}
///
/// 使用任务调度
///
///
public static void UseTaskScheduler(this IApplicationBuilder app)
{
ServiceProvider = app.ApplicationServices;
}
}