ITask
The ITask interface allows you to implement a background task which can then be registered in the Task Daemon system in Veva. Instead of implementing the ITask interface directly, we recommend that you inherit from the BaseTask class which is a part of the Task Daemon module, and then you simply need to override the Run method with your own implementation. The BaseTask class gives you access to the Log() method to log information to the Task log.
Interface declaration
public interface ITask { Guid TaskId { get; set; } DateTime DateTimeOfRun { get; set; } Task Run(ILogger logger); }
Example
This example shows a basic task which outputs some data to the console.
[Description("This is a test task which ships with the Hangfire task runner module")] [DisplayName("Hangfire test task")] public class TestTask : BaseTask { [EditorBrowsable] [Description("Text to output to console")] public string Output { get; set; } public TestTask(IPropertyValueMapperService propertyValueMapperService, ITaskLogEntryRepository taskLogEntryRepository) : base(propertyValueMapperService, taskLogEntryRepository) { } public async override Task Run(ILogger logger) { await base.Run(logger); Random rnd = new Random(); logger.LogInformation("This is a test task with output set to: {0}!", Output); int nrOfLines = rnd.Next(0, 30); for (var i = 0; i < nrOfLines; i++) { logger.LogInformation("Log line nr {0} of {1}!", i, nrOfLines); } } }
Registration
This is how the implementation is registered into the DI container in Veva, this should be done in your module config file (The class which implements IModule).
public static void AddCoreModelServices(this IServiceCollection services) { services.AddNamed<ITask, TestTask>(); }
Usage
Open the Tasks module in the Veva backend, and click "Add". Your task should be visible in the list of registered tasks. Select it and then give it a name/description and define when you want it to be executed, whether it should run once or be run on a schedule (recurring).