Scheduling asynchronous tasks on your server - marcos8154/SocketAppServer GitHub Wiki
In some projects, there are certain routines that run at a specific time interval (either days, hours, or minutes). We usually call this "Scheduled Tasks".
Several times, these tasks must be performed on the server, and for this, the framework has a feature that will help you.
Basically, the framework provides a very interesting mechanism for executing autonomous and asynchronous code routines scheduled on the server. Let's see how this happens.
Create a class in your project where your routine will be executed. Let's create as an example a hypothetical routine that sends XML files anywhere
This class must extend the abstract class "ScheduledTask", located in the namespace "MobileAppServer.ScheduledServices"
public class SendXMLFiles : ScheduledTask
{
public SendXMLFiles()
: base("SendXMLFiles", true, new ScheduledTaskInterval(0, 0, 5))
{
}
public override void RunTask()
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Sending XML Files");
Console.ForegroundColor = ConsoleColor.DarkGray;
}
}
Let's better understand the anatomy of this class (which by the way is quite simple). The base constructor of the abstract class receives 3 essential parameters:
//abstract ScheduledTask constructor
public ScheduledTask(string taskName, bool runOnServerStart, ScheduledTaskInterval interval)
- string taskName: An identifiable name for the task (we'll talk about the importance later)
- bool runOnServerStart: Determines whether the tera runs every time the server starts
- ScheduledTaskInterval interval: Parameterizable interval for task execution
Determining the Intervals
The ScheduledTaskInterval class has the following parameters, through them, you must determine the days, hours and minutes of interval in which the task should be executed.
public ScheduledTaskInterval(int days, int hours, int minutes)
The RunTask() Method
The RunTask method is the method invoked by the abstract class whenever its task is executed. This is where you should implement your code and related routines (whatever they may be)
Remember that this method executes within a thread parallel to the main thread of the server and received requests, so it is not possible to access information from requests made on the server.
Understanding More About Time Ranges
Each task will have its specific time interval in which it should be performed. However, some things can happen. Imagine that a task has a 7 day interval between runs.
On day 1, the task was executed. 2 days passed. Let's say the server has been restarted or something similar. When the server returns, in theory, we would lose the missing days to the next run, and then our task would return to the original state of time. So what should be the 7th will not be anymore.
How does the framework solve this? In the same folder as your application binaries, a folder named "ScheduledTasks" will be created, and inside it will be a file named "next_events.stn". You MUST NEVER DELETE THIS FILE.
This file is basically a Json that stores the next execution dates of each task, and it is through it that the server will know the next correct time each task should be started, even if it is restarted.
Remember that you should also take care that the operating system date/time is always correct, in order to prevent the server from getting lost in the dates and its tasks collapse execution.
Enabling the scheduled task on the server
For your scheduled task to be effectively functional, you must register the instance of your task class with the server instance. In the following way:
server.AddScheduledTask(new SendXMLFiles());