Luftborn.SimpleCronScheduler 1.0.0

This is a simple package for scheduling tasks in ASP.Net Core WebAPI, MVC projects

Install-Package Luftborn.SimpleCronScheduler -Version 1.0.0
dotnet add package Luftborn.SimpleCronScheduler --version 1.0.0
<PackageReference Include="Luftborn.SimpleCronScheduler" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Luftborn.SimpleCronScheduler --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

SimpleCronScheduler

Disclaimer this package is inspired by This Article

Overview

This is a simple package for scheduling tasks in ASP.Net Core WebAPI / MVC projects

Usage

To use this package you will have to:

  • 1. Create Cron Job: which is a class that implements "CronJobService" abstract class
  • 2. Add This Cron Job as a service (in project startup)
1. Create Cron Job
  • First add below using statements
    using Luftborn.ASPCore.SimpleCronScheduler;
    using Luftborn.ASPCore.SimpleCronScheduler.ScheduleConfig;

  • Below is a sample implementation:

public  class  MyCronJob1 : CronJobService

{
	public  MyCronJob1(IScheduleConfig<MyCronJob1> config) : base(config.CronExpression, config.TimeZoneInfo)
	{ }

	public  override  Task  StartAsync(CancellationToken  cancellationToken)
	{
		Log.Information("CronJob 1 starts.");
		return  base.StartAsync(cancellationToken);
	}

	public  override  Task  DoWork(CancellationToken  cancellationToken)
	{
		Log.Information($"{DateTime.Now:hh:mm:ss} CronJob 1 is working.");
		// this is where the logic is put
		return  Task.CompletedTask;
	}

	public  override  Task  StopAsync(CancellationToken  cancellationToken)
	{
		Log.Information("CronJob 1 is stopping.");
		return  base.StopAsync(cancellationToken);
	}
}

The key point here is to put the business logic into the DoWork method.
P.S. Serilog is used for logging in this example, but any other logger would do.

1. Add Cron Job as a service

In the project "startup.cs"

  • add below using statement
    using Luftborn.ASPCore.SimpleCronScheduler.Extensions;

  • In the "ConfigureServices" method add logic like the below
    where "CronExpression" here represents that the service would run each 5 minutes.

services.AddCronJob<MyCronJob1>(c =>
{
	c.TimeZoneInfo = TimeZoneInfo.Local;
	c.CronExpression = @"*/5 * * * *";
});

This uses Cronos as a cron expression parser, so please check their page, if you wou'd like to know more about CronExpression format or TimeZoneInfo

SimpleCronScheduler

Disclaimer this package is inspired by This Article

Overview

This is a simple package for scheduling tasks in ASP.Net Core WebAPI / MVC projects

Usage

To use this package you will have to:

  • 1. Create Cron Job: which is a class that implements "CronJobService" abstract class
  • 2. Add This Cron Job as a service (in project startup)
1. Create Cron Job
  • First add below using statements
    using Luftborn.ASPCore.SimpleCronScheduler;
    using Luftborn.ASPCore.SimpleCronScheduler.ScheduleConfig;

  • Below is a sample implementation:

public  class  MyCronJob1 : CronJobService

{
	public  MyCronJob1(IScheduleConfig<MyCronJob1> config) : base(config.CronExpression, config.TimeZoneInfo)
	{ }

	public  override  Task  StartAsync(CancellationToken  cancellationToken)
	{
		Log.Information("CronJob 1 starts.");
		return  base.StartAsync(cancellationToken);
	}

	public  override  Task  DoWork(CancellationToken  cancellationToken)
	{
		Log.Information($"{DateTime.Now:hh:mm:ss} CronJob 1 is working.");
		// this is where the logic is put
		return  Task.CompletedTask;
	}

	public  override  Task  StopAsync(CancellationToken  cancellationToken)
	{
		Log.Information("CronJob 1 is stopping.");
		return  base.StopAsync(cancellationToken);
	}
}

The key point here is to put the business logic into the DoWork method.
P.S. Serilog is used for logging in this example, but any other logger would do.

1. Add Cron Job as a service

In the project "startup.cs"

  • add below using statement
    using Luftborn.ASPCore.SimpleCronScheduler.Extensions;

  • In the "ConfigureServices" method add logic like the below
    where "CronExpression" here represents that the service would run each 5 minutes.

services.AddCronJob<MyCronJob1>(c =>
{
	c.TimeZoneInfo = TimeZoneInfo.Local;
	c.CronExpression = @"*/5 * * * *";
});

This uses Cronos as a cron expression parser, so please check their page, if you wou'd like to know more about CronExpression format or TimeZoneInfo

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0 138 5/12/2020