From b17d56bccb2f5148f594cf05bed450014256437e Mon Sep 17 00:00:00 2001 From: Marcin Gordel Date: Tue, 9 Jul 2024 10:05:49 +0200 Subject: [PATCH 1/3] docs: added example for quickstart --- .../quickstarts/quickstart/requestor.mjs | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 examples/docs-examples/quickstarts/quickstart/requestor.mjs diff --git a/examples/docs-examples/quickstarts/quickstart/requestor.mjs b/examples/docs-examples/quickstarts/quickstart/requestor.mjs new file mode 100644 index 000000000..e4e37d046 --- /dev/null +++ b/examples/docs-examples/quickstarts/quickstart/requestor.mjs @@ -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 + concurrency: { 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} compute 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); From c3a4bf8088562003bce39b732e5c4cb2c3c03a1e Mon Sep 17 00:00:00 2001 From: Marcin Gordel Date: Tue, 9 Jul 2024 16:27:48 +0200 Subject: [PATCH 2/3] Update examples/docs-examples/quickstarts/quickstart/requestor.mjs Co-authored-by: jalas167 <42438166+jalas167@users.noreply.github.com> --- examples/docs-examples/quickstarts/quickstart/requestor.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/docs-examples/quickstarts/quickstart/requestor.mjs b/examples/docs-examples/quickstarts/quickstart/requestor.mjs index e4e37d046..f25bfbde4 100644 --- a/examples/docs-examples/quickstarts/quickstart/requestor.mjs +++ b/examples/docs-examples/quickstarts/quickstart/requestor.mjs @@ -33,7 +33,7 @@ const order = { 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 - concurrency: { min: 1, max: 3 }, + poolSize: { min: 1, max: 3 }, order, }); // I have 5 parts of the task to perform in parallel From 4e8bd4d3b9ab9e18b237a33783e4e623036ed3d7 Mon Sep 17 00:00:00 2001 From: Marcin Gordel Date: Tue, 9 Jul 2024 16:43:59 +0200 Subject: [PATCH 3/3] docs: fixed typo in quickstart example --- examples/docs-examples/quickstarts/quickstart/requestor.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/docs-examples/quickstarts/quickstart/requestor.mjs b/examples/docs-examples/quickstarts/quickstart/requestor.mjs index f25bfbde4..866dfb21f 100644 --- a/examples/docs-examples/quickstarts/quickstart/requestor.mjs +++ b/examples/docs-examples/quickstarts/quickstart/requestor.mjs @@ -25,7 +25,7 @@ const order = { logger: pinoPrettyLogger({ level: "info", }), - api: { key: "try_golem" }, + // api: { key: "try_golem" }, }); try { @@ -45,7 +45,7 @@ const order = { .getExeUnit() .then((exe) => exe.run( - `echo "Part #${i} compute on provider ${exe.provider.name} with CPU:" && cat /proc/cpuinfo | grep 'model name'`, + `echo "Part #${i} computed on provider ${exe.provider.name} with CPU:" && cat /proc/cpuinfo | grep 'model name'`, ), ), ),