Login

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:

INSERT INTO tblScheduledItem

  Enabled, methodName, fStatic, typeName, assemblyName, 
  nextExec, [DatePart], [Interval], InstanceData
)
VALUES

  1,                           /* Enabled (1=yes, 0=no) */ 
  'Execute',                      /* Methodname */
  1,                           /* Static (1=yes, 0=no) */ 
  'SampleNamespace.ScheduledJobSample', /* typename of scheduled job class */
  'YetAnotherAssembly',        /* Name of assembly that contains the
                                  typename above, without the ".dll" part. */ 
  getutcdate(),                /* UTC date and time of next execution */
  'mi',                        /* Interval units, see more info below */
  '60',                        /* Job execution interval (every 60 minutes
                                  in this example) */
  null                         /* Instance data */
)

Troubleshooting

If the scheduler does not run your job when expected you should get a warning on the administrative page, try restarting the scheduler service to force it to reload the jobs. If the job is still not executed, check the event log for "EPiServer Scheduler" entries. Verify that the execution time in table tblScheduledItem in the database is correct (dates are in UTC in the database). If the execution time is wrong for some reason you can update it manually by running an UPDATE SQL command (don't forget to restart the scheduler service if you do so, to make the scheduler aware of the changes).                 

Important! The dates stored in tblScheduledItem are always stored in UTC.

EPiTrace logger