0

I would like to create a timer job which should run only on Application Server running Timer Service. I am little bit confused after reading couple of blog and came up with construtor based on my understanding.

internal class NewJob : SPJobDefinition
{
    public const string JOB_NAME = "New Job";

    public NewJob()
        : base()
    {
    }

    public NewJob(SPWebApplication webApp)
        : base(JOB_NAME, SPFarm.Local.TimerService, null, SPJobLockType.Job)
    {
        Title = string.Format("{0} {1}", JOB_NAME, webApp.DisplayName);
    }

    public override void Execute(Guid targetInstanceId)
    {
    }
}

Am I in the right path?

3
  • SPServer should be the third parameter as per this Commented Nov 7, 2014 at 19:45
  • @AmalHashim Don't take this the wrong way, but there is really no point in providing a link to MSDN SP documentation. It's useless. Commented Nov 7, 2014 at 19:58
  • @SlavenSemper that link contains the Signature of the SPJobDefinition constructors. So I think it makes sense in this context. Commented Nov 7, 2014 at 20:00

1 Answer 1

0

If you are targeting the timer job to be run on a specific server then use below snippet

class TimerJobCustom : SPServerJobDefinition
{   
    public TimerJobCustom(): base()
    {
    }
    public TimerJobCustom(string jobName, SPServer server): base(jobName, server)
    {
        this.Title = jobName;
    }
    public override void Execute(SPJobState _jobState)
    {
        Logs.LogMessage(Logs.Log_INFO, Logs.Log_severity_verbose, "Timer Job - Server specific is running on ");
    }
}

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    TimerJobCustom myJob = new TimerJobCustom("Server Timer Job", SPFarm.Local.Servers["Server"]);

    SPDailySchedule schedule = new SPDailySchedule();
    schedule.BeginHour = 1;
    schedule.EndHour = 2;

    myJob.Schedule = schedule;
    myJob.Update();
}

Specify the server name in feature activation event

4
  • what if we add a new server to the farm with TimerJob Role. Is there a way where we do not have to mention any server name? Commented Nov 7, 2014 at 21:38
  • @RaviKhambhati if you use SPFarm.Local.TimerService then the job will run on all servers which has OWSTimer service running. If you want to execute the service only on a specific server then follow my answer. You can modify it in such a way that you can read the server name from feature xml file. This will enable you to change the server name in future. Commented Nov 7, 2014 at 22:22
  • That's exactly i asked for, need to run timer job on servers running TimerJob Service. Commented Nov 7, 2014 at 22:41
  • OK my bad I read you want to run it only on Application Server. Commented Nov 7, 2014 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.