Class JobContext
java.lang.Object
org.jobrunr.jobs.context.JobContext
- Direct Known Subclasses:
RunnerJobContext
The JobContext class gives access to the Job id, the Job name, the state, ... .
Note: We recommend the use of the parametric approach to avoid mistakenly calling the
Usage
Using thegetMetadata(), it also allows to store some data between different job retries so jobs can be made re-entrant.
This comes in handy when your job exists out of multiple steps, and you want to keep track of which step already succeeded. Then,
in case of a failure, you can skip the steps that already completed successfully.
As soon as the job is completed successfully the metadata is cleared (for storage purpose reasons).
It may also be used to keep track of the progress of a job on the Dashboard:
public void doWork(JobContext jobContext) {
JobDashboardProgressBar progressBar = jobContext().progressBar(100);
for(Element e : elements) {
progressBar.increaseByOne();
}
}
Or used for logging purposes also reported to the Dashboard:
public void doWork(JobContext jobContext) {
jobContext.logger().info('this will appear in the dashboard');
}
Alternatively, you may use ThreadLocalJobContext.getJobContext():
public void doWork() {
var jobContext = ThreadLocalJobContext.getJobContext();
jobContext.logger().info('this will appear in the dashboard');
}
Note: We recommend the use of the parametric approach to avoid mistakenly calling the
doWork method outside a JobRunr execution context.
Testing
To test a method that uses aJobContext, use MockThreadLocalJobContext from JobRunr's test-fixtures to set a mock JobContext.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfacestatic interface -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedprotectedJobContext(Job job) Keep constructor package protected to remove confusion on how to instantiate the JobContext. -
Method Summary
Modifier and TypeMethodDescriptionintintgetJobId()Gives access to Job Metadata via an UnmodifiableMap.<T> TgetMetadata(String key) Allows retrieving the metadata with the given key from a Job.booleanhasCompletedStep(String stepName) Returns true if the given step has already completed successfully in a previous run.logger()progressBar(int totalAmount) progressBar(long totalAmount) voidrunStepOnce(String step, Exceptions.ThrowingRunnable task) Run the supplied task exactly once (i.e.<T> TrunStepOnce(String step, Exceptions.ThrowingSupplier<T> task) Run the supplied task exactly once (i.e.voidsaveMetadata(String key, Object metadata) Allows saving metadata for a certain Job.voidsaveMetadataIfAbsent(String key, Object metadata) Allows saving metadata for a certain Job.
-
Field Details
-
Null
-
-
Constructor Details
-
JobContext
protected JobContext() -
JobContext
Keep constructor package protected to remove confusion on how to instantiate the JobContext. Tip - To use the JobContext in your Job, pass JobContext.Null- Parameters:
job- the job for this JobContext
-
-
Method Details
-
getJobId
-
getJobName
-
getJobLabels
-
getJobState
-
getCreatedAt
-
getUpdatedAt
-
getJobSignature
-
currentRetry
public int currentRetry() -
amountOfFailures
public int amountOfFailures() -
logger
-
progressBar
-
progressBar
-
getMetadata
Gives access to Job Metadata via an UnmodifiableMap. To save Metadata, use thesaveMetadata(String, Object)method- Returns:
- all user defined metadata about a Job. This metadata is only accessible up to the point a job succeeds.
-
saveMetadata
Allows saving metadata for a certain Job. The value must either be a simple type (String, UUID, Integers, ...) or implement the Metadata interface for serialization to Json. Note that it is important that the objects you save are thread-safe (e.g. a CopyOnWriteArrayList, ... ).If the key already exists, the metadata is updated.
- Parameters:
key- the key to store the metadatametadata- the metadata itself
-
saveMetadataIfAbsent
Allows saving metadata for a certain Job. The value must either be a simple type (String, UUID, Integers, ...) or implement the Metadata interface for serialization to Json. Note that it is important that the objects you save are thread-safe (e.g. a CopyOnWriteArrayList, ... ).If the key already exists, the metadata is NOT updated.
- Parameters:
key- the key to store the metadatametadata- the metadata itself
-
getMetadata
Allows retrieving the metadata with the given key from a Job.- Parameters:
key- the key to retrieve the metadata- Returns:
- the given value associated with the provided key.
-
hasCompletedStep
Returns true if the given step has already completed successfully in a previous run. -
runStepOnce
public void runStepOnce(String step, Exceptions.ThrowingRunnable task) throws StepExecutionException Run the supplied task exactly once (i.e. only if it hasn’t already completed). If the task throws anyException, the step won’t be marked completed.- Parameters:
step- the name of the steptask- the step to execute as a Runnable- Throws:
StepExecutionException- when an exception happens during the execution of this step
-
runStepOnce
public <T> T runStepOnce(String step, Exceptions.ThrowingSupplier<T> task) throws StepExecutionException Run the supplied task exactly once (i.e. only if it hasn’t already completed) and allow to return a result (only java classes supported by theAutoboxeror an implementation ofJobContext.StepResultdue to JSON Serialization). If the task throws, the step won’t be marked completed.- Parameters:
step- the name of the steptask- the step to execute as a Runnable- Returns:
- the object returned by the
Exceptions.ThrowingSupplier<T>(only java classes supported by theAutoboxeror an implementation ofJobContext.StepResultis supported due to JSON Serialization) - Throws:
StepExecutionException- when an exception happens during the execution of this step
-