Cron expression: run task once per-day in ASP.Net Core

后端 未结 1 465
自闭症患者
自闭症患者 2021-01-24 03:45

My asp.net core program needs to collect all the IP which access the website pre-day.

I need to output the IP list to a txt file when a day is over.

Therefore, t

1条回答
  •  礼貌的吻别
    2021-01-24 04:10

    This is how I implement scheduler task in ASP.Net Core. Please note that you have to know how to use cron epxression.

    First add this package in your csproj

    
    

    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);
        }
    

    The ExecuteAsync will be run for our scenario

    Please note that we have to implement GetCronExpression method

    Next I will implement the service class

    public class ScheduledEmailService : ScheduledProcessor
        {
            public ScheduledEmailService(
                IServiceScopeFactory serviceScopeFactory) : base(serviceScopeFactory)
            {
            }
    
            /// 
            /// Get cron setting from db
            /// 
            /// 
            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;
            }
        }
    

    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";
        }
    

    Finally add this to your Startup.cs

    services.AddSingleton();
    

    0 讨论(0)
提交回复
热议问题