Skip to content

CronServices

Furkan Güngör edited this page Feb 6, 2022 · 2 revisions

Usage

You can write your own CronJob classes using the CronJobService abstract class. We've included a sample cron job for you. When this cron job runs, it logs the related job on the console.

First, install the EasyCronJob.Core library on your application via Nuget.

You can now create your own cron jobs.

    public class ConsoleCronJob : CronJobService
    {
        private readonly ILogger<ConsoleCronJob> logger;

        public ConsoleCronJob(ICronConfiguration<ConsoleCronJob> cronConfiguration, ILogger<ConsoleCronJob> logger) 
            : base(cronConfiguration.CronExpression,cronConfiguration.TimeZoneInfo,cronConfiguration.CronFormat)
        {
            this.logger = logger;
        }

        public override Task StartAsync(CancellationToken cancellationToken)
        {
            logger.LogInformation("Start");
            return base.StartAsync(cancellationToken);
        }


        protected override Task ScheduleJob(CancellationToken cancellationToken)
        {
            logger.LogInformation("Scheduled");
            return base.ScheduleJob(cancellationToken);
        }

        public override Task DoWork(CancellationToken cancellationToken)
        {
            logger.LogInformation("Do Work");
            return base.DoWork(cancellationToken);
        }
    }
Clone this wiki locally