In Grandnode
you can create a task that will be invoked in some time interval. You can manage
it from the admin panel.
Firstly create a class that implements a simple interface ISheduleTask, which has only one method Execute.
public partial class EndpointProvider : IEndpointProvider
namespace Misc.MyPlugin.Tasks
{
public class SimpleTask : IScheduleTask
{
private readonly ILogger _logger;
public SimpleTask(ILogger logger)
{
_logger = logger;
}
public async Task Execute()
{
await _logger.InsertLog(LogLevel.Information, "Invoke my simple task");
}
}
}
Next, you
must insert the SheduleTask object. The best place for it is the Installation() method from
BasePlugin (more information in “Base plugin structure” article ) :
var myTask = new ScheduleTask() {
ScheduleTaskName = "My simple task",
Type = "Misc.MyPlugin.Tasks.SimpleTask, Misc.MyPlugin",
Enabled = false,
StopOnError = true,
TimeInterval = 10
};
await _scheduleTaskService.InsertTask(myTask);
ScheduleTaskName - the name of your task that will be available in the tasks list we create a ScheduleTask object that has some significant properties:
Type –
this is very important because the GrandNode background task uses this field for
fetch implementation of ISheduleTask. So you must use template: “namespace.ClassName, MainNamespace” in our
case "Misc.MyPlugin.Tasks.SimpleTask, Misc.MyPlugin".
Enabled – indicate if the task is enabled for invoking, usually in installation we set it as false
StopOnError – assign true if you want to stop the task if an error occurs
TimeInterval
– interval in minutes
Some of
those properties you can edit in GrandNode admin panel (Admin panel -> System -> Schedule Tasks)
Finally
register IScheduleTask in the container :
services.AddScoped<IScheduleTask,
SimpleTask >();