【问题标题】:Cron expression: run task once per-day in ASP.Net CoreCron 表达式:在 ASP.Net Core 中每天运行一次任务
【发布时间】:2020-02-20 11:24:40
【问题描述】:

我的asp.net核心程序需要收集前一天访问网站的所有IP。

一天结束后,我需要将 IP 列表输出到 txt 文件。

因此,程序应该每天运行一次保存 IP 列表的方法。

我应该如何设计它?

在我看来,我会在startup.cs 中添加一个循环任务,并将Task.Delay 设置为24 小时(一天)。

我应该这样做吗?谢谢。

【问题讨论】:

  • 你可以使用 IHostedService 来做一些后台任务。更多详情here
  • 制作一个控制台应用程序,在发布模式下构建它以获取 exe,使用 windows 调度程序每 24 小时调度一次
  • 你绝对不应该将它包含在你的网站(startup.cs)中。根据您的环境(Azure/Linux/Windows ...)添加一个 cron 作业或利用其他一些机制。 Web 服务往往只为请求运行,并且可以被主机从内存中删除。
  • @Thangadurai 谢谢。让我试试看。
  • @KunalMukherjee 但是,如何从 asp.net 核心项目中获取数据?

标签: c# asp.net-core .net-core cron asp.net-core-mvc


【解决方案1】:

这就是我在 ASP.Net Core 中实现调度程序任务的方式。请注意,您必须知道如何使用 cron epxression。

首先在你的csproj中添加这个包

<PackageReference Include="ncrontab" Version="3.3.1" />

接下来我将创建 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

请注意,我们必须实现 GetCronExpression 方法

接下来我将实现服务类

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 表达式预定义值,你可以使用它

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

services.AddSingleton<IHostedService, ScheduledEmailService>();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 2017-10-18
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    相关资源
    最近更新 更多