Skip to content

Commit

Permalink
Merge pull request #909 from golemfactory/grisha87/clean-arch
Browse files Browse the repository at this point in the history
Refactoring toward clean architecture
  • Loading branch information
grisha87 authored May 6, 2024
2 parents 7307f97 + 465ba8d commit 605311b
Show file tree
Hide file tree
Showing 113 changed files with 4,992 additions and 3,848 deletions.
47 changes: 47 additions & 0 deletions examples/basic/hello-world-v2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { GolemNetwork } from "@golem-sdk/golem-js";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

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

try {
await glm.connect();

const lease = await glm.oneOf({
demand: {
imageTag: "golem/alpine:latest",
minCpuCores: 4,
minMemGib: 8,
minStorageGib: 16,
},
market: {
rentHours: 12,
pricing: {
maxStartPrice: 1,
maxCpuPerHourPrice: 1,
maxEnvPerHourPrice: 1,
},
},
payment: {
driver: "erc20",
network: "holesky",
},
});

const exe = await lease.getExeUnit();

const result = await exe.run("echo 'Hello World!'");
console.log(result.stdout);

// Wait for the lease to be fully terminated and settled
await lease.finalize();
} catch (err) {
console.error("Failed to run the example", err);
}

await glm.disconnect();
})().catch(console.error);
94 changes: 48 additions & 46 deletions examples/basic/hello-world.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import {
YagnaApi,
MarketModuleImpl,
ActivityModuleImpl,
PaymentModuleImpl,
DraftOfferProposalPool,
WorkContext,
Package,
Allocation,
} from "@golem-sdk/golem-js";
import { DraftOfferProposalPool, GolemNetwork } from "@golem-sdk/golem-js";

import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

(async () => {
const yagnaApi = new YagnaApi();
const logger = pinoPrettyLogger({
level: "debug",
});

const glm = new GolemNetwork({
logger,
});

try {
await yagnaApi.connect();
const modules = {
market: new MarketModuleImpl(yagnaApi),
activity: new ActivityModuleImpl(yagnaApi),
payment: new PaymentModuleImpl(yagnaApi),
};
const demandOptions = {
await glm.connect();

const demand = {
demand: {
image: "golem/alpine:latest",
imageTag: "golem/alpine:latest",
resources: {
minCpu: 4,
minMemGib: 8,
Expand All @@ -35,42 +30,49 @@ import {
maxCpuPerHourPrice: 1,
maxEnvPerHourPrice: 1,
},
withProviders: ["0x123123"],
withoutProviders: ["0x123123"],
withOperators: ["0x123123"],
withoutOperators: ["0x123123"],
},
};

const proposalPool = new DraftOfferProposalPool();
const workload = Package.create({
imageTag: demandOptions.demand.image,
const proposalPool = new DraftOfferProposalPool({
logger,
});
const allocation = await Allocation.create(yagnaApi, {
account: {
address: (await yagnaApi.identity.getIdentity()).identity,
platform: "erc20-holesky-tglm",
},
budget: 1,

const payerDetails = await glm.payment.getPayerDetails();
const demandSpecification = await glm.market.buildDemand(demand.demand, payerDetails);
const proposal$ = glm.market.startCollectingProposals({
demandSpecification,
bufferSize: 15,
});
const demandOffer = await modules.market.buildDemand(workload, allocation, {});
const proposalSubscription = modules.market
.startCollectingProposals({
demandOffer,
paymentPlatform: "erc20-holesky-tglm",
})
.subscribe((proposalsBatch) => proposalsBatch.forEach((proposal) => proposalPool.add(proposal)));
const proposalSubscription = proposalPool.readFrom(proposal$);
const draftProposal = await proposalPool.acquire();
const agreement = await modules.market.proposeAgreement(modules.payment, draftProposal);
const activity = await modules.activity.createActivity(agreement);
const ctx = new WorkContext(activity, {});
await ctx.before();
const result = await ctx.run("echo Hello World");
console.log(result.stdout);

const agreement = await glm.market.proposeAgreement(draftProposal);

const allocation = await glm.payment.createAllocation({ budget: 1 });
const lease = await glm.market.createLease(agreement, allocation);
const activity = await glm.activity.createActivity(agreement);

// We managed to create the activity, no need to look for more agreement candidates
proposalSubscription.unsubscribe();

// Access your work context to perform operations
const ctx = await glm.activity.createWorkContext(activity);

// Perform your work
const result = await ctx.run("echo Hello World");
console.log("Result=", result.stdout);

// Start clean shutdown procedure for business components
await glm.activity.destroyActivity(activity);
await glm.market.terminateAgreement(agreement);
await proposalPool.remove(draftProposal);

// This will keep the script waiting for payments etc
await lease.finalize();
} catch (err) {
console.error("Failed to run example on Golem", err);
} finally {
await yagnaApi.disconnect();
// Clean shutdown of infrastructure components
await glm.disconnect();
}
})().catch(console.error);
29 changes: 12 additions & 17 deletions examples/deployment/new-api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { GolemNetwork } from "@golem-sdk/golem-js";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

async function main() {
const golem = new GolemNetwork({
api: {
url: process.env.YAGNA_API_URL || "http://127.0.0.1:7465",
key: process.env.YAGNA_APPKEY || "try-golem",
},
payment: {},
logger: pinoPrettyLogger({
level: "debug",
}),
market: {},
dataTransferProtocol: "gftp",
});
Expand All @@ -22,15 +21,13 @@ async function main() {
})
.createActivityPool("app", {
demand: {
image: "golem/node:latest",
imageTag: "golem/node:latest",
// image: "golem/node:20",
// image: "http://golem.io/node:20",
// imageHash: "0x30984084039480493840",
resources: {
minCpu: 4,
minMemGib: 8,
minStorageGib: 16,
},
minCpuCores: 4,
minMemGib: 8,
minStorageGib: 16,
},
market: {
rentHours: 12,
Expand All @@ -51,12 +48,10 @@ async function main() {
})
.createActivityPool("db", {
demand: {
image: "golem/alpine:latest",
resources: {
minCpu: 2,
minMemGib: 16,
minStorageGib: 4,
},
imageTag: "golem/alpine:latest",
minCpuCores: 2,
minMemGib: 16,
minStorageGib: 4,
},
market: {
rentHours: 12 /* REQUIRED */,
Expand Down
13 changes: 7 additions & 6 deletions examples/experimental/express/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ const port = 3000;

app.use(express.text());

const golemClient = new JobManager({
yagna: {
apiKey: "try_golem",
},
});
const golemClient = new JobManager();

await golemClient
.init()
Expand All @@ -28,9 +24,14 @@ app.post("/tts", async (req, res) => {
return;
}
const job = golemClient.createJob({
package: {
demand: {
imageTag: "severyn/espeak:latest",
},
market: {}, // TODO: This should be optional
payment: {
driver: "erc20",
network: "holesky",
},
});

job.events.on("created", () => {
Expand Down
9 changes: 7 additions & 2 deletions examples/experimental/job/cancel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ async function main() {
await golem.init();

const job = golem.createJob<string>({
package: {
imageTag: "golem/alpine:latest",
demand: {
imageTag: "severyn/espeak:latest",
},
market: {}, // TODO: This should be optional
payment: {
driver: "erc20",
network: "holesky",
},
});

Expand Down
9 changes: 7 additions & 2 deletions examples/experimental/job/getJobById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ const golem = new JobManager({

function startJob() {
const job = golem.createJob<string>({
package: {
imageTag: "golem/alpine:latest",
demand: {
imageTag: "severyn/espeak:latest",
},
market: {}, // TODO: This should be optional
payment: {
driver: "erc20",
network: "holesky",
},
});

Expand Down
9 changes: 7 additions & 2 deletions examples/experimental/job/waitForResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ async function main() {
await golem.init();

const job = golem.createJob<string>({
package: {
imageTag: "golem/alpine:latest",
demand: {
imageTag: "severyn/espeak:latest",
},
market: {}, // TODO: This should be optional
payment: {
driver: "erc20",
network: "holesky",
},
});

Expand Down
Loading

0 comments on commit 605311b

Please sign in to comment.