Skip to content

Commit

Permalink
Merge pull request #940 from golemfactory/feature/JST-863/transfer-ex…
Browse files Browse the repository at this point in the history
…ample

File transfer example
  • Loading branch information
mgordel authored May 28, 2024
2 parents 9573d09 + 2f54a26 commit 4e081f2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
67 changes: 67 additions & 0 deletions examples/basic/transfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { MarketOrderSpec, GolemNetwork } from "@golem-sdk/golem-js";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";
import { appendFile, readFile, unlink } from "fs/promises";

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

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

try {
await glm.connect();
const pool = await glm.manyOf({
concurrency: 2,
order,
});
const lease1 = await pool.acquire();
const lease2 = await pool.acquire();

const exe1 = await lease1.getExeUnit();
const exe2 = await lease2.getExeUnit();

await exe1
.beginBatch()
.run(`echo "Message from provider ${exe1.provider.name}. Hello 😻" >> /golem/work/message.txt`)
.downloadFile("/golem/work/message.txt", "./message.txt")
.end();

await appendFile("./message.txt", "Message from requestor. Hello 🤠\n");

await exe2
.beginBatch()
.uploadFile("./message.txt", "/golem/work/message.txt")
.run(`echo "Message from provider ${exe2.provider.name}. Hello 👻" >> /golem/work/message.txt`)
.downloadFile("/golem/work/message.txt", "./results.txt")
.end();

console.log("File content: ");
console.log(await readFile("./results.txt", { encoding: "utf-8" }));

await lease1.finalize();
await lease2.finalize();
} catch (err) {
console.error("Failed to run the example", err);
} finally {
await glm.disconnect();
await unlink("./message.txt");
await unlink("./results.txt");
}
})().catch(console.error);
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"basic-one-of": "tsx basic/one-of.ts",
"basic-many-of": "tsx basic/many-of.ts",
"basic-vpn": "tsx basic/vpn.ts",
"basic-transfer": "tsx basic/transfer.ts",
"advanced-hello-world": "tsx advanced/hello-world.ts",
"advanced-manual-pools": "tsx advanced/manual-pools.ts",
"local-image": "tsx advanced/local-image/serveLocalGvmi.ts",
Expand Down
1 change: 1 addition & 0 deletions tests/examples/examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{ "cmd": "tsx", "path": "examples/basic/many-of.ts" },
{ "cmd": "tsx", "path": "examples/basic/one-of.ts" },
{ "cmd": "tsx", "path": "examples/basic/vpn.ts" },
{ "cmd": "tsx", "path": "examples/basic/transfer.ts" },
{ "cmd": "tsx", "path": "examples/advanced/hello-world.ts" },
{ "cmd": "tsx", "path": "examples/advanced/manual-pools.ts" },
{ "cmd": "tsx", "path": "examples/experimental/deployment/new-api.ts" }
Expand Down

0 comments on commit 4e081f2

Please sign in to comment.