Skip to content

Commit

Permalink
Merge pull request #19 from golemfactory/master
Browse files Browse the repository at this point in the history
Master -> Beta sync
  • Loading branch information
mgordel authored Mar 6, 2024
2 parents 9da5f16 + 4828df7 commit 0c7dad2
Show file tree
Hide file tree
Showing 28 changed files with 1,210 additions and 1,462 deletions.
13 changes: 4 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@
.env

#Node modules
/node_modules
/examples/logs
/examples/node_modules
/examples/adv-web-example/node_modules
/tests/web/node_modules/
node_modules/

#Outputs
dist/
coverage/
/.nyc_output
/coverage
/dist
/examples/dist
/examples/blender/*.png
/examples/yacat/*.sh
/examples/yacat/*.txt
Expand All @@ -33,4 +28,4 @@ logs/
#Nuxt
/examples/adv-web-example/.nuxt
tmp/
reports/
reports/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![GitHub](https://img.shields.io/github/license/golemfactory/golem-sdk-task-executor)
![npm](https://img.shields.io/npm/v/@golem-sdk/task-executor)
![node-current](https://img.shields.io/node/v/@golem-sdk/golem-sdk-task-executor)
![node-current](https://img.shields.io/node/v/@golem-sdk/task-executor)
![npm type definitions](https://img.shields.io/npm/types/@golem-sdk/task-executor)

## What's TaskExecutor?
Expand Down
4 changes: 3 additions & 1 deletion examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.bin
*.gvmi
*.gvmi

package-lock.json
15 changes: 14 additions & 1 deletion examples/blender/blender.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TaskExecutor } from "@golem-sdk/task-executor";
import { program } from "commander";
import { fileURLToPath } from "url";

const DIR_NAME = fileURLToPath(new URL(".", import.meta.url));

const blenderParams = (frame) => ({
Expand All @@ -21,6 +22,7 @@ const blenderParams = (frame) => ({
WORK_DIR: "/golem/work",
OUTPUT_DIR: "/golem/output",
});

async function main(subnetTag: string, driver?: string, network?: string, maxParallelTasks?: number) {
const executor = await TaskExecutor.create({
subnetTag,
Expand All @@ -31,21 +33,32 @@ async function main(subnetTag: string, driver?: string, network?: string, maxPar

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((frame) =>
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);

const result = await ctx
.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);

return result?.length ? `output_${frame}.png` : "";
}),
);

console.log("Scheduling all tasks");
const results = await Promise.all(futureResults);
console.log("Completed all tasks");

results.forEach((result) => console.log(result));
} catch (error) {
console.error("Computation failed:", error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TaskExecutor } from "@golem-sdk/task-executor";

var costData = [];

const myFilter = async (proposal) => {
const myFilter = (proposal) => {
let decision = false;
let usageVector = proposal.properties["golem.com.usage.vector"];
let counterIdx = usageVector.findIndex((ele) => ele === "golem.usage.duration_sec");
Expand Down
23 changes: 0 additions & 23 deletions examples/docs-examples/quickstarts/retrievable-task/task.mjs

This file was deleted.

This file was deleted.

6 changes: 3 additions & 3 deletions examples/fibonacci/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:16-alpine
COPY fibo.js /golem/work/
FROM node:18-alpine
COPY dist/fibo.js /golem/work/
VOLUME /golem/input
WORKDIR /golem/work
WORKDIR /golem/work
13 changes: 7 additions & 6 deletions examples/fibonacci/fibo.js → examples/fibonacci/fibo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-env node */

function fiboLoop(n) {
type GetFibonacciSeries = (n: number) => number;

function fiboLoop(n: number) {
let a = 1,
b = 0;
while (n >= 0) {
Expand All @@ -10,25 +12,24 @@ function fiboLoop(n) {
return b;
}

function fiboRecur(n) {
function fiboRecur(n: number): number {
return n > 1 ? fiboRecur(n - 1) + fiboRecur(n - 2) : 1;
}

function fiboMemo(n, memo) {
memo = memo || {};
function fiboMemo(n: number, memo: Record<number, number> = {}): number {
if (memo[n]) return memo[n];
if (n <= 1) return 1;
return (memo[n] = fiboMemo(n - 1, memo) + fiboMemo(n - 2, memo));
}

function benchmark(fn, n, type) {
function benchmark(fn: GetFibonacciSeries, n: number, type: string) {
const timeStart = process.hrtime();
const result = fn(n);
const timeSTop = process.hrtime(timeStart);
return { type, result, time: parseFloat(timeSTop.join(".")) };
}

const n = process.argv[2] || 1;
const n = parseInt(process.argv[2] ?? "1");

const results = [
benchmark(fiboLoop, n, "loop"),
Expand Down
40 changes: 0 additions & 40 deletions examples/fibonacci/fibonacci.js

This file was deleted.

46 changes: 46 additions & 0 deletions examples/fibonacci/fibonacci.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { TaskExecutor } from "@golem-sdk/task-executor";
import { program } from "commander";

type MainOptions = {
subnetTag: string;
paymentDriver: string;
paymentNetwork: string;
tasksCount: number;
fibonacciNumber: number;
};

program
.option("-n, --fibonacci-number <n>", "fibonacci number", "1")
.option("-c, --tasks-count <c>", "tasks count", "1")
.option("--subnet-tag <subnet>", "set subnet name, for example 'public'", "public")
.option("--payment-driver, --driver <driver>", "payment driver name, for example 'erc20'", "erc20")
.option("--payment-network, --network <network>", "network name, for example 'goerli'", "goerli")
.action(async (options: MainOptions) => {
const executor = await TaskExecutor.create({
package: "golem/js-fibonacci:latest",
subnetTag: options.subnetTag,
payment: { driver: options.paymentDriver, network: options.paymentNetwork },
});

const runningTasks: Promise<string | undefined>[] = [];
for (let i = 0; i < options.tasksCount; i++) {
runningTasks.push(
executor.run(async (ctx) => {
const result = await ctx.run("/usr/local/bin/node", [
"/golem/work/fibo.js",
options.fibonacciNumber.toString(),
]);
console.log(result.stdout);
return result.stdout?.toString().trim();
}),
);
}

try {
await Promise.all(runningTasks);
} finally {
await executor.shutdown();
}
});

program.parse();
18 changes: 18 additions & 0 deletions examples/fibonacci/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "golem/js-fibonacci",
"description": "Example nodejs project that should end-up as a docker image that can be downloaded from the Golem Registry to be run on the provider",
"version": "1.0.0",
"repository": "https://github.com/golemfactory/golem-js",
"private": true,
"scripts": {
"compile": "tsc",
"image:build": "docker build -t $npm_package_name:latest .",
"image:publish": "gvmkit-build --push $npm_package_name:latest",
"build": "npm run compile && npm run image:build && npm run image:publish"
},
"author": "GolemFactory <[email protected]>",
"license": "LGPL-3.0",
"devDependencies": {
"typescript": "^5.3.3"
}
}
12 changes: 12 additions & 0 deletions examples/fibonacci/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "esnext" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"module": "commonjs" /* Specify what module code is generated. */,
"rootDir": "./src" /* Specify the root folder within your source files. */,
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
"strict": true /* Enable all strict type-checking options. */,
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
File renamed without changes.
Loading

0 comments on commit 0c7dad2

Please sign in to comment.