Switchable Sitecore schedule task

Reading time ~3 minutes

Today I will show you how you can make Sitecore schedule tasks usage easier with a tiny feature.

Some time ago I implemented a bunch of schedule tasks running inside Sitecore. The problem I had with so many tasks was to switch them off and on during specific time.

I didn’t want to manipulate schedule field and mess things up. I wanted to keep tasks configuration in place but disable them when needed at the same time.

Solution for that was a simple ‘switch button’. See how I solved my problem.

Switchable schedule task

My plan was to create a custom template based on /sitecore/templates/System/Tasks/Schedule template with additional checkbox to control if task is enabled or not.

I ran through the Sitecore configuration and found the following config file Sitecore.Processing.config:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <scheduling>
      <!-- An agent that processes scheduled tasks embedded as items in the core database. -->
      <agent type="Sitecore.Tasks.DatabaseAgent" method="Run" interval="00:10:00" name="Core_Database_Agent">
        <param desc="database">core</param>
        <param desc="schedule root">/sitecore/system/tasks/schedules</param>
        <LogActivity>true</LogActivity>
      </agent>
      <!-- An agent that processes scheduled tasks embedded as items in the master database. -->
      <agent type="Sitecore.Tasks.DatabaseAgent" method="Run" interval="00:00:01" name="Master_Database_Agent">
        <param desc="database">master</param>
        <param desc="schedule root">/sitecore/system/tasks/schedules</param>
        <LogActivity>true</LogActivity>
      </agent>
    </scheduling>
  </sitecore>
</configuration>

Pro-tip: If you are using older version of Sitecore following configuration may be located in Web.config file

My next step was to find out how DatabaseAgent works. I found following method inside

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private ScheduleItem[] GetSchedules()
{
    Item obj = Database.Items[ScheduleRoot];
    if (obj == null)
    {
        return new ScheduleItem[0];
    }
    ArrayList arrayList = new ArrayList();
    foreach (Item innerItem in obj.Axes.GetDescendants())
    {
        if (innerItem.TemplateID == TemplateIDs.Schedule)
        {
            arrayList.Add(new ScheduleItem(innerItem));
        }
    }
    return arrayList.ToArray(typeof(ScheduleItem)) as ScheduleItem[];
}

Then I started to code:

First edited GetSchedules method with extra else if check to include my custom template (line 15).

Then I created IsEnabled method to handle my checkbox value from schedle items (line 30) and used it in GetSchedules (line 17)

After my additions, whole code looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
private ScheduleItem[] GetSchedules()
{
    Item obj = Database.Items[ScheduleRoot];
    if (obj == null)
    {
        return new ScheduleItem[0];
    }
    ArrayList arrayList = new ArrayList();
    foreach (Item innerItem in obj.Axes.GetDescendants())
    {
        if (innerItem.TemplateID == TemplateIDs.Schedule)
        {
            arrayList.Add(new ScheduleItem(innerItem));
        }
        else if (TemplateManager.GetTemplate(innerItem).InheritsFrom(TemplateIDs.Schedule))
        {
            if (IsEnabled(innerItem))
            {
                arrayList.Add(new ScheduleItem(innerItem));
            }
            else
            {
                LogInfo("Not enabled: " + innerItem.Name);
            }
        }
    }
    return arrayList.ToArray(typeof(ScheduleItem)) as ScheduleItem[];
}

protected virtual bool IsEnabled(Item innerItem)
{
    var field = (CheckboxField)innerItem.Fields["Enabled"];
    return field != null && field.Checked;
}

To override default configuration I created the following config file:

1
2
3
4
5
6
7
8
9
10
11
12
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <scheduling>
      <agent type="Sitecore.Tasks.DatabaseAgent" method="Run" name="Core_Database_Agent">
        <patch:attribute name="type">SwitchableTask.CustomDatabaseAgent</patch:attribute>
      </agent>
      <agent type="Sitecore.Tasks.DatabaseAgent" method="Run" name="Master_Database_Agent">
        <patch:attribute name="type">SwitchableTask.CustomDatabaseAgent</patch:attribute>
      </agent>
    </scheduling>
  </sitecore>
</configuration>

And that is it. Now you can create switchable schedule tasks under default location and switch them on and off without touching schedule configuration.

Very easy but useful solution.

Summmary

If you want to see the whole code visit SwitchableTask module git repository.

I also created Sitecore package if you want to use it in your own instance, without building sources.

SwitchableTask-1.0.zip for Sitecore.NET 8.1 (rev. 160302)

Appendix: Schedule format

This section describes configuration format for schedule field.

General format for this field looks like this.

startDate|endDate|days|minimum interval

Please find each parameter below:

Parameters

startDate,endDate

Format: yyyyMMddTHHmmss

Example

20150423T003000

20141104T1918002

days

Format:

  • 1=Sunday,
  • 2=Monday,
  • 4=Tuesday,
  • 8=Wednesday,
  • 16=Thursday,
  • 32=Friday,
  • 64=Saturday

Example

All days: 127

Only weekend (Sunday, Saturday): 65

minimum interval

Format: HH:mm:ss

Example

1 hour 12 minutes and 3 seconds : 01:12:03

Asset Optimizer configuration

Explanation of different configuration options of SXA Asset Optimizer Continue reading

Items as resources and Unicorn

Published on November 21, 2021

Sitecore Extensions version 3.4 released

Published on November 07, 2020