Skip to content

Commit

Permalink
Merge pull request #931 from golemfactory/demo-examples
Browse files Browse the repository at this point in the history
Update examples
  • Loading branch information
SewerynKras authored May 21, 2024
2 parents 2ff1bfc + 516fe64 commit 75b37f8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 71 deletions.
58 changes: 28 additions & 30 deletions examples/basic/hello-world-v2.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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);
30 changes: 19 additions & 11 deletions examples/deployment/new-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
63 changes: 33 additions & 30 deletions examples/pool/hello-world.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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();
Expand Down Expand Up @@ -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)),
]);

Expand All @@ -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);

0 comments on commit 75b37f8

Please sign in to comment.