-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathproposal-selector.ts
55 lines (50 loc) · 1.51 KB
/
proposal-selector.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
import { MarketOrderSpec, GolemNetwork, OfferProposal } from "@golem-sdk/golem-js";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";
/**
* Example demonstrating how to write a selector which choose the best provider based on scores provided as object: [providerName]: score
* A higher score rewards the provider.
*/
const scores = {
"provider-1": 100,
"golem-provider": 50,
"super-provider": 25,
};
const bestProviderSelector = (scores: { [providerName: string]: number }) => (proposals: OfferProposal[]) => {
proposals.sort((a, b) => ((scores?.[a.provider.name] || 0) >= (scores?.[b.provider.name] || 0) ? -1 : 1));
return proposals[0];
};
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,
},
offerProposalSelector: bestProviderSelector(scores),
},
};
(async () => {
const glm = new GolemNetwork({
logger: pinoPrettyLogger({
level: "info",
}),
});
try {
await glm.connect();
const rental = await glm.oneOf({ order });
await rental
.getExeUnit()
.then((exe) => exe.run(`echo [provider:${exe.provider.name}] Hello, Golem! 👋`))
.then((res) => console.log(res.stdout));
await rental.stopAndFinalize();
} catch (err) {
console.error("Failed to run the example", err);
} finally {
await glm.disconnect();
}
})().catch(console.error);