Skip to content

Latest commit

 

History

History
70 lines (50 loc) · 2.27 KB

File metadata and controls

70 lines (50 loc) · 2.27 KB

⚠️ DeprecatedSolTechnology.Core.Scheduler is deprecated in favour of SolTechnology.Core.Hangfire recurring jobs. See background-processing architecture.

Overview

The SolTechnology.Core.Scheduler library provides minimum functionality needed for scheduled background tasks. It handles needed services registration and configuration. It is based on Hangfire.Cronos library. The service is using in-memory scheduler, so does not share the scheduling information between instances.

Registration

For installing the library, reference SolTechnology.Core.Scheduler nuget package and invoke AddScheduledJob() service collection extension method:

services.AddScheduledJob<SynchornizeCristianoRonaldoMatches>();

Configuration

  1. The first option is to create an appsettings.json section:
  "Configuration": {
    "ScheduledJobs": [
        {
        "JobName": "SynchornizeCristianoRonaldoMatches",
        "CronExpression": "0 0 * * *"
        }
    ]
  }

In this case, JobName has to match registered service (T) name.

  1. Alternatevely the same settings can be provided by optional parameter during registration:
builder.Services.AddScheduledJob<SynchornizeCristianoRonaldoMatches>(new ScheduledJobConfiguration("0 0 * * *")); //every day at midnight

Usage

  1. Derivate from ScheduledJob base class
public class SynchornizeCristianoRonaldoMatches : ScheduledJob
  1. Implement constructor and Execute() method
        public SynchornizeCristianoRonaldoMatches(
            ISchedulerConfigurationProvider schedulerConfigurationProvider,
            IServiceScopeFactory serviceScopeFactory,
            ILogger<ScheduledJob> logger)
            : base(schedulerConfigurationProvider, serviceScopeFactory, logger)
        {
            var scope = serviceScopeFactory.CreateScope();
            var handler = scope.ServiceProvider.GetRequiredService<ICommandHandler<SynchronizePlayerMatchesCommand>>();
            _handler = handler;
        }

        public override async Task Execute()
        {
            await _handler.Handle(new SynchronizePlayerMatchesCommand(44));
        }
  1. The job will run based on provided Cron expression.