-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #235 from golemfactory/feature/JST-1059/one-of
feat: added example of using `oneOf` to nodejs template
- Loading branch information
Showing
4 changed files
with
216 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,87 @@ | ||
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,87 @@ | ||
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,87 @@ | ||
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); |
Oops, something went wrong.