How do I create my own scheduled job?
Versions:
4.20,
FAQ number:
79,
Old FAQ number: 1049
Q: How do I create my own scheduled job ?
A: Below are the steps to create your own scheduled job:
In this example we create a new scheduled job class using the plug-in features. You will automatically get a graphical user interface in admin mode under "Scheduled jobs".
We recommend using at least EPiServer 4.21 to be able to access all features described below.
Create a new scheduled job class.
The class must implement a public static method with name "Execute", it will be called on the interval specified in administration mode. A optional return value can be specified for a descriptive text used for logging. The return type could be void if you don't want to return a descriptive message. Throwing a exception will in this case show up as "FAILED" on the history tab i admin mode.
Note! If you change the class or namespace name of a existing scheduled job it will still be active but without a settings page, you should manually delete this job from the database in table tblScheduledItem if you plan to change jobs that have been installed already.
using EPiServer.PlugIn;
.....
[ScheduledPlugIn(DisplayName="My job",Description="This job is just a sample")]
public class MyJob
{
public static string Execute()
{
if(DateTime.Now.DayOfWeek==DayOfWeek.Sunday)
throw new Exception("I'm out of office");
else
return "I was here";
}
}
Register the scheduler job programatically (optional)
The EPiServer.DataAbstraction namespace contains classes for working with data in EPiServer, in this case we are interested in the ScheduledJob class. Example usage below:
using EPiServer.DataAbstraction;
.........
ScheduledJob job = new ScheduledJob();
job.Name = "My test job";
job.MethodName = "Execute";
job.TypeName = "EPiServerSample.MyJob";
job.AssemblyName = "EPiServerSample";
job.IsStaticMethod = true;
job.NextExecution = DateTime.Today;
job.IntervalType = ScheduledIntervalType.Hours;
job.IntervalLength = 1;
job.Save();
Register the scheduled job manually in the database (optional)
The preferable way to register a scheduled job is to use the classes and methods described above. But if you need to register a job manually in the database you can do that by executing the following SQL statement: