-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathrun-and-stream.ts
59 lines (53 loc) · 1.54 KB
/
run-and-stream.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { MarketOrderSpec, GolemNetwork } from "@golem-sdk/golem-js";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";
/**
* Example demonstrating the execution of a command on a provider which may take a long time
* and returns the results from the execution during the command as a stream
*/
const order: MarketOrderSpec = {
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",
}),
});
try {
await glm.connect();
const rental = await glm.oneOf({ order });
const exe = await rental.getExeUnit();
const remoteProcess = await exe.runAndStream(
`
sleep 1
echo -n 'Hello from stdout' >&1
echo -n 'Hello from stderr' >&2
sleep 1
echo -n 'Hello from stdout again' >&1
echo -n 'Hello from stderr again' >&2
sleep 1
echo -n 'Hello from stdout yet again' >&1
echo -n 'Hello from stderr yet again' >&2
`,
);
remoteProcess.stdout.subscribe((data) => console.log("stdout>", data));
remoteProcess.stderr.subscribe((data) => console.error("stderr>", data));
await remoteProcess.waitForExit();
await rental.stopAndFinalize();
} catch (err) {
console.error("Failed to run the example", err);
} finally {
await glm.disconnect();
}
})().catch(console.error);