Skip to content

Commit

Permalink
Merge pull request #934 from golemfactory/feature/JST-928/many-of
Browse files Browse the repository at this point in the history
Many-of
  • Loading branch information
SewerynKras authored May 23, 2024
2 parents 6db5d84 + d87d90b commit 129e7e5
Show file tree
Hide file tree
Showing 25 changed files with 665 additions and 410 deletions.
4 changes: 0 additions & 4 deletions examples/basic/hello-world-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ const demandOptions: DemandSpec = {
maxEnvPerHourPrice: 0.5,
},
},
payment: {
driver: "erc20",
network: "holesky",
},
};

(async () => {
Expand Down
4 changes: 0 additions & 4 deletions examples/basic/hello-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ import { pinoPrettyLogger } from "@golem-sdk/pino-logger";
maxEnvPerHourPrice: 1,
},
},
payment: {
network: "holesky",
driver: "erc20",
},
};

const proposalPool = new DraftOfferProposalPool({
Expand Down
63 changes: 63 additions & 0 deletions examples/basic/many-of.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* This example demonstrates how easily lease multiple machines at once.
*/

import { DemandSpec, GolemNetwork } from "@golem-sdk/golem-js";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

const demand: DemandSpec = {
demand: {
activity: { imageTag: "golem/alpine:latest" },
},
market: {
maxAgreements: 1,
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();
// create a pool that can grow up to 3 leases at the same time
const pool = await glm.manyOf({
concurrency: 3,
demand,
});
await Promise.allSettled([
pool.withLease(async (lease) =>
lease
.getExeUnit()
.then((exe) => exe.run("echo Hello, Golem from the first machine! 👋"))
.then((res) => console.log(res.stdout)),
),
pool.withLease(async (lease) =>
lease
.getExeUnit()
.then((exe) => exe.run("echo Hello, Golem from the second machine! 👋"))
.then((res) => console.log(res.stdout)),
),
pool.withLease(async (lease) =>
lease
.getExeUnit()
.then((exe) => exe.run("echo Hello, Golem from the third machine! 👋"))
.then((res) => console.log(res.stdout)),
),
]);
} catch (err) {
console.error("Failed to run the example", err);
} finally {
await glm.disconnect();
}
})().catch(console.error);
11 changes: 0 additions & 11 deletions examples/deployment/new-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@ async function main() {
logger: pinoPrettyLogger({
level: "info",
}),
market: {
maxAgreements: 1,
rentHours: 0.5,
pricing: {
model: "linear",
maxStartPrice: 1,
maxCpuPerHourPrice: 1,
maxEnvPerHourPrice: 1,
},
},
dataTransferProtocol: "gftp",
});

try {
Expand Down
4 changes: 0 additions & 4 deletions examples/experimental/express/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ app.post("/tts", async (req, res) => {
maxEnvPerHourPrice: 1,
},
},
payment: {
driver: "erc20",
network: "holesky",
},
});

job.events.on("created", () => {
Expand Down
4 changes: 0 additions & 4 deletions examples/experimental/job/cancel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ async function main() {
maxEnvPerHourPrice: 1,
},
},
payment: {
driver: "erc20",
network: "holesky",
},
});

console.log("Job object created, initial status is", job.state);
Expand Down
4 changes: 0 additions & 4 deletions examples/experimental/job/getJobById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ function startJob() {
maxEnvPerHourPrice: 1,
},
},
payment: {
driver: "erc20",
network: "holesky",
},
});

console.log("Job object created, initial status is", job.state);
Expand Down
4 changes: 0 additions & 4 deletions examples/experimental/job/waitForResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ async function main() {
maxEnvPerHourPrice: 1,
},
},
payment: {
driver: "erc20",
network: "holesky",
},
});

console.log("Job object created, initial status is", job.state);
Expand Down
6 changes: 0 additions & 6 deletions examples/pool/hello-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ const demandOptions = {

const glm = new GolemNetwork({
logger,
payment: {
payment: {
driver: "erc20",
network: "holesky",
},
},
});
let allocation: Allocation | undefined;

Expand Down
2 changes: 1 addition & 1 deletion src/activity/activity.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { EventEmitter } from "eventemitter3";
import { Agreement } from "../agreement";
import { Activity, IActivityApi } from "./index";
import { defaultLogger } from "../shared/utils";
import { GolemServices } from "../golem-network";
import { GolemServices } from "../golem-network/golem-network";
import { WorkContext, WorkOptions } from "./work";

export interface ActivityEvents {}
Expand Down
21 changes: 21 additions & 0 deletions src/agreement/lease-process-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,25 @@ export class LeaseProcessPool {
}
this.events.emit("ready");
}

/**
* Acquire a lease process from the pool and release it after the callback is done
* @example
* ```typescript
* const result = await pool.withLease(async (lease) => {
* // Do something with the lease
* return result;
* // pool.release(lease) is called automatically
* // even if an error is thrown in the callback
* });
* ```
*/
public async withLease<T>(callback: (lease: LeaseProcess) => Promise<T>): Promise<T> {
const lease = await this.acquire();
try {
return await callback(lease);
} finally {
await this.release(lease);
}
}
}
7 changes: 6 additions & 1 deletion src/agreement/lease-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,14 @@ export class LeaseProcess {
}

/**
* @return Resolves when the lease will be fully terminated and all pending business operations finalized
* Resolves when the lease will be fully terminated and all pending business operations finalized.
* If the lease is already finalized, it will resolve immediately.
*/
async finalize() {
if (this.paymentProcess.isFinished()) {
return;
}

try {
this.logger.debug("Waiting for payment process of agreement to finish", { agreementId: this.agreement.id });
if (this.currentActivity) {
Expand Down
2 changes: 1 addition & 1 deletion src/deployment/builder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GolemConfigError } from "../shared/error/golem-error";
import { NetworkOptions } from "../network";
import { Deployment, DeploymentComponents } from "./deployment";
import { GolemNetwork } from "../golem-network";
import { GolemNetwork } from "../golem-network/golem-network";
import { validateDeployment } from "./validate-deployment";
import { MarketOptions } from "../market";
import { PaymentModuleOptions } from "../payment";
Expand Down
4 changes: 0 additions & 4 deletions src/experimental/job/job.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ describe.skip("Job", () => {
maxCpuPerHourPrice: 1,
},
},
payment: {
network: "holesky",
driver: "erc20",
},
},
instance(imock<Logger>()),
);
Expand Down
2 changes: 1 addition & 1 deletion src/experimental/job/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NetworkOptions } from "../../network";
import { PaymentModuleOptions } from "../../payment";
import { EventEmitter } from "eventemitter3";
import { GolemAbortError, GolemUserError } from "../../shared/error/golem-error";
import { GolemNetwork } from "../../golem-network";
import { GolemNetwork } from "../../golem-network/golem-network";
import { Logger } from "../../shared/utils";
import { ActivityDemandDirectorConfigOptions } from "../../market/demand/options";

Expand Down
2 changes: 1 addition & 1 deletion src/experimental/job/job_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { v4 } from "uuid";
import { Job, RunJobOptions } from "./job";
import { defaultLogger, Logger, runtimeContextChecker, YagnaOptions } from "../../shared/utils";
import { GolemUserError } from "../../shared/error/golem-error";
import { GolemNetwork } from "../../golem-network";
import { GolemNetwork } from "../../golem-network/golem-network";
import {
GftpStorageProvider,
NullStorageProvider,
Expand Down
Loading

0 comments on commit 129e7e5

Please sign in to comment.