Skip to content

Instantly share code, notes, and snippets.

@hishaamn
Created April 25, 2018 06:51
Show Gist options
  • Save hishaamn/3f524db60e91f007dbca59a008582c4c to your computer and use it in GitHub Desktop.
Save hishaamn/3f524db60e91f007dbca59a008582c4c to your computer and use it in GitHub Desktop.
Override Database Agent class to add display name
namespace Sitecore.Tools.Extensions
{
using System;
using System.Collections;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Jobs;
using Sitecore.Tasks;
public class DatabaseAgent : Tasks.DatabaseAgent
{
public DatabaseAgent(string databaseName, string scheduleRoot) : base(databaseName, scheduleRoot)
{
}
public new void Run()
{
this.LogInfo("Scheduling.DatabaseAgent started. Database: " + this.Database.Name);
Job job = Context.Job;
ScheduleItem[] schedules = this.GetSchedules();
this.LogInfo($"Examining schedules (count: {schedules.Length})");
if (this.IsValidJob(job))
{
job.Status.Total = schedules.Length;
}
foreach (ScheduleItem scheduleItem in schedules)
{
try
{
if (scheduleItem.IsDue)
{
this.LogInfo($"Starting: {scheduleItem.Name} {(scheduleItem.Asynchronous ? "(asynchronously)" : string.Empty)}");
if (!scheduleItem.Asynchronous)
{
job.Options.JobDisplayName = scheduleItem.DisplayName;
}
scheduleItem.Execute();
this.LogInfo("Ended: " + scheduleItem.Name);
}
else
{
this.LogInfo("Not due: " + scheduleItem.Name);
}
if (scheduleItem.AutoRemove)
{
if (scheduleItem.Expired)
{
this.LogInfo("Schedule is expired. Auto removing schedule item: " + scheduleItem.Name);
scheduleItem.Remove();
}
}
}
catch (Exception ex)
{
Log.Error($"An error occurred. Message: {ex.Message}. StackTrace: {ex.StackTrace}", this);
}
if (this.IsValidJob(job))
{
++job.Status.Processed;
}
}
}
private bool IsValidJob(Job job)
{
if (job != null)
{
return job.Category == "schedule";
}
return false;
}
private ScheduleItem[] GetSchedules()
{
Item obj = this.Database.Items[this.ScheduleRoot];
if (obj == null)
{
return new ScheduleItem[0];
}
ArrayList arrayList = new ArrayList();
foreach (Item descendant in obj.Axes.GetDescendants())
{
if (descendant.TemplateID == TemplateIDs.Schedule)
{
arrayList.Add(new ScheduleItem(descendant));
}
}
return arrayList.ToArray(typeof(ScheduleItem)) as ScheduleItem[];
}
private void LogInfo(string message)
{
if (!this.LogActivity)
{
return;
}
Log.Info(message, this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment