Skip to content

Commit

Permalink
Merge pull request #77 from golemfactory/bump-golem-js
Browse files Browse the repository at this point in the history
Golem-js beta 3 New API
  • Loading branch information
SewerynKras authored Jul 10, 2024
2 parents f6ae452 + c31145d commit 0b3785c
Show file tree
Hide file tree
Showing 111 changed files with 6,993 additions and 3,787 deletions.
13 changes: 12 additions & 1 deletion .github/actions/prepare-tests/action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
name: "Prepare providers and requestor"
description: "Prepare providers and requestor"
inputs:
type:
description: "Type of test"
required: true
default: "test"
runs:
using: "composite"
steps:
Expand Down Expand Up @@ -43,4 +48,10 @@ runs:
- name: Install and build the SDK in the docker container
shell: bash
run: |
docker exec -t docker-requestor-1 /bin/sh -c "cd /golem-sdk-task-executor && npm i && npm run build && ./node_modules/.bin/cypress install && npm install --prefix examples"
docker exec -t docker-requestor-1 /bin/sh -c "cd /golem-sdk-task-executor && npm i && npm run build"
- name: Install Cypress
if: ${{ inputs.type == 'cypress' }}
shell: bash
run: |
docker exec -t docker-requestor-1 /bin/sh -c "cd /golem-sdk-task-executor && ./node_modules/.bin/cypress install"
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ jobs:
npm run lint
npm run test:unit
npm run build
npm install --prefix examples
npm install --prefix examples --legacy-peer-deps
npm run --prefix examples lint:ts
npm run test:import
16 changes: 9 additions & 7 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ on:
required: false
default: "holesky"
provider_version:
description: "Provider version (e.g., v0.14.0 or pre-rel-v0.14.1)"
description: "Provider version (e.g., v0.15.2 or pre-rel-v0.15.1)"
required: false
default: "v0.14.0"
default: "v0.15.2"
requestor_version:
description: "Requestor version (e.g., v0.14.0 or pre-rel-v0.14.1)"
description: "Requestor version (e.g., v0.15.2 or pre-rel-v0.15.1)"
required: false
default: "v0.14.0"
default: "v0.15.2"
provider_wasi_version:
description: "Provider WASI version (e.g., v0.2.2)"
required: false
Expand All @@ -40,8 +40,8 @@ permissions:
contents: read # for checkout

env:
PROVIDER_VERSION: ${{ github.event.inputs.provider_version || 'v0.14.0' }}
REQUESTOR_VERSION: ${{ github.event.inputs.requestor_version || 'v0.14.0' }}
PROVIDER_VERSION: ${{ github.event.inputs.provider_version || 'v0.15.2' }}
REQUESTOR_VERSION: ${{ github.event.inputs.requestor_version || 'v0.15.2' }}
PROVIDER_WASI_VERSION: ${{ github.event.inputs.provider_wasi_version || 'v0.2.2' }}
PROVIDER_VM_VERSION: ${{ github.event.inputs.provider_vm_version || 'v0.3.0' }}
PAYMENT_NETWORK: ${{ github.event.inputs.payment_network || 'holesky' }}
Expand Down Expand Up @@ -74,7 +74,7 @@ jobs:
npm run lint
npm run test:unit
npm run build
npm install --prefix examples
npm install --prefix examples --legacy-peer-deps
npm run --prefix examples lint:ts
npm run test:import
- name: Upload unit test reports
Expand Down Expand Up @@ -118,6 +118,8 @@ jobs:

- name: Prepare providers and requestor
uses: ./.github/actions/prepare-tests
with:
type: "cypress"

- name: Copy docs examples
run: docker exec -t docker-requestor-1 /bin/sh -c '
Expand Down
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ with the Golem Network or managing modules such as payments or market.

## System requirements

To use `task-executor`, it is necessary to have yagna installed, with a **recommended minimum version of v0.14.0**. Yagna is a
To use `task-executor`, it is necessary to have yagna installed, with a **recommended minimum version of v0.15.0**. Yagna is a
service that communicates and performs operations on the Golem Network, upon your requests via the SDK. You
can [follow these instructions](https://docs.golem.network/docs/creators/javascript/quickstarts/quickstart#install-yagna-2)
to set it up.
Expand Down Expand Up @@ -101,14 +101,29 @@ This will generate production code in the `dist/` directory ready to be used in
### Hello World example

```ts
import { TaskExecutor, WorkContext } from "@golem-sdk/task-executor";
import { TaskExecutor } from "@golem-sdk/task-executor";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

(async function main() {
const executor = await TaskExecutor.create("golem/alpine:latest");
const executor = await TaskExecutor.create({
logger: pinoPrettyLogger({ level: "info" }),
demand: {
workload: {
imageTag: "golem/alpine:latest",
},
},
market: {
rentHours: 0.5,
pricing: {
model: "linear",
maxStartPrice: 0.5,
maxCpuPerHourPrice: 1.0,
maxEnvPerHourPrice: 0.5,
},
},
});
try {
const task = async (ctx: WorkContext) => (await ctx.run("echo 'Hello World'")).stdout?.toString();
const result = await executor.run(task);
console.log("Result:", result);
await executor.run(async (exe) => console.log((await exe.run("echo 'Hello World'")).stdout));
} catch (error) {
console.error("Computation failed:", error);
} finally {
Expand Down
46 changes: 30 additions & 16 deletions examples/blender/blender.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TaskExecutor, pinoPrettyLogger } from "@golem-sdk/task-executor";
import { TaskExecutor } from "@golem-sdk/task-executor";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";
import { program } from "commander";
import { fileURLToPath } from "url";

Expand All @@ -23,34 +24,47 @@ const blenderParams = (frame) => ({
OUTPUT_DIR: "/golem/output",
});

async function main(subnetTag: string, driver?: string, network?: string, maxParallelTasks?: number) {
async function main(subnetTag: string, driver?: "erc20", network?: string, maxParallelTasks?: number) {
const setup = async (exe) => {
console.log("Uploading the scene to the provider %s", exe.provider.name);
await exe.uploadFile(`${DIR_NAME}/cubes.blend`, "/golem/resource/scene.blend");
console.log("Upload of the scene to the provider %s finished", exe.provider.name);
};
const executor = await TaskExecutor.create({
subnetTag,
payment: { driver, network },
package: "golem/blender:latest",
maxParallelTasks,
task: {
maxParallelTasks,
setup,
},
logger: pinoPrettyLogger(),
payment: { driver, network },
demand: {
workload: { imageTag: "golem/blender:latest" },
subnetTag,
},
market: {
rentHours: 0.5,
pricing: {
model: "linear",
maxStartPrice: 0.5,
maxCpuPerHourPrice: 1.0,
maxEnvPerHourPrice: 0.5,
},
},
});

try {
executor.onActivityReady(async (ctx) => {
console.log("Uploading the scene to the provider %s", ctx.provider.name);
await ctx.uploadFile(`${DIR_NAME}/cubes.blend`, "/golem/resource/scene.blend");
console.log("Upload of the scene to the provider %s finished", ctx.provider.name);
});

const futureResults = [0, 10, 20, 30, 40, 50].map(async (frame) =>
executor.run(async (ctx) => {
console.log("Started rendering of frame %d on provider %s", frame, ctx.provider.name);
executor.run(async (exe) => {
console.log("Started rendering of frame %d on provider %s", frame, exe.provider.name);

const result = await ctx
const result = await exe
.beginBatch()
.uploadJson(blenderParams(frame), "/golem/work/params.json")
.run("/golem/entrypoints/run-blender.sh")
.downloadFile(`/golem/output/out${frame?.toString().padStart(4, "0")}.png`, `${DIR_NAME}/output_${frame}.png`)
.end();

console.log("Finished rendering of frame %d on provider %s", frame, ctx.provider.name);
console.log("Finished rendering of frame %d on provider %s", frame, exe.provider.name);

return result?.length ? `output_${frame}.png` : "";
}),
Expand Down
28 changes: 21 additions & 7 deletions examples/docs-examples/examples/composing-tasks/alert-code.mjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
import { TaskExecutor, pinoPrettyLogger } from "@golem-sdk/task-executor";
import { TaskExecutor } from "@golem-sdk/task-executor";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";
(async () => {
const executor = await TaskExecutor.create({
package: "golem/node:20-alpine",
logger: pinoPrettyLogger(),
yagnaOptions: { apiKey: "try_golem" },
api: { key: "try_golem" },
demand: {
workload: {
imageTag: "golem/node:20-alpine",
},
},
market: {
rentHours: 0.5,
pricing: {
model: "linear",
maxStartPrice: 0.5,
maxCpuPerHourPrice: 1.0,
maxEnvPerHourPrice: 0.5,
},
},
});

try {
const result = await executor.run(async (ctx) => {
const res = await ctx
const result = await executor.run(async (exe) => {
const res = await exe
.beginBatch()
.uploadFile("./worker.mjs", "/golem/input/worker.mjs")
.run("node /golem/input/worker.mjs > /golem/input/output.txt")
.run("cat /golem/input/output.txt")
.downloadFile("/golem/input/output.txt", "./output.txt")
.endStream();

for await (const chunk of res) {
res.subscribe((chunk) => {
if (chunk.index === 2) console.log(chunk.stdout);
}
});
});
} catch (err) {
console.error("Task encountered an error:", err);
Expand Down
24 changes: 19 additions & 5 deletions examples/docs-examples/examples/composing-tasks/batch-end.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
import { TaskExecutor, pinoPrettyLogger } from "@golem-sdk/task-executor";
import { TaskExecutor } from "@golem-sdk/task-executor";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

(async () => {
const executor = await TaskExecutor.create({
package: "golem/node:20-alpine",
logger: pinoPrettyLogger(),
yagnaOptions: { apiKey: "try_golem" },
api: { key: "try_golem" },
demand: {
workload: {
imageTag: "golem/node:20-alpine",
},
},
market: {
rentHours: 0.5,
pricing: {
model: "linear",
maxStartPrice: 0.5,
maxCpuPerHourPrice: 1.0,
maxEnvPerHourPrice: 0.5,
},
},
});

try {
const result = await executor.run(async (ctx) => {
const result = await executor.run(async (exe) => {
return (
await ctx
await exe
.beginBatch()
.uploadFile("./worker.mjs", "/golem/input/worker.mjs")
.run("node /golem/input/worker.mjs > /golem/input/output.txt")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { TaskExecutor, pinoPrettyLogger } from "@golem-sdk/task-executor";
import { TaskExecutor } from "@golem-sdk/task-executor";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

(async () => {
const executor = await TaskExecutor.create({
package: "golem/node:20-alpine",
logger: pinoPrettyLogger(),
yagnaOptions: { apiKey: "try_golem" },
logger: pinoPrettyLogger({ level: "info" }),
api: { key: "try_golem" },
demand: {
workload: {
imageTag: "golem/node:20-alpine",
},
},
market: {
rentHours: 0.5,
pricing: {
model: "linear",
maxStartPrice: 0.5,
maxCpuPerHourPrice: 1.0,
maxEnvPerHourPrice: 0.5,
},
},
});

try {
const result = await executor.run(async (ctx) => {
const res = await ctx
const result = await executor.run(async (exe) => {
const res = await exe
.beginBatch()
.uploadFile("./worker.mjs", "/golem/input/worker.mjs")
.run("node /golem/input/worker.mjs > /golem/input/output.txt")
Expand All @@ -18,9 +32,11 @@ import { TaskExecutor, pinoPrettyLogger } from "@golem-sdk/task-executor";
.endStream();

return new Promise((resolve) => {
res.on("data", (result) => console.log(result));
res.on("error", (error) => console.error(error));
res.once("close", resolve);
res.subscribe({
next: (result) => console.log(result),
error: (error) => console.error(error),
complete: () => resolve(),
});
});
});
} catch (error) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import { TaskExecutor, pinoPrettyLogger } from "@golem-sdk/task-executor";
import { TaskExecutor } from "@golem-sdk/task-executor";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

(async () => {
const executor = await TaskExecutor.create({
package: "golem/node:20-alpine",
logger: pinoPrettyLogger(),
yagnaOptions: { apiKey: "try_golem" },
logger: pinoPrettyLogger({ level: "info" }),
api: { key: "try_golem" },
demand: {
workload: {
imageTag: "golem/node:20-alpine",
},
},
market: {
rentHours: 0.5,
pricing: {
model: "linear",
maxStartPrice: 0.5,
maxCpuPerHourPrice: 1.0,
maxEnvPerHourPrice: 0.5,
},
},
});

try {
const result = await executor.run(async (ctx) => {
await ctx.uploadFile("./worker.mjs", "/golem/input/worker.mjs");
await ctx.run("node /golem/input/worker.mjs > /golem/input/output.txt");
const result = await ctx.run("cat /golem/input/output.txt");
await ctx.downloadFile("/golem/input/output.txt", "./output.txt");
const result = await executor.run(async (exe) => {
await exe.uploadFile("./worker.mjs", "/golem/input/worker.mjs");
await exe.run("node /golem/input/worker.mjs > /golem/input/output.txt");
const result = await exe.run("cat /golem/input/output.txt");
await exe.downloadFile("/golem/input/output.txt", "./output.txt");
return result.stdout;
});

Expand Down
Loading

0 comments on commit 0b3785c

Please sign in to comment.