When building a custom timer job for Sharepoint 2010, special attention should be put on where do we need this job to run. When we have a farm environment, we can choose to run the job on all servers, in one particular server, only the front end servers, etc. Depending on our requirements the timer job implementation and installation approach will change, so we should decide where we want it to run at the first place.
All Sharepoint timer jobs ultimately inherit from the SPJobDefinition class. This class provides 3 constructors:
|SPJobDefinition() ||Default constructor needed for serialization purposes. |
|SPJobDefinition(String, SPService, SPServer, SPJobLockType) ||Instantiates a timer job associated with the given SPService. |
|SPJobDefinition(String, SPWebApplication, SPServer, SPJobLockType) ||Instantiates a timer job associated with the given SPWebApplication. |
The first constructor is required for serialization and is for internal use only. One of the other two constructors will be invoked from our custom timer job constructor. The parameters passed to it will define where the timer job will run.
Here is a sample code of a custom timer job definition:
class MyTimerJob1 : SPJobDefinition
public MyTimerJob1(string name, SPService service, SPServer server,
SPJobLockType lockType) : base(name, service, server, lockType)
public MyTimerJob1(string name, SPWebApplication webApplication, SPServer server,
SPJobLockType lockType) : base(name, webApplication, server, lockType)
public override void Execute(Guid targetInstanceId)
//Execute Timer Job Tasks
Besides the required default constructor, we need to provide at least one of the other 2 constructors. Depending on which constructor we use, the timer job definition can be associated either with a service or a web application. It can also be associated with a particular server in the farm and a lock type. So, the first thing is that for a particular server to be eligible to run the job, it must be provisioned with the service or web app associated with the job. Then, if a particular server is passed to the constructor, the job will run only on that server (if it has the associated service or web app, otherwise the job won’t run at all). If no server is associated, then it will run on one or many servers depending on the lock type.
The SPJobLockType enumeration can take one of the following values:
- None: Provides no locks. The timer job runs on every machine in the farm on which the parent service is provisioned, unless the job I associated with a specified server in which case it runs on only that server (and only if the parent service is provisioned on the server).
- ContentDatabase: Locks the content database. A timer job runs one time for each content database associated with the Web application.
- Job: Locks the timer job so that it runs only on one machine in the farm.
So, if we instantiate a timer job passing null as the associated server and None as the lock type, we will expect it to run on every machine in the farm on which the parent service is provisioned. If we passed an SPService to the constructor, we now which service we are talking about, and now on which servers it is provisioned. But, if we passed an SPWebApplication to the constructor, in which servers will the job run? The answer is on every web font-end server, that is the servers where the Web Application service is running on.
Remember that the different server roles that we can found on a Sharepoint farm are:
- Database Server: the server that hosts the Microsoft SQL Server database for the farm. Since Sharepoint Foundation is not typically installed in this server, no jobs will run here.
- Web Front End Server: server where the Microsoft SharePoint Foundation Web Application service is running on.
- Application Server: Any other Sharepoint server.
Here are a couple of examples on where the jobs will run depending on the parameters passed to the constructor:
//Job associated with a web app, no server in particular and none lock:
// will run on all fron end servers.
var jobRunningOnAllFrontEndServers = new MyTimerJob1("mytimerjob",
SPWebApplication.Lookup(webAppURI), null, SPJobLockType.None);
//Job associated with a web app, one front end server and job lock:
// will run only in the frontEndServer1 server.
var jobRunningOnAParticularFronEndServer = new MyTimerJob1("mytimerjob",
SPWebApplication.Lookup(webAppURI), fronEndServer1, SPJobLockType.Job);
//Job associated with a webApp, and an app server and lock type job:
// it won't run on any server since the server specified is NOT running
// the Web Application Service
var jobRunningOnNoServer = new MyTimerJob1("mytimerjob",
SPWebApplication.Lookup(webAppURI), appServer1, SPJobLockType.Job);
//Job associated with the timer service, a particular app server and none lock:
// will run on the appServer1 server only.
var jobRunningOnAppServer = new MyTimerJob1("mytimerjob",
SPFarm.Local.TimerService, appServer1, SPJobLockType.None);
There are some other classes on the Sharepoint Object Model that inherit from the SPServiceJob definition and can be used to inherit our custom timer jobs from. For example:
- SPContentDatabaseJobDefinition: This job is executed by all WFE servers in the farm. Each content database is processed by only one job so that work is distributed across all the running jobs.
An abstract base class for a timer job that will be run on the first available server where the specified service is provisioned.
- SPServerJobDefinition:This job definition is executed on a specific server within the SharePoint farm.
- SPServiceJobDefinition: A timer job that runs on every server in the farm where the service is provisioned.
So, for example, if you need a job to run on all servers (including the app servers) it would be better to derive directly from the SPServiceJobDefinition class and, if you need a job to run in one particular app server, to derive from SPServerJobDefinition.