Skip to content

Commit

Permalink
Merge pull request #1023 from golemfactory/mgordel/new-quickstart
Browse files Browse the repository at this point in the history
docs: added example for quick-start
  • Loading branch information
mgordel authored Jul 9, 2024
2 parents db0de16 + 4e8bd4d commit 599e7e9
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions examples/docs-examples/quickstarts/quickstart/requestor.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* This example demonstrates how easily lease multiple machines at once.
*/

import { GolemNetwork } from "@golem-sdk/golem-js";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

const order = {
demand: {
workload: { imageTag: "golem/alpine:latest" },
},
market: {
rentHours: 0.5,
pricing: {
model: "linear",
maxStartPrice: 0.5,
maxCpuPerHourPrice: 1.0,
maxEnvPerHourPrice: 0.5,
},
},
};

(async () => {
const glm = new GolemNetwork({
logger: pinoPrettyLogger({
level: "info",
}),
// api: { key: "try_golem" },
});

try {
await glm.connect();
const pool = await glm.manyOf({
// I want to have a minimum of one machine in the pool,
// but only a maximum of 3 machines can work at the same time
poolSize: { min: 1, max: 3 },
order,
});
// I have 5 parts of the task to perform in parallel
const data = [...Array(5).keys()];
const results = await Promise.allSettled(
data.map((i) =>
pool.withRental((rental) =>
rental
.getExeUnit()
.then((exe) =>
exe.run(
`echo "Part #${i} computed on provider ${exe.provider.name} with CPU:" && cat /proc/cpuinfo | grep 'model name'`,
),
),
),
),
);
results.forEach((result) => {
if (result.status === "fulfilled") {
console.log("Success:", result.value.stdout);
} else {
console.log("Failure:", result.reason);
}
});
} catch (err) {
console.error("Failed to run the example", err);
} finally {
await glm.disconnect();
}
})().catch(console.error);

0 comments on commit 599e7e9

Please sign in to comment.