Job
OCP App SDK / Job
Class: abstract
Job
abstract
JobDefined in: src/app/Job.ts:41
Constructors
Constructor
new Job(
invocation
):Job
Defined in: src/app/Job.ts:61
Initializes a job to be run
Parameters
invocation
details of the job invocation
Returns
Job
Properties
invocation
protected
invocation:JobInvocation
Defined in: src/app/Job.ts:61
details of the job invocation
isInterruptible
isInterruptible:
boolean
=false
Defined in: src/app/Job.ts:53
Set this to true during an interruptible operation, such as waiting for a long running export.
When true, a job can be interrupted and resumed with the PREVIOUS Job state (the one perform was last called with).
A job is normally expected to complete a job loop (perform) within < 60s. Your job CAN perform a loop for longer
than 60 seconds if isInterruptible is set to true for a significant part of each 60 seconds of runtime
and is performing NON-BLOCKING operations.
IMPORTANT
You MUST ensure the process is NOT BLOCKED while interruptible. This can be achieved
by manually calling await this.sleep()
regularly or is automatic if you are waiting on non-blocking calls.
Job::sleep
and Job::performInterruptibleTask
will set this value automatically.
Methods
perform()
abstract
perform(status
):Promise
<JobStatus
>
Defined in: src/app/Job.ts:81
Performs a unit of work. Jobs should perform a small unit of work and then return the current state.
Perform is automatically called in a loop where the previously returned state will be given to the next iteration.
Iteration will continue until complete is set to true in the returned job status.
Parameters
status
last known job state and status
Returns
Promise
<JobStatus
>
The current JobStatus/state that can be used to perform the next iteration or resume a job if interrupted.
performInterruptibleTask()
protected
performInterruptibleTask<T
>(task
):Promise
<T
>
Defined in: src/app/Job.ts:90
Wrapper for interruptible tasks, such as waiting for a long api call or a timeout loop waiting for a result.
Interruptible tasks MUST BE NON-BLOCKING or must manually call await this.sleep()
regularly (every few seconds).
Type Parameters
T
T
Parameters
task
() => Promise
<T
>
Returns
Promise
<T
>
Usage
const result = await this.performInterruptibleTask(() => fetch(...)));
In this example, the job can be interrupted during the fetch operation, and if interrupted will be resumed
with the previous job state.
prepare()
abstract
prepare(params
,status?
,resuming?
):Promise
<JobStatus
>
Defined in: src/app/Job.ts:72
Prepares to run a job. Prepare is called at the start of a job
and again only if the job was interrupted and is being resumed.
Use this function to read secrets and establish connections to simplify the job loop (perform).
Parameters
params
a hash if params were supplied to the job run, otherwise an empty hash
status?
provided ONLY if the job was interrupted and should continue from the last known state
resuming?
boolean
if the job was interrupted, resuming will be set to true when it is resumed
Returns
Promise
<JobStatus
>
sleep()
protected
sleep(miliseconds?
,options?
):Promise
<void
>
Defined in: src/app/Job.ts:110
Sleep the job without CPU thrashing. Use this method to wait for long running tasks, like an export API.
Parameters
miliseconds?
number
duration to sleep in miliseconds
options?
{interruptible: true}
if the job can be interrupted while sleeping.
A sleep that is not interruptible cannot safely be longer than about 55 seconds.
Returns
Promise
<void
>
Usage
await this.sleep(5000);
Updated 3 days ago