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

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


init

init(): Promise<void>

Initialize executor

Returns

Promise<void>

Description

Method responsible initialize all executor services.

Defined in

src/executor/executor.ts:209


end

end(): 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.

Returns

Promise<void>

Defined in

src/executor/executor.ts:261


getStats

getStats(): Object

Statistics of execution process

Returns

Object

array

Defined in

src/executor/executor.ts:293


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


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


map

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

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

Deprecated

This method is marked for removal in a future release. Migrate your code by using Array.map and Promise.all instead.

Example

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

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

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


forEach

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

Type parameters

Name
InputType
OutputType

Parameters

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

Returns

Promise<void>

Deprecated

This method is marked for removal in a future release. Migrate your code by using Array.map and Promise.all instead.

Example

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

Iterates over given data and execute task using worker function

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


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


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


cancel

cancel(reason?): Promise<void>

Parameters

NameType
reason?string

Returns

Promise<void>

Defined in

src/executor/executor.ts:539