In-Memory Background Job
Initial Setup
To set up a background job that runs periodically, follow these steps:
- Create a Public Instance Class
- Inherit from
TickerController
- Annotate with
TickerFunction
Attribute- This attribute requires a parameters: FunctionName.
Example
Here's a simple example of a background job that will be executed periodically
or specified time
based on the data that are stored in the database using below steps.
csharp
public class MyFirstExample : TickerController
{
[TickerFunction(FunctionName: "ExampleTicker")]
public async Task ExampleTicker(TickerFunctionContext<string> tickerContext, CancellationToken cancellationToken)
{
Console.WriteLine(await tickerContext.Request.Value); // output Hello
}
}
Storing jobs in database
Store a CronTicker (periodic job)
.
Steps:
- Retrive the
ICronTickerManager<CronTicker>
from DependencyInjection.
csharpprivate readonly ICronTickerManager<CronTicker> _cronTickerManager;
- Store a
CronTicker
to the database:
csharpawait _cronTickerManager.AddAsync(new CronTicker { Request = TickerHelper.CreateTickerRequest<string>("Hello"), Expression = "* * * * *", Function = "ExampleTicker" })
Store a TimeTicker (time based job)
.
INFO
- Retrive the
ITimeTickerManager<TimeTicker>
from DependencyInjection.
csharpprivate readonly ITimeTickerManager<TimeTicker> _timeTickerManager;
- Store a
TimeTicker
to the database:
csharpawait _timeTickerManager.AddAsync(new TimeTicker { Request = TickerHelper.CreateTickerRequest<string>("Hello"), ExecutionTime = DateTime.Now.AddMinutes(10), Function = "ExampleTicker" })