JS API Reference

Class: TaskExecutor

executor/executor.TaskExecutor

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

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new TaskExecutor(options): TaskExecutor

Create a new TaskExecutor object.

Parameters

NameTypeDescription
optionsExecutorOptionsMixincontains information needed to start executor, if string the imageHash is required, otherwise it should be a type of ExecutorOptions

Returns

TaskExecutor

Defined in

src/executor/executor.ts:169

Properties

events

Readonly events: EventEmitter<TaskExecutorEventsDict, any>

EventEmitter (EventEmitter3) instance emitting TaskExecutor events.

See

TaskExecutorEventsDict for available events.

Defined in

src/executor/executor.ts:94

Methods

create

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

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

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:158


init

init(): Promise<void>

Initialize executor

Returns

Promise<void>

Description

Method responsible initialize all executor services.

Defined in

src/executor/executor.ts:222


shutdown

shutdown(): Promise<void>

Stop all executor services and shut down executor instance.

You can call this method multiple times, it will resolve only once the executor is shutdown.

When shutdown() is initially called, a beforeEnd event is emitted.

Once the executor is fully stopped, an end event is emitted.

Returns

Promise<void>

Defined in

src/executor/executor.ts:286


getStats

getStats(): Object

Statistics of execution process

Returns

Object

array

Defined in

src/executor/executor.ts:323


onActivityReady

onActivityReady(worker): void

Registers a worker function that will be run when an activity is ready. This is the perfect place to run setup functions that need to be run only once per activity, for example uploading files that will be used by all tasks in the activity. This function can be called multiple times, each worker will be run in the order they were registered.

Parameters

NameTypeDescription
workerWorker<unknown>worker function that will be run when an activity is ready

Returns

void

Example

const uploadFile1 = async (ctx) => ctx.uploadFile("./file1.txt", "/file1.txt");
const uploadFile2 = async (ctx) => ctx.uploadFile("./file2.txt", "/file2.txt");

executor.onActivityReady(uploadFile1);
executor.onActivityReady(uploadFile2);

await executor.run(async (ctx) => {
 await ctx.run("cat /file1.txt /file2.txt");
});

Defined in

src/executor/executor.ts:348


run

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

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

Type parameters

Name
OutputType

Parameters

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

Returns

Promise<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:363


cancel

cancel(reason): Promise<void>

Parameters

NameType
reasonstring

Returns

Promise<void>

Defined in

src/executor/executor.ts:414

Was this helpful?