-
Notifications
You must be signed in to change notification settings - Fork 19
/
manual-pools.ts
103 lines (85 loc) · 2.67 KB
/
manual-pools.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { Allocation, DraftOfferProposalPool, GolemNetwork } from "@golem-sdk/golem-js";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";
const RENTAL_DURATION_HOURS = 0.25;
const ALLOCATION_DURATION_HOURS = RENTAL_DURATION_HOURS + 0.25;
const demandOptions = {
demand: {
workload: {
imageTag: "golem/alpine:latest",
minCpuCores: 1,
minMemGib: 1,
minStorageGib: 2,
},
},
market: {
rentHours: RENTAL_DURATION_HOURS,
pricing: {
model: "linear",
maxStartPrice: 1,
maxCpuPerHourPrice: 1,
maxEnvPerHourPrice: 1,
},
},
} as const;
(async () => {
const logger = pinoPrettyLogger({
level: "info",
});
const glm = new GolemNetwork({
logger,
});
console.assert(
ALLOCATION_DURATION_HOURS > RENTAL_DURATION_HOURS,
"Always create allocations that will live longer than the planned rental duration",
);
let allocation: Allocation | undefined;
try {
await glm.connect();
allocation = await glm.payment.createAllocation({ budget: 1, expirationSec: ALLOCATION_DURATION_HOURS * 60 * 60 });
const proposalPool = new DraftOfferProposalPool({ minCount: 1 });
const demandSpecification = await glm.market.buildDemandDetails(
demandOptions.demand,
demandOptions.market,
allocation,
);
const draftProposal$ = glm.market.collectDraftOfferProposals({
demandSpecification,
pricing: demandOptions.market.pricing,
});
const proposalSubscription = proposalPool.readFrom(draftProposal$);
/** How many providers you plan to engage simultaneously */
const PARALLELISM = 2;
const depModules = {
market: glm.market,
activity: glm.activity,
payment: glm.payment,
rental: glm.rental,
};
const pool = depModules.rental.createResourceRentalPool(proposalPool, allocation, {
poolSize: { max: PARALLELISM },
});
const rental1 = await pool.acquire();
const rental2 = await pool.acquire();
await Promise.allSettled([
rental1
.getExeUnit()
.then((exe) => exe.run("echo Hello from first activity 👋"))
.then((result) => console.log(result.stdout)),
rental2
.getExeUnit()
.then((exe) => exe.run("echo Hello from second activity 👋"))
.then((result) => console.log(result.stdout)),
]);
await pool.release(rental1);
await pool.release(rental2);
proposalSubscription.unsubscribe();
await pool.drainAndClear();
} catch (err) {
console.error("Pool execution failed:", err);
} finally {
await glm.disconnect();
if (allocation) {
await glm.payment.releaseAllocation(allocation);
}
}
})().catch(console.error);