From c3ecb38dfca8d4d1a1a9ad835f5eabbc9cd6c263 Mon Sep 17 00:00:00 2001 From: Marcin Gordel Date: Wed, 28 Aug 2024 11:55:53 +0200 Subject: [PATCH 1/3] feat: added example of using `oneOf` to nodejs template docs: added comments in nodejs templates --- .../js-node-esm/src/index.js | 78 +++++++++++++------ data/project-templates/js-node/src/index.js | 78 +++++++++++++------ data/project-templates/ts-node/src/index.ts | 78 +++++++++++++------ 3 files changed, 165 insertions(+), 69 deletions(-) diff --git a/data/project-templates/js-node-esm/src/index.js b/data/project-templates/js-node-esm/src/index.js index ef6abfc..c85d9c6 100644 --- a/data/project-templates/js-node-esm/src/index.js +++ b/data/project-templates/js-node-esm/src/index.js @@ -1,56 +1,88 @@ import "dotenv/config"; import { GolemNetwork } from "@golem-sdk/golem-js"; -const order = { - demand: { - workload: { imageTag: "golem/alpine:latest" }, - }, - market: { - // 15 minutes - rentHours: 15 / 60, - pricing: { - model: "linear", - maxStartPrice: 0.5, - maxCpuPerHourPrice: 1.0, - maxEnvPerHourPrice: 0.5, - }, - }, -}; - (async () => { + // Initialize a new GolemNetwork instance, + // you can also pass additional options here like logger or api key const glm = new GolemNetwork(); try { + // Connect to the Golem network using the Yagna node await glm.connect(); - // create a pool that can grow up to 3 rentals at the same time + + // Define the specifications for a market order + const order = { + demand: { + // Specify workload options such as image tag from golem registry + // or other criteria for rented machines + workload: { imageTag: "golem/alpine:latest" }, + }, + market: { + // Specify the rental time (15 minutes) + rentHours: 15 / 60, + pricing: { + // Pricing model set to linear + model: "linear", + // Set the maximum starting price + maxStartPrice: 0.5, + // Set the maximum price per CPU per hour + maxCpuPerHourPrice: 1.0, + // Set the maximum price per environment per hour + maxEnvPerHourPrice: 0.5, + }, + }, + }; + + // Create a pool that can handle up to 3 rentals simultaneously const pool = await glm.manyOf({ poolSize: 3, order, }); + console.log("Starting work on Golem!"); + + console.log("Running three different commands on a pool of three rented machines"); + + // Execute three tasks concurrently, each on a different rented machine in the pool await Promise.allSettled([ pool.withRental(async (rental) => rental .getExeUnit() - .then((exe) => exe.run("echo Hello, Golem from the first machine! 👋")) - .then((res) => console.log(res.stdout)), + .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 😻`)) + .then((res) => console.log(res.stdout)) + .catch((err) => console.error(`Something went wrong:`, err)), ), pool.withRental(async (rental) => rental .getExeUnit() - .then((exe) => exe.run("echo Hello, Golem from the second machine! 👋")) - .then((res) => console.log(res.stdout)), + .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 🤠`)) + .then((res) => console.log(res.stdout)) + .catch((err) => console.error(`Something went wrong:`, err)), ), pool.withRental(async (rental) => rental .getExeUnit() - .then((exe) => exe.run("echo Hello, Golem from the third machine! 👋")) - .then((res) => console.log(res.stdout)), + .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 👻`)) + .then((res) => console.log(res.stdout)) + .catch((err) => console.error(`Something went wrong:`, err)), ), ]); + + console.log("Running a command on a single rented machine"); + + // Acquire a single rental, execute a command, and log the output or an error + const singleRental = await glm.oneOf({ order }); + await singleRental + .getExeUnit() + .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 👽`)) + .then((res) => console.log(res.stdout)) + .catch((err) => console.error(`Something went wrong:`, err)); + } catch (err) { console.error("Something went wrong:", err); } finally { + // Disconnect from the Golem Network. + // This will clear the rental pools and finalize and pay all rentals that were created within the Golem Network await glm.disconnect(); } })().catch(console.error); diff --git a/data/project-templates/js-node/src/index.js b/data/project-templates/js-node/src/index.js index 065e868..063ba77 100644 --- a/data/project-templates/js-node/src/index.js +++ b/data/project-templates/js-node/src/index.js @@ -1,56 +1,88 @@ require("dotenv").config(); const { GolemNetwork } = require("@golem-sdk/golem-js"); -const order = { - demand: { - workload: { imageTag: "golem/alpine:latest" }, - }, - market: { - // 15 minutes - rentHours: 15 / 60, - pricing: { - model: "linear", - maxStartPrice: 0.5, - maxCpuPerHourPrice: 1.0, - maxEnvPerHourPrice: 0.5, - }, - }, -}; - (async () => { + // Initialize a new GolemNetwork instance, + // you can also pass additional options here like logger or api key const glm = new GolemNetwork(); try { + // Connect to the Golem network using the Yagna node await glm.connect(); - // create a pool that can grow up to 3 rentals at the same time + + // Define the specifications for a market order + const order = { + demand: { + // Specify workload options such as image tag from golem registry + // or other criteria for rented machines + workload: { imageTag: "golem/alpine:latest" }, + }, + market: { + // Specify the rental time (15 minutes) + rentHours: 15 / 60, + pricing: { + // Pricing model set to linear + model: "linear", + // Set the maximum starting price + maxStartPrice: 0.5, + // Set the maximum price per CPU per hour + maxCpuPerHourPrice: 1.0, + // Set the maximum price per environment per hour + maxEnvPerHourPrice: 0.5, + }, + }, + }; + + // Create a pool that can handle up to 3 rentals simultaneously const pool = await glm.manyOf({ poolSize: 3, order, }); + console.log("Starting work on Golem!"); + + console.log("Running three different commands on a pool of three rented machines"); + + // Execute three tasks concurrently, each on a different rented machine in the pool await Promise.allSettled([ pool.withRental(async (rental) => rental .getExeUnit() - .then((exe) => exe.run("echo Hello, Golem from the first machine! 👋")) - .then((res) => console.log(res.stdout)), + .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 😻`)) + .then((res) => console.log(res.stdout)) + .catch((err) => console.error(`Something went wrong:`, err)), ), pool.withRental(async (rental) => rental .getExeUnit() - .then((exe) => exe.run("echo Hello, Golem from the second machine! 👋")) - .then((res) => console.log(res.stdout)), + .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 🤠`)) + .then((res) => console.log(res.stdout)) + .catch((err) => console.error(`Something went wrong:`, err)), ), pool.withRental(async (rental) => rental .getExeUnit() - .then((exe) => exe.run("echo Hello, Golem from the third machine! 👋")) - .then((res) => console.log(res.stdout)), + .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 👻`)) + .then((res) => console.log(res.stdout)) + .catch((err) => console.error(`Something went wrong:`, err)), ), ]); + + console.log("Running a command on a single rented machine"); + + // Acquire a single rental, execute a command, and log the output or an error + const singleRental = await glm.oneOf({ order }); + await singleRental + .getExeUnit() + .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 👽`)) + .then((res) => console.log(res.stdout)) + .catch((err) => console.error(`Something went wrong:`, err)); + } catch (err) { console.error("Something went wrong:", err); } finally { + // Disconnect from the Golem Network. + // This will clear the rental pools and finalize and pay all rentals that were created within the Golem Network await glm.disconnect(); } })().catch(console.error); diff --git a/data/project-templates/ts-node/src/index.ts b/data/project-templates/ts-node/src/index.ts index 236a8b7..04da841 100644 --- a/data/project-templates/ts-node/src/index.ts +++ b/data/project-templates/ts-node/src/index.ts @@ -1,56 +1,88 @@ import "dotenv/config"; import { GolemNetwork, MarketOrderSpec } from "@golem-sdk/golem-js"; -const order: MarketOrderSpec = { - demand: { - workload: { imageTag: "golem/alpine:latest" }, - }, - market: { - // 15 minutes - rentHours: 15 / 60, - pricing: { - model: "linear", - maxStartPrice: 0.5, - maxCpuPerHourPrice: 1.0, - maxEnvPerHourPrice: 0.5, - }, - }, -}; - (async () => { + // Initialize a new GolemNetwork instance, + // you can also pass additional options here like logger or api key const glm = new GolemNetwork(); try { + // Connect to the Golem network using the Yagna node await glm.connect(); - // create a pool that can grow up to 3 rentals at the same time + + // Define the specifications for a market order + const order: MarketOrderSpec = { + demand: { + // Specify workload options such as image tag from golem registry + // or other criteria for rented machines + workload: { imageTag: "golem/alpine:latest" }, + }, + market: { + // Specify the rental time (15 minutes) + rentHours: 15 / 60, + pricing: { + // Pricing model set to linear + model: "linear", + // Set the maximum starting price + maxStartPrice: 0.5, + // Set the maximum price per CPU per hour + maxCpuPerHourPrice: 1.0, + // Set the maximum price per environment per hour + maxEnvPerHourPrice: 0.5, + }, + }, + }; + + // Create a pool that can handle up to 3 rentals simultaneously const pool = await glm.manyOf({ poolSize: 3, order, }); + console.log("Starting work on Golem!"); + + console.log("Running three different commands on a pool of three rented machines"); + + // Execute three tasks concurrently, each on a different rented machine in the pool await Promise.allSettled([ pool.withRental(async (rental) => rental .getExeUnit() - .then((exe) => exe.run("echo Hello, Golem from the first machine! 👋")) - .then((res) => console.log(res.stdout)), + .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 😻`)) + .then((res) => console.log(res.stdout)) + .catch((err) => console.error(`Something went wrong:`, err)), ), pool.withRental(async (rental) => rental .getExeUnit() - .then((exe) => exe.run("echo Hello, Golem from the second machine! 👋")) - .then((res) => console.log(res.stdout)), + .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 🤠`)) + .then((res) => console.log(res.stdout)) + .catch((err) => console.error(`Something went wrong:`, err)), ), pool.withRental(async (rental) => rental .getExeUnit() - .then((exe) => exe.run("echo Hello, Golem from the third machine! 👋")) - .then((res) => console.log(res.stdout)), + .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 👻`)) + .then((res) => console.log(res.stdout)) + .catch((err) => console.error(`Something went wrong:`, err)), ), ]); + + console.log("Running a command on a single rented machine"); + + // Acquire a single rental, execute a command, and log the output or an error + const singleRental = await glm.oneOf({ order }); + await singleRental + .getExeUnit() + .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 👽`)) + .then((res) => console.log(res.stdout)) + .catch((err) => console.error(`Something went wrong:`, err)); + } catch (err) { console.error("Something went wrong:", err); } finally { + // Disconnect from the Golem Network. + // This will clear the rental pools and finalize and pay all rentals that were created within the Golem Network await glm.disconnect(); } })().catch(console.error); From 6f20b9db74e47774f3bc8820aa43c251e52db370 Mon Sep 17 00:00:00 2001 From: Marcin Gordel Date: Wed, 28 Aug 2024 12:03:55 +0200 Subject: [PATCH 2/3] chore: added new template for ts-node --- .../ts-node-esm/src/index.ts | 78 +++++++++++++------ 1 file changed, 55 insertions(+), 23 deletions(-) diff --git a/data/project-templates/ts-node-esm/src/index.ts b/data/project-templates/ts-node-esm/src/index.ts index 236a8b7..04da841 100644 --- a/data/project-templates/ts-node-esm/src/index.ts +++ b/data/project-templates/ts-node-esm/src/index.ts @@ -1,56 +1,88 @@ import "dotenv/config"; import { GolemNetwork, MarketOrderSpec } from "@golem-sdk/golem-js"; -const order: MarketOrderSpec = { - demand: { - workload: { imageTag: "golem/alpine:latest" }, - }, - market: { - // 15 minutes - rentHours: 15 / 60, - pricing: { - model: "linear", - maxStartPrice: 0.5, - maxCpuPerHourPrice: 1.0, - maxEnvPerHourPrice: 0.5, - }, - }, -}; - (async () => { + // Initialize a new GolemNetwork instance, + // you can also pass additional options here like logger or api key const glm = new GolemNetwork(); try { + // Connect to the Golem network using the Yagna node await glm.connect(); - // create a pool that can grow up to 3 rentals at the same time + + // Define the specifications for a market order + const order: MarketOrderSpec = { + demand: { + // Specify workload options such as image tag from golem registry + // or other criteria for rented machines + workload: { imageTag: "golem/alpine:latest" }, + }, + market: { + // Specify the rental time (15 minutes) + rentHours: 15 / 60, + pricing: { + // Pricing model set to linear + model: "linear", + // Set the maximum starting price + maxStartPrice: 0.5, + // Set the maximum price per CPU per hour + maxCpuPerHourPrice: 1.0, + // Set the maximum price per environment per hour + maxEnvPerHourPrice: 0.5, + }, + }, + }; + + // Create a pool that can handle up to 3 rentals simultaneously const pool = await glm.manyOf({ poolSize: 3, order, }); + console.log("Starting work on Golem!"); + + console.log("Running three different commands on a pool of three rented machines"); + + // Execute three tasks concurrently, each on a different rented machine in the pool await Promise.allSettled([ pool.withRental(async (rental) => rental .getExeUnit() - .then((exe) => exe.run("echo Hello, Golem from the first machine! 👋")) - .then((res) => console.log(res.stdout)), + .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 😻`)) + .then((res) => console.log(res.stdout)) + .catch((err) => console.error(`Something went wrong:`, err)), ), pool.withRental(async (rental) => rental .getExeUnit() - .then((exe) => exe.run("echo Hello, Golem from the second machine! 👋")) - .then((res) => console.log(res.stdout)), + .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 🤠`)) + .then((res) => console.log(res.stdout)) + .catch((err) => console.error(`Something went wrong:`, err)), ), pool.withRental(async (rental) => rental .getExeUnit() - .then((exe) => exe.run("echo Hello, Golem from the third machine! 👋")) - .then((res) => console.log(res.stdout)), + .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 👻`)) + .then((res) => console.log(res.stdout)) + .catch((err) => console.error(`Something went wrong:`, err)), ), ]); + + console.log("Running a command on a single rented machine"); + + // Acquire a single rental, execute a command, and log the output or an error + const singleRental = await glm.oneOf({ order }); + await singleRental + .getExeUnit() + .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 👽`)) + .then((res) => console.log(res.stdout)) + .catch((err) => console.error(`Something went wrong:`, err)); + } catch (err) { console.error("Something went wrong:", err); } finally { + // Disconnect from the Golem Network. + // This will clear the rental pools and finalize and pay all rentals that were created within the Golem Network await glm.disconnect(); } })().catch(console.error); From 82e008d81eb456b1c3dad412959551e003df0989 Mon Sep 17 00:00:00 2001 From: Marcin Gordel Date: Wed, 28 Aug 2024 12:07:27 +0200 Subject: [PATCH 3/3] chore: fixed formatting by linter --- data/project-templates/js-node-esm/src/index.js | 1 - data/project-templates/js-node/src/index.js | 1 - data/project-templates/ts-node-esm/src/index.ts | 1 - data/project-templates/ts-node/src/index.ts | 1 - 4 files changed, 4 deletions(-) diff --git a/data/project-templates/js-node-esm/src/index.js b/data/project-templates/js-node-esm/src/index.js index c85d9c6..a79f6b5 100644 --- a/data/project-templates/js-node-esm/src/index.js +++ b/data/project-templates/js-node-esm/src/index.js @@ -77,7 +77,6 @@ import { GolemNetwork } from "@golem-sdk/golem-js"; .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 👽`)) .then((res) => console.log(res.stdout)) .catch((err) => console.error(`Something went wrong:`, err)); - } catch (err) { console.error("Something went wrong:", err); } finally { diff --git a/data/project-templates/js-node/src/index.js b/data/project-templates/js-node/src/index.js index 063ba77..142f0db 100644 --- a/data/project-templates/js-node/src/index.js +++ b/data/project-templates/js-node/src/index.js @@ -77,7 +77,6 @@ const { GolemNetwork } = require("@golem-sdk/golem-js"); .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 👽`)) .then((res) => console.log(res.stdout)) .catch((err) => console.error(`Something went wrong:`, err)); - } catch (err) { console.error("Something went wrong:", err); } finally { diff --git a/data/project-templates/ts-node-esm/src/index.ts b/data/project-templates/ts-node-esm/src/index.ts index 04da841..88e46e7 100644 --- a/data/project-templates/ts-node-esm/src/index.ts +++ b/data/project-templates/ts-node-esm/src/index.ts @@ -77,7 +77,6 @@ import { GolemNetwork, MarketOrderSpec } from "@golem-sdk/golem-js"; .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 👽`)) .then((res) => console.log(res.stdout)) .catch((err) => console.error(`Something went wrong:`, err)); - } catch (err) { console.error("Something went wrong:", err); } finally { diff --git a/data/project-templates/ts-node/src/index.ts b/data/project-templates/ts-node/src/index.ts index 04da841..88e46e7 100644 --- a/data/project-templates/ts-node/src/index.ts +++ b/data/project-templates/ts-node/src/index.ts @@ -77,7 +77,6 @@ import { GolemNetwork, MarketOrderSpec } from "@golem-sdk/golem-js"; .then((exe) => exe.run(`echo Hello Golem from provider ${exe.provider.name} 👽`)) .then((res) => console.log(res.stdout)) .catch((err) => console.error(`Something went wrong:`, err)); - } catch (err) { console.error("Something went wrong:", err); } finally {