Quartz Makes Scheduling Jobs Easy With EPiServer Commerce

Quartz rockA common requirement for e-commerce applications is the ability to schedule jobs or tasks. I am currently working on a project which uses EPiServer Commerce and I needed to schedule an order export job.

I was pleasantly surprised to find that EPiServer Commerce is supplied bundled with the Quartz.NET enterprise code library to facilitate these job scheduling requirements.

Gentlemen, choose your weapons

I had three main options available for implementing the export job, a separate windows service, an EPiServer CMS scheduled job or a Quartz job. I decided that the Quartz service was better than the option of a separate windows service because the framework was already there and available so would not increase the complexity of deployments. I also felt it was a better option than a EPiServer CMS scheduled job because Quartz runs independently to the website app pool so should not put the web application under greater load while it is running and potentially degrade the performance of the end user website.

A 1000 foot example

I’m not going to go into a massively detailed explanation of how to implement the jobs as there is a already a good example available in the EPiServer Commerce dev guide along with a detailed tutorial on the Quartz .NET site. However a quick overview demonstrates how intuitive it is to implement.

The first thing you need to do is create your job class which will run when Quartz triggers the execution. The class must inherit from the IJob interface which just has a single Execute method to implement. Here is a sample skeleton class:

    public class CommerceOrderExportJob : IStatefulJob
    {
        private readonly ILog _log;

        public CommerceOrderExportJob()
        {
            _log = LogManager.GetLogger(base.GetType());
            //Any other init code
        }

        public void Execute(JobExecutionContext context)
        {
            //Your export methods
        }
    }

You then need to extend the config file to configure Quartz to call your job. I think all the parameters are pretty self explanatory:

<job>
    <job-detail>
      <name>OrderExportJob</name>
      <group>all</group>
      <description>This job exports orders from commerce</description>
      <job-type>Your.Assembly.CommerceOrderExportJob, Your.Assembly</job-type>
      <volatile>false</volatile>
      <durable>true</durable>
      <recover>false</recover>
    </job-detail>
    <trigger>
      <simple>
        <name>OrderExportJobTrigger</name>
        <group>eCommerceFramework</group>
        <description>Fires Order Export Job</description>
        <misfire-instruction>SmartPolicy</misfire-instruction>
        <volatile>false</volatile>
        <job-name>OrderExportJob</job-name>
        <job-group>eCommerceFramework</job-group>
        <repeat-count>RepeatIndefinitely</repeat-count>
        <repeat-interval>300000</repeat-interval>
      </simple>
    </trigger>
  </job>

Hit run on the service and check the event log for any problems. Make sure to include some useful event/error logging in the job otherwise it will just be a mysterious black box! There are a couple of snags you may run into when running the job against EPiServer Commerce:

  • ensure that your compiled assembly is actually available in the Quartz service directory, this is different from the main commerce site bin folder so it is not enough just to include it in the commerce app references
  • Make sure the data context has been initialised and the config file in the Quartz service directory is pointing to your database

Mining for more gems

Ultimately only time will tell if Quartz was the best choice but I feel it has the best chance of high reliability and performance in the long term. I hope that, where appropriate, EPiServer can continue to expand the incorporation of proven open source libraries with their product offerings.

Comments

Copyright © 2016 - Hook Technologies Ltd - Powered by Octopress