Processing scheduled tasks when developing MVC applications
When using a separate MVC application together with a Kentico application, you need to ensure that scheduled tasks on the Kentico application get processed in regular intervals. As the Kentico application itself does not usually receive traffic from site visitors, you either need to keep the application alive in a different way or process the tasks independently on the Kentico application’s worker process.
You can use one of the following approaches for processing scheduled tasks when developing MVC applications:
- Automatic scheduler mode – recommended approach. Requires the Kentico application’s worker process to remain alive.
- Scheduler Windows service – independent of the Kentico application’s worker process.
Automatic scheduler mode
Configure the Kentico application to use Automatic scheduler mode.
Set the Idle Time-out (minutes) setting of your IIS application pool to 0. Otherwise, the application pool worker process shuts down after a period of inactivity. As the Kentico application does not receive traffic from visitors, the worker process would shut down if no one used the Kentico administration for a certain period of time and scheduled tasks would not get executed.
- If you do not want to set the Idle Time-out (minutes) setting to 0, you can keep the worker process alive by sending requests to the Kentico application within the set interval.
Scheduler Windows service
You can use the Scheduler Windows service if you want to process scheduled tasks independently on the Kentico application
Note:
- The Windows scheduler service requires the Kentico EMS license.
- Tasks that are processed by the Windows service cannot resolve macros dependent on application context, such as: {% ApplicationPath %}.
Example - Regularly processing search indexing tasks on MVC sites
To use the scheduler to regularly process search indexing tasks, you need to duplicate the default Execute search tasks scheduled task, which is not runnable by an external scheduler:
As a prerequisite, you need to:
- Install the Scheduler Windows service.
- Open the Settings application in Kentico.
- Expand the System category and enable the Use external service setting.
- Click Save.
Open the Scheduled tasks application.
Click New task.
Set the following properties of the new task:
- Assembly name: CMS.Scheduler
- Class: CMS.Scheduler.SearchTaskExecutor
- Period: set the desired time interval (the original task uses a 4 hour interval by default)
- Use external service: enabled
Click Save.
Edit the original Execute search tasks scheduled task.
Disable the Task enabled option.
Click Save.
The system now processes search tasks generated by both the MVC and Kentico application using the Windows scheduler service.