Dev Guide
Dev GuideUser GuidesLegal TermsGitHubDevCommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

OCP App SDK / Job

Job

Index

Constructors

Properties

Methods

Constructors

constructor()

Initializes a job to be run

Signature

new Job(invocation: JobInvocation): Job;

Parameters

NameTypeDescription
invocationJobInvocationdetails of the job invocation

Returns

Job

Defined in: src/app/Job.ts:61

Properties

invocation

Protected JobInvocation

details of the job invocation

Defined in: src/app/Job.ts:61

isInterruptible

boolean = false

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.

Defined in: src/app/Job.ts:53

Methods

perform()

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.

Signature

Abstract perform(status: JobStatus): Promise<JobStatus>;

Parameters

NameTypeDescription
statusJobStatuslast 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.

Defined in: src/app/Job.ts:81

performInterruptibleTask()

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).

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.

Signature

Protected performInterruptibleTask<T>(task: Function): Promise<T>;

Type parameters

  • T

Parameters

NameType
task() => Promise<T>

Returns

Promise<T>

Defined in: src/app/Job.ts:90

prepare()

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).

Signature

Abstract prepare(params: ValueHash, status?: JobStatus, resuming?: boolean): Promise<JobStatus>;

Parameters

NameTypeDescription
paramsValueHasha hash if params were supplied to the job run, otherwise an empty hash
status?JobStatusprovided ONLY if the job was interrupted and should continue from the last known state
resuming?booleanif the job was interrupted, resuming will be set to true when it is resumed

Returns

Promise<JobStatus>

Defined in: src/app/Job.ts:72

sleep()

Sleep the job without CPU thrashing. Use this method to wait for long running tasks, like an export API.

Usage

await this.sleep(5000);

Signature

Protected sleep(miliseconds?: number, options?: SleepOptions): Promise<void>;

Parameters

NameTypeDescription
miliseconds?numberduration to sleep in miliseconds
options?SleepOptions{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>

Defined in: src/app/Job.ts:110