-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #940 from golemfactory/feature/JST-863/transfer-ex…
…ample File transfer example
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters