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?

  • SPServer should be the third parameter as per this – Amal Hashim Nov 7 '14 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. – Slaven Semper Nov 7 '14 at 19:58
  • @SlavenSemper that link contains the Signature of the SPJobDefinition constructors. So I think it makes sense in this context. – Amal Hashim Nov 7 '14 at 20:00
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

  • 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? – Ravi Khambhati Nov 7 '14 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. – Amal Hashim Nov 7 '14 at 22:22
  • That's exactly i asked for, need to run timer job on servers running TimerJob Service. – Ravi Khambhati Nov 7 '14 at 22:41
  • OK my bad I read you want to run it only on Application Server. – Amal Hashim Nov 7 '14 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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