Getting Started with Job Scheduler¶
This is a brief manual to get started with the InfinniPlatform job scheduler.
1. Install InfinniPlatform.Scheduler.Quartz
package:
dotnet add package InfinniPlatform.Scheduler.Quartz -s https://www.myget.org/F/infinniplatform/
2. Call AddQuartzScheduler() in ConfigureServices()
:
using System;
using InfinniPlatform.AspNetCore;
using Microsoft.Extensions.DependencyInjection;
public class Startup
{
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddQuartzScheduler();
// ...
return services.BuildProvider();
}
// ...
}
3. Create MyJobHandler.cs and define job handler:
class MyJobHandler : IJobHandler
{
public async Task Handle(IJobInfo jobInfo, IJobHandlerContext context)
{
await Console.Out.WriteLineAsync($"Greetings from {nameof(MyJobHandler)}!");
}
}
4. Create MyJobInfoSource.cs and define job info source:
class MyJobInfoSource : IJobInfoSource
{
public Task<IEnumerable<IJobInfo>> GetJobs(IJobInfoFactory factory)
{
var jobs = new[]
{
// Job will be handled every 5 seconds
factory.CreateJobInfo<MyJobHandler>("MyJob",
b => b.CronExpression(e => e.Seconds(i => i.Each(0, 5))))
};
return Task.FromResult<IEnumerable<IJobInfo>>(jobs);
}
}
5. Register in IoC-container the job handler and the job source:
builder.RegisterType<MyJobHandler>().AsSelf().As<IJobHandler>().SingleInstance();
builder.RegisterType<MyJobInfoSource>().As<IJobInfoSource>().SingleInstance();