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
Name | Type | Description |
---|---|---|
options | ExecutorOptionsMixin | Task 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
init
▸ init(): Promise
<void
>
Initialize executor
Returns
Promise
<void
>
Description
Method responsible initialize all executor services.
Defined in
end
▸ end(): Promise
<void
>
Stop all executor services and shut down executor instance
Returns
Promise
<void
>
Defined in
getStats
▸ getStats(): Object
Statistics of execution process
Returns
Object
array
Defined in
beforeEach
▸ beforeEach(worker
): void
Define worker function that will be runs before every each computation Task, within the same activity.
Parameters
Name | Type | Description |
---|---|---|
worker | Worker | worker 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
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
Name | Type |
---|---|
OutputType | Result <any > |
Parameters
Name | Type | Description |
---|---|---|
worker | Worker <undefined , OutputType > | function that run task |
options? | TaskOptions | task 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
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
Name | Type | Description |
---|---|---|
data | Iterable <InputType > | Iterable data |
worker | Worker <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
forEach
▸ forEach<InputType
, OutputType
>(data
, worker
): Promise
<void
>
Iterates over given data and execute task using worker function
Type parameters
Name |
---|
InputType |
OutputType |
Parameters
Name | Type | Description |
---|---|---|
data | Iterable <InputType > | Iterable data |
worker | Worker <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
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
Name | Type |
---|---|
InputType | unknown |
OutputType | unknown |
Parameters
Name | Type | Description |
---|---|---|
worker | Worker <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
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
Name | Type | Description |
---|---|---|
jobId | string | Job ID |
Returns
Job
<unknown
>
Job object.
Defined in
cancel
▸ cancel(reason?
): Promise
<void
>
Parameters
Name | Type |
---|---|
reason? | string |
Returns
Promise
<void
>