From 516fe64eae1462737c8082e2278a97aa7c351e47 Mon Sep 17 00:00:00 2001 From: Seweryn Kras Date: Mon, 20 May 2024 16:08:07 +0200 Subject: [PATCH] docs: update examples --- examples/basic/hello-world-v2.ts | 58 ++++++++++++++--------------- examples/deployment/new-api.ts | 30 +++++++++------ examples/pool/hello-world.ts | 63 +++++++++++++++++--------------- 3 files changed, 80 insertions(+), 71 deletions(-) diff --git a/examples/basic/hello-world-v2.ts b/examples/basic/hello-world-v2.ts index cd27adf68..b00f79c68 100644 --- a/examples/basic/hello-world-v2.ts +++ b/examples/basic/hello-world-v2.ts @@ -1,6 +1,26 @@ -import { GolemNetwork } from "@golem-sdk/golem-js"; +import { DemandSpec, GolemNetwork } from "@golem-sdk/golem-js"; import { pinoPrettyLogger } from "@golem-sdk/pino-logger"; +const demandOptions: DemandSpec = { + demand: { + activity: { imageTag: "golem/alpine:latest" }, + }, + market: { + maxAgreements: 1, + rentHours: 0.5, + pricing: { + model: "linear", + maxStartPrice: 0.5, + maxCpuPerHourPrice: 1.0, + maxEnvPerHourPrice: 0.5, + }, + }, + payment: { + driver: "erc20", + network: "holesky", + }, +}; + (async () => { const glm = new GolemNetwork({ logger: pinoPrettyLogger({ @@ -10,37 +30,15 @@ import { pinoPrettyLogger } from "@golem-sdk/pino-logger"; try { await glm.connect(); - - const lease = await glm.oneOf({ - demand: { - activity: { imageTag: "golem/alpine:latest" }, - }, - market: { - maxAgreements: 1, - rentHours: 0.5, - pricing: { - model: "linear", - maxStartPrice: 0.5, - maxCpuPerHourPrice: 1.0, - maxEnvPerHourPrice: 0.5, - }, - }, - payment: { - driver: "erc20", - network: "holesky", - }, - }); - - const exe = await lease.getExeUnit(); - - const result = await exe.run("echo 'Hello World!'"); - console.log(result.stdout); - - // Wait for the lease to be fully terminated and settled + const lease = await glm.oneOf(demandOptions); + await lease + .getExeUnit() + .then((exe) => exe.run("echo Hello, Golem! 👋")) + .then((res) => console.log(res.stdout)); await lease.finalize(); } catch (err) { console.error("Failed to run the example", err); + } finally { + await glm.disconnect(); } - - await glm.disconnect(); })().catch(console.error); diff --git a/examples/deployment/new-api.ts b/examples/deployment/new-api.ts index b41b41119..80c6705b3 100644 --- a/examples/deployment/new-api.ts +++ b/examples/deployment/new-api.ts @@ -4,7 +4,7 @@ import { pinoPrettyLogger } from "@golem-sdk/pino-logger"; async function main() { const golem = new GolemNetwork({ logger: pinoPrettyLogger({ - level: "debug", + level: "info", }), market: { maxAgreements: 1, @@ -88,18 +88,26 @@ async function main() { const dbPool = deployment.getLeaseProcessPool("db"); // Get an instance out of the pool for use - const app = await appPool.acquire(); - const db = await dbPool.acquire(); + const appReplica1 = await appPool.acquire(); + const appReplica2 = await appPool.acquire(); + const dbReplica = await dbPool.acquire(); - // Run a command on the app VM instance - const appResult = await app.getExeUnit().then((exe) => exe.run("node -v")); - console.log(appResult.stdout); - await appPool.release(app); + await Promise.allSettled([ + appReplica1 + .getExeUnit() + .then((exe) => exe.run("echo Running some code on app replica 1 🏃")) + .then((res) => console.log(res.stdout)), + appReplica2 + .getExeUnit() + .then((exe) => exe.run("echo Running some code on app replica 2 🏃")) + .then((res) => console.log(res.stdout)), + dbReplica + .getExeUnit() + .then((exe) => exe.run("echo Running some code on db replica 🏃")) + .then((res) => console.log(res.stdout)), + ]); - // Run a command on the db VM instance - const dbResult = await db.getExeUnit().then((exe) => exe.run("echo Hello World")); - console.log(dbResult.stdout); - await dbPool.release(db); + await Promise.allSettled([appPool.destroy(appReplica1), appPool.destroy(appReplica2), dbPool.destroy(dbReplica)]); // Stop the deployment cleanly await deployment.stop(); diff --git a/examples/pool/hello-world.ts b/examples/pool/hello-world.ts index 71322a0f0..aa41cd9ed 100644 --- a/examples/pool/hello-world.ts +++ b/examples/pool/hello-world.ts @@ -1,6 +1,32 @@ -import { DraftOfferProposalPool, GolemNetwork } from "@golem-sdk/golem-js"; +import { Allocation, DraftOfferProposalPool, GolemNetwork } from "@golem-sdk/golem-js"; import { pinoPrettyLogger } from "@golem-sdk/pino-logger"; +const RENT_HOURS = 0.25; + +const demandOptions = { + demand: { + activity: { + imageTag: "golem/alpine:latest", + minCpuCores: 1, + minMemGib: 1, + minStorageGib: 2, + }, + }, + market: { + rentHours: RENT_HOURS, + pricing: { + model: "linear", + maxStartPrice: 1, + maxCpuPerHourPrice: 1, + maxEnvPerHourPrice: 1, + }, + withProviders: ["0x123123"], + withoutProviders: ["0x123123"], + withOperators: ["0x123123"], + withoutOperators: ["0x123123"], + }, +} as const; + (async () => { const logger = pinoPrettyLogger({ level: "info", @@ -15,37 +41,12 @@ import { pinoPrettyLogger } from "@golem-sdk/pino-logger"; }, }, }); - let allocation; + let allocation: Allocation | undefined; try { - const RENT_HOURS = 0.25; - await glm.connect(); allocation = await glm.payment.createAllocation({ budget: 1, expirationSec: RENT_HOURS * 60 * 60 }); - const demandOptions = { - demand: { - activity: { - imageTag: "golem/alpine:latest", - minCpuCores: 1, - minMemGib: 1, - minStorageGib: 2, - }, - }, - market: { - rentHours: RENT_HOURS, - pricing: { - model: "linear", - maxStartPrice: 1, - maxCpuPerHourPrice: 1, - maxEnvPerHourPrice: 1, - }, - withProviders: ["0x123123"], - withoutProviders: ["0x123123"], - withOperators: ["0x123123"], - withoutOperators: ["0x123123"], - }, - }; const proposalPool = new DraftOfferProposalPool({ minCount: 1 }); const payerDetails = await glm.payment.getPayerDetails(); @@ -76,11 +77,11 @@ import { pinoPrettyLogger } from "@golem-sdk/pino-logger"; await Promise.allSettled([ lease .getExeUnit() - .then((exe) => exe.run("echo Hello World from first activity")) + .then((exe) => exe.run("echo Hello from first activity 👋")) .then((result) => console.log(result.stdout)), lease2 .getExeUnit() - .then((exe) => exe.run("echo Hello Golem from second activity")) + .then((exe) => exe.run("echo Hello from second activity 👋")) .then((result) => console.log(result.stdout)), ]); @@ -93,6 +94,8 @@ import { pinoPrettyLogger } from "@golem-sdk/pino-logger"; console.error("Pool execution failed:", err); } finally { await glm.disconnect(); - allocation?.release(); + if (allocation) { + await glm.payment.releaseAllocation(allocation); + } } })().catch(console.error);