Cron 表达式:在 ASP.Net Core 中每天运行一次任务 [英] Cron expression: run task once per-day in ASP.Net Core
问题描述
我的asp.net核心程序需要收集所有访问网站的IP.
My asp.net core program needs to collect all the IP which access the website pre-day.
一天结束后我需要将IP列表输出到txt文件中.
I need to output the IP list to a txt file when a day is over.
因此,程序应该每天运行一次保存 IP 列表的方法.
Therefore, the program should run a method to save the IP list once per-day.
我应该如何设计它?
在我看来,我会在 startup.cs
中添加一个循环任务,并将 Task.Delay
设置为 24 小时(一天).
In my opinion, I will add a looping task in the startup.cs
and set the Task.Delay
to 24 hours(one day).
我应该这样做吗?谢谢.
Should I do it like this? Thank you.
推荐答案
这就是我在 ASP.Net Core 中实现调度程序任务的方式.请注意,您必须知道如何使用 cron epxression.
This is how I implement scheduler task in ASP.Net Core. Please note that you have to know how to use cron epxression.
首先在你的 csproj 中添加这个包
First add this package in your csproj
<PackageReference Include="ncrontab" Version="3.3.1" />
接下来我将创建SceduledProcessor.cs
Next I will create SceduledProcessor.cs
public abstract class ScheduledProcessor : ScopedProcessor
{
private readonly CrontabSchedule _schedule;
private DateTime _nextRun;
protected ScheduledProcessor(IServiceScopeFactory serviceScopeFactory) : base(serviceScopeFactory)
{
_schedule = CrontabSchedule.Parse(GetCronExpression(serviceScopeFactory));
_nextRun = _schedule.GetNextOccurrence(DateTime.Now);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
do
{
var now = DateTime.Now;
_schedule.GetNextOccurrence(now);
if (now > _nextRun)
{
await ProcessBackgroundTask();
_nextRun = _schedule.GetNextOccurrence(DateTime.Now);
}
await Task.Delay(5000, stoppingToken);
}
while (!stoppingToken.IsCancellationRequested);
}
protected abstract string GetCronExpression(IServiceScopeFactory serviceScopeFactory);
}
将为我们的场景运行 ExecuteAsync
The ExecuteAsync will be run for our scenario
请注意,我们必须实现 GetCronExpression 方法
Please note that we have to implement GetCronExpression method
接下来我将实现服务类
public class ScheduledEmailService : ScheduledProcessor
{
public ScheduledEmailService(
IServiceScopeFactory serviceScopeFactory) : base(serviceScopeFactory)
{
}
/// <summary>
/// Get cron setting from db
/// </summary>
/// <returns></returns>
protected override string GetCronExpression(IServiceScopeFactory serviceScopeFactory)
{
return ""; // return your cron expression here you can get from db or static string
}
public override Task ProcessInScope(IServiceProvider serviceProvider)
{
// do something you wish
return Task.CompletedTask;
}
}
奖励在这里我有预定义的 cron 表达式值的小班,你可以使用它
Bonus here I small class with predefined value for cron expression you can use it
public static class CronExpression
{
public static readonly string EveryMinute = "0 * * * * *";
public static readonly string EveryDay = "0 0 0 * * *";
public static readonly string EveryWeek = "0 0 * * 0";
public static readonly string EveryWeekend = "0 0 0 * * 6,0";
}
最后将其添加到您的 Startup.cs
Finally add this to your Startup.cs
services.AddSingleton<IHostedService, ScheduledEmailService>();
这篇关于Cron 表达式:在 ASP.Net Core 中每天运行一次任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!