- 后台任务使用开源的
Coravel
- 在后端
AppWeb.cs
中配置后台任务的Job
public static class AppWeb
{
public static void AddApp(this IServiceCollection services, Action<AppInfo> action = null)
{
//...
services.AddScheduler();
services.AddTransient<YourTaskJob>(); //在此注入任务Job实例
}
public static void UseApp(this IServiceProvider provider)
{
provider.UseScheduler(scheduler =>
{
//此处配置任务的执行计划
scheduler.Schedule<YourTaskJob>().EveryFiveSeconds();
});
}
}
class YourTaskJob : IInvocable
{
public Task Invoke() => TaskHelper.RunAsync("业务类型", ExecuteAsync);
private static Task<Result> ExecuteAsync(Database db, SysTask task)
{
// 这里是执行后台任务的逻辑
return Result.SuccessAsync("执行成功!");
}
}