E-commerce integration frequently requires the use of scheduled tasks to update or export data on a periodic basis. Some examples of this include:
- Product data import
- Product stock updates
- Order data export
- Order data update
Scheduled Tasks in Sitecore provide a means of running these sort of periodic jobs. Active Commerce provides some enhancements to the scheduled task system to make jobs easier to configure, and easier to debug.
Creating an Enhanced Schedule Item
To utilize the Active Commerce Extended Schedule, first create a Command item under /sitecore/system/Tasks/Commands as you would for any Sitecore scheduled task. Configure it with the class/assembly and method you wish to execute.
Then under /sitecore/system/Tasks/Schedules, utilize the Extended Schedule insert option to create an Extended Schedule item.
Configuring the Extended Schedule
Configure the Command, Schedule, and other fields as you would for any Sitecore scheduled task. Typically you will want to enable the Async option. The Extended Schedule has three additional fields which can help in developing your scheduled task code:
- Site Context - The Active Commerce Shop Context is highly dependent on an active site context in order to resolve a product repository and other commerce configuration. As scheduled tasks always run in the scheduler site context, you'll need the name of the site context under which you wish to execute your scheduled task. This is the name of the Active Commerce site as you've defined it in the Sitecore <sites /> configuration.
- Database Context - Similar to site context, you'll need the name of a Sitecore database in which your scheduled task will execute. This is almost always the master database.
- Arguments - This name/value field allows you to define additional arguments that will be available in your scheduled task. These could be item paths, file paths, server URLs, API credentials, etc.
To use these values in your scheduled task command class, wrap the ScheduleItem argument with a ActiveCommerce.SitecoreX.ScheduledTasks.ExtendedScheduleItem and access the SiteContext, Database, and Arguments properties. The ShopContext and Database properties in particular can be utilized with an ActiveCommerce.ShopContext.ShopContextSwitcher in a using statement to wrap your import or export logic.
The Product Import Example on the Active Commerce examples GitHub is a great reference.
Testing the Extended Schedule
Extended Schedule items come with an additional ribbon button which makes it easier to run and test them on an as-needed basis, rather than waiting for the schedule to execute. When an Extended Schedule item is selected, an additional Scheduled Task ribbon will appear in the Content Editor, containing a Run Now button which will execute the task immediately.
If you wish to control security on this ribbon in order to prevent content authors from utilizing it, it can be found in the core database at /sitecore/content/Applications/Content Editor/Ribbons/Contextual Ribbons/Scheduled Task.
Enabling Extended Schedule Items with the Sitecore Scheduler
By default, the Sitecore Scheduling Agent will only execute items which directly inherit from its own Schedule template. Active Commerce includes an enhanced agent which will execute any items which inherit from that template. To enable this agent, remove the .example extension from the xActiveCommerce.Scheduling.config.example configuration patch found in your website's App_Config\Include folder.
For More Information
For more information on Sitecore Scheduled Tasks, review the following articles: