reference

Class: TaskExecutor

executor/executor.TaskExecutor

A high-level module for defining and executing tasks in the golem network

Table of contents

Methods

Methods

create

Static create(options): Promise<TaskExecutor>

Create a new Task Executor

Parameters

NameTypeDescription
optionsExecutorOptionsMixinTask executor options

Returns

Promise<TaskExecutor>

TaskExecutor

Description

Factory Method that create and initialize an instance of the TaskExecutor

Example

Simple usage of Task Executor

The executor can be created by passing appropriate initial parameters such as package, budget, subnet tag, payment driver, payment network etc. One required parameter is a package. This can be done in two ways. First by passing only package image hash or image tag, e.g.

const executor = await TaskExecutor.create("9a3b5d67b0b27746283cb5f287c13eab1beaa12d92a9f536b747c7ae");

or

const executor = await TaskExecutor.create("golem/alpine:3.18.2");

Example

Usage of Task Executor with custom parameters

Or by passing some optional parameters, e.g.

const executor = await TaskExecutor.create({
  subnetTag: "public",
  payment: { driver: "erc-20", network: "goerli" },
  package: "golem/alpine:3.18.2",
});

Defined in

src/executor/executor.ts:147


init

init(): Promise<void>

Initialize executor

Returns

Promise<void>

Description

Method responsible initialize all executor services.

Defined in

src/executor/executor.ts:198


end

end(): Promise<void>

Stop all executor services and shut down executor instance

Returns

Promise<void>

Defined in

src/executor/executor.ts:248


getStats

getStats(): Object

Statistics of execution process

Returns

Object

array

Defined in

src/executor/executor.ts:269


beforeEach

beforeEach(worker): void

Define worker function that will be runs before every each computation Task, within the same activity.

Parameters

NameTypeDescription
workerWorkerworker function - task

Returns

void

Example

executor.beforeEach(async (ctx) => {
  await ctx.uploadFile("./params.txt", "/params.txt");
});

await executor.forEach([1, 2, 3, 4, 5], async (ctx, item) => {
   await ctx
     .beginBatch()
     .run(`/run_some_command.sh --input ${item} --params /input_params.txt --output /output.txt`)
     .downloadFile("/output.txt", "./output.txt")
     .end();
});

Defined in

src/executor/executor.ts:292


run

run<OutputType>(worker, options?): Promise<undefined | OutputType>

Run task - allows to execute a single worker function on the Golem network with a single provider.

Type parameters

NameType
OutputTypeResult<any>

Parameters

NameTypeDescription
workerWorker<undefined, OutputType>function that run task
options?TaskOptionstask options

Returns

Promise<undefined | OutputType>

result of task computation

Example

await executor.run(async (ctx) => console.log((await ctx.run("echo 'Hello World'")).stdout));

Defined in

src/executor/executor.ts:307


map

map<InputType, OutputType>(data, worker): AsyncIterable<undefined | OutputType>

Map iterable data to worker function and return computed Task result as AsyncIterable

Type parameters

Name
InputType
OutputType

Parameters

NameTypeDescription
dataIterable<InputType>Iterable data
workerWorker<InputType, OutputType>worker function

Returns

AsyncIterable<undefined | OutputType>

AsyncIterable with results of computed tasks

Example

const data = [1, 2, 3, 4, 5];
const results = executor.map(data, (ctx, item) => ctx.run(`echo "${item}"`));
for await (const result of results) console.log(result.stdout);

Defined in

src/executor/executor.ts:330


forEach

forEach<InputType, OutputType>(data, worker): Promise<void>

Iterates over given data and execute task using worker function

Type parameters

Name
InputType
OutputType

Parameters

NameTypeDescription
dataIterable<InputType>Iterable data
workerWorker<InputType, OutputType>Worker function

Returns

Promise<void>

Example

const data = [1, 2, 3, 4, 5];
await executor.forEach(data, async (ctx, item) => {
    console.log((await ctx.run(`echo "${item}"`)).stdout);
});

Defined in

src/executor/executor.ts:378


createJob

createJob<InputType, OutputType>(worker): Promise<Job<OutputType>>

Start a new job without waiting for the result. The job can be retrieved later using TaskExecutor.getJobById. The job's status is stored in the JobStorage provided in the ExecutorOptions (in-memory by default). For distributed environments, it is recommended to use a form of storage that is accessible from all nodes (e.g. a database).

Type parameters

NameType
InputTypeunknown
OutputTypeunknown

Parameters

NameTypeDescription
workerWorker<InputType, OutputType>Worker function to be executed

Returns

Promise<Job<OutputType>>

Job object

Example

Simple usage of createJob

const job = executor.createJob(async (ctx) => {
 return (await ctx.run("echo 'Hello World'")).stdout;
});
// save job.id somewhere

// later...
const job = await executor.fetchJob(jobId);
const status = await job.fetchState();
const results = await job.fetchResults();
const error = await job.fetchError();

Defined in

src/executor/executor.ts:440


getJobById

getJobById(jobId): Job<unknown>

Retrieve a job by its ID. The job's status is stored in the JobStorage provided in the ExecutorOptions (in-memory by default). Use Job.fetchState, Job.fetchResults and Job.fetchError to get the job's status.

Parameters

NameTypeDescription
jobIdstringJob ID

Returns

Job<unknown>

Job object.

Defined in

src/executor/executor.ts:465


cancel

cancel(reason?): Promise<void>

Parameters

NameType
reason?string

Returns

Promise<void>

Defined in

src/executor/executor.ts:490