sitecorescheduled-taskssitecore8.1

Sitecore Task Scheduling With Time Period Exception


I have a number of scheduled tasks set up in Sitecore. Some run every 15 mins others every few hours.

An example config:

  <sitecore>
    <scheduling>
      <agent type="Acme.Tasks.SitemapProducts, AcmeSitecore" method="Run" interval="00:15:00" site="website">
        <param desc="filename">~/products.xml</param>
        <param desc="Site">website</param>
        <param desc="HasVariants">false</param>
      </agent>
    </scheduling>
  </sitecore>

This runs a task that builds an xml file every 15 mins and is working fine. However I now want to stop this task from running between certain times on certain days. Is there any way for me to add this via config?


Solution

  • No. There is no way of doing this via config only.

    What you can do is to pass extra parameters to your scheduled task and set start and end time. And then in your scheduled task code just check if current time is between start and end time.

    The job will be executed but it will not generate new xml.

    <sitecore>
        <scheduling>
          <agent type="Acme.Tasks.SitemapProducts, AcmeSitecore" method="Run" interval="00:15:00" site="website">
            <param desc="filename">~/products.xml</param>
            <param desc="Site">website</param>
            <param desc="HasVariants">false</param>
            <StartTime>08:00</StartTime>
            <Endtime>21:00</Endtime>
          </agent>
        </scheduling>
    </sitecore>
    

    Code should be something like this (not tested):

    public class SitemapProducts
    {
        public string StartTime { get; set; }
        public string EndTime { get; set; }
    
        public void Run()
        {
          if (!string.IsNullOrEmpty(StartTime) && DateTime.Now.ToString("HH:mm") < StartTime)
          {
              return;
          }
          if (!string.IsNullOrEmpty(EndTime) && DateTime.Now.ToString("HH:mm") > EndTime)
          {
              return;
          }
        }