Table of Contents

Class NativeJob

Namespace
Quartz.Jobs
Assembly
Quartz.Jobs.dll

Built in job for executing native executables in a separate process.

[RetryPolicy(0)]
public class NativeJob : IJob
Inheritance
NativeJob
Implements
Inherited Members

Remarks

What is run is configured through the job data keys below. NativeJobOptions names them all, and UsingNativeJobOptions<TConfigurator>(TConfigurator, NativeJobOptions) writes them, so the settings can be given as a value rather than as string keys; the keys stay the persisted form either way.

IJobDetail job = JobBuilder.Create<NativeJob>() .WithIdentity("dumbJob") .UsingNativeJobOptions(new NativeJobOptions { Command = "echo \"hi\" >> foobar.txt" }) .Build();
ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity("dumbTrigger")
    .WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromSeconds(5)).RepeatForever())
    .Build();

await scheduler.ScheduleJob(job, trigger);

On Windows the command runs as cmd.exe /C command parameters. On Linux the command and its parameters are joined into the one script /bin/sh -c runs. Elsewhere the command is started itself, with the parameters as its arguments.

If PropertyWaitForProcess is true, the integer exit value of the process is the job execution result in the JobExecutionContext, and cancelling the job (an interrupt, or a shutdown that interrupts running jobs) kills the process and its descendants and ends the job with OperationCanceledException. If it is false, the job returns as soon as the process has started, and the process is deliberately left running whatever happens to the job.

With PropertyConsumeStreams on as well, every line of output is logged before the job completes, unless a process it started and left running keeps the streams open more than five seconds after it exits. The job then completes, and that process's lines go on being logged.

Never retried unless its trigger names a retry policy of its own: it carries [RetryPolicy(0)], so the scheduler's default does not apply. A retry would run the command again.

Constructors

NativeJob()

Initializes a new instance of the NativeJob class.

public NativeJob()

Fields

PropertyCommand

Required parameter that specifies the name of the command (executable) to be ran.

public const string PropertyCommand = "command"

Field Value

string

PropertyConsumeStreams

Optional parameter (value should be 'true' or 'false') that specifies whether the spawned process's stdout and stderr streams should be consumed. If the process creates output, it is possible that it might 'hang' if the streams are not consumed.

Defaults to false.

public const string PropertyConsumeStreams = "consumeStreams"

Field Value

string

PropertyParameters

Optional parameter that specifies the parameters to be passed to the executed command.

public const string PropertyParameters = "parameters"

Field Value

string

PropertyWaitForProcess

Optional parameter (value should be 'true' or 'false') that specifies whether the job should wait for the execution of the native process to complete before it completes.

Defaults to true.

public const string PropertyWaitForProcess = "waitForProcess"

Field Value

string

PropertyWorkingDirectory

Optional parameter that specifies the working directory to be used by the executed command.

public const string PropertyWorkingDirectory = "workingDirectory"

Field Value

string

Methods

Execute(IJobExecutionContext, CancellationToken)

Called by the IScheduler when a ITrigger fires that is associated with the IJob.

The implementation may wish to set a result object on the JobExecutionContext before this method exits. The result itself is meaningless to Quartz, but may be informative to IJobListeners or ITriggerListeners that are watching the job's execution.

public virtual ValueTask Execute(IJobExecutionContext context, CancellationToken cancellationToken = default)

Parameters

context IJobExecutionContext

The firing this job is running for, whose merged job data names the command.

cancellationToken CancellationToken

The cancellation instruction.

Returns

ValueTask