In this tutorial, you will learn about building reoccurring tasks within Umbraco V8. When building a new website, you may often bump into a requirement that will require a task to be repeatedly executed over a period of time. Common use-cases for these types of tasks might be pulling data from a third party, clearing a cache, or, cleaning up a database table before it gets too large. Within V8 reoccurring tasks can be set up and configured using a Umbraco task runner and a reoccurring task. We will start off by looking at the code required to create a reoccurring task.
To create your own reoccurring task, the first thing you need to do create a class that inherits from an abstract base class called RecurringTaskBase.
When inheriting from
RecurringTaskBase, you will need to supply three constructor parameters. The first parameter will be the task runner. The second parameter is called 'delayMilliseconds'. 'delayMilliseconds' sets the delay before the task will first execute. The last parameter 'periodMilliseconds' will define how often the task is executed. As both parameters are set in milliseconds, to set the task to run every hour you would set 'periodMilliseconds' to 3600000. The code below will create the task runner. The purpose of this runner is pointless. The task will simply write a message to the logfile whenever it is executed:
When creating your own task, it is possible to inject your own dependencies. Taking the example above, the four argument called
MyLogger, which is of type 'ILogger' is also being injected into the class. If you want to inject your own parameters, this is the pattern that you will need to follow. Remember, to pass the correct values down into
To register the task runner with Umbraco will be done using components and composers. The component will be responsible for instantiating the runner and passing in the configuration parameters.
Finally, you register the component using a composer. If components and composers are a new concept to you, I suggest reading this article.
Happy Coding 🤘