The process of scheduling a custom task includes:
Before you start developing scheduled tasks in your local environment, you must configure the scheduler to call the appropriate URLs when processing tasks from the system.
If you locally host the live site and administration applications without using a specific port, follow the steps described in Domain names for local development to configure either a domain alias or a Project URL. However, if the live site or administration applications in your local environment run on specific ports (e.g., when launched using IIS Express), you must set the following configuration keys for your applications:
Add the following configuration keys to the web.config file of the administration application. As the key's values, enter the full local URLs of both the administration and live site application, including the port and virtual directory path (if present):
<appSettings>
...
<!-- Sets the URL for the administration application that gets used by the scheduler -->
<add key="CMSSchedulerAdministrationUrl" value="http://localhost:3000" />
<!-- Sets the URL for the live site application that gets used by the scheduler -->
<add key="CMSSchedulerPresentationUrl" value="http://localhost:5000" />
...
</appSettings>The administration interface needs to have the live site URL available due to the ability to manually launch any scheduled task via the Scheduled tasks application.
Add the following key to the configuration file in the live site application. As the key's value, enter the full local URL of the live site application, including the port and virtual directory path (if present):
"CMSSchedulerPresentationUrl": "http://localhost:5000",<add key="CMSSchedulerPresentationUrl" value="http://localhost:5000" />The scheduler is now configured for local development.
You need to define each scheduled task as a class that implements the CMS.Scheduler.ITask interface.
Add the class as part of a custom assembly (Class Library project), and include the assembly into the appropriate solution. Depending on where you plan to execute the task, add the assembly to the Xperience administration application, the live site application, or both.
Prepare a separate project for custom classes in your Xperience solution (or use an existing one):
Create a new class under your custom project. For example, name the class CustomTask.cs.
Edit the class and add using statements for the following namespaces:
using CMS.Core;
using CMS.Scheduler;Make the class implement the ITask interface.
public class CustomTask : ITaskDefine the Execute method in the class:
public class CustomTask : ITask
{
/// <summary>
/// Executes the task.
/// </summary>
/// <param name="ti">Info object representing the scheduled task</param>
public string Execute(TaskInfo ti)
{
string details = "Custom scheduled task executed. Task data: " + ti.TaskData;
// Logs the execution of the task in the event log
Service.Resolve<IEventLogService>().LogInformation("CustomTask", "Execute", details);
// Returns a null value to indicate that the task executed successfully
// Return an error message string with details in cases where the execution fails
return null;
}
}Save all changes and Rebuild your solution.
You must always include the Execute method when writing scheduled tasks. The system calls this method whenever the given task is executed, so it needs to contain all code implementing the required functionality.
In this example, the task only creates a record in the application's event log so that you can confirm it is being executed:
The system now executes the task regularly according to the specified interval.
To check the result of this sample custom task, open the Event log application and look for entries with CustomTask as the Source.