There are some partner developers followed this article http://world.episerver.com/documentation/developer-guides/CMS/scheduled-jobs/ to stop a job in admin UI. According to this post, a basic scheduled with the possibility to stop a job by overriding Stop method, so this method is called when a user clicks on Stop button.
using System;
using EPiServer.Core;
using EPiServer.PlugIn;
using EPiServer.Scheduler;
namespace MyEpiserverSite.Jobs
{
[ScheduledPlugIn(DisplayName = "ScheduledJobExample", GUID = "d6619008-3e76-4886-b3c7-9a025a0c2603")]
public class ScheduledJobExample : ScheduledJobBase
{
private bool _stopSignaled;
public ScheduledJobExample()
{
IsStoppable = true;
}
/// <summary>
/// Called when a user clicks on Stop for a manually started job, or when ASP.NET shuts down.
/// </summary>
public override void Stop()
{
_stopSignaled = true;
}
/// <summary>
/// Called when a scheduled job executes
/// </summary>
/// <returns>A status message to be stored in the database log and visible from admin mode</returns>
public override string Execute()
{
//Call OnStatusChanged to periodically notify progress of job for manually started jobs
OnStatusChanged(String.Format("Starting execution of {0}", this.GetType()));
//Add implementation
//For long running jobs periodically check if stop is signaled and if so stop execution
if (_stopSignaled)
{
return "Stop of job was called";
}
return "Change to message that describes outcome of execution";
}
}
}
Such a scheduled job is required to have a static method named Execute and inside this method we need
to check if stop is signaled and if so stop execution.
They created a custom job and overrode the Stop method but it does not stop the job. Knowing that we can set the columns [Enabled] and [IsRunning] to '0' in the database table [tblScheduledItem] to stop a job but they would like the stop button works in the Admin UI.
The stop signal must be checked periodically. Some developers made mistake by checking it only inside the Execute() method, which is not correct. If your schedule job has a loop, try to check the stop signal inside the loop, breakout of the loop and return once the stop signal is true.
For example:
public override string Execute()
{
string resultMessage = string.Empty;
int current = 0;
try
{
for(int i = 0; i< 100000; i++)
{
current++;
//For long running jobs periodically check if stop is signaled and if so stop execution
if(_stopSignaled)
{
System.Threading.Thread.Sleep(60000);
return String.Format("Stop of job was called after executed {0} items",current);
}
//Call OnStatusChanged to periodically notify progress of job for manually started jobs
OnStatusChanged(String.Format("Starting execution of {0}", current));
}
resultMessage = string.Format("Job is completed, {0} has been executed",current);
}
catch(Exception ex)
{
resultMessage = "The job could not be completed";
}
return resultMessage;
}
Please sign in to leave a comment.