Skip to content

Commit

Permalink
feat: rename finalize to stopAndFinalize
Browse files Browse the repository at this point in the history
  • Loading branch information
SewerynKras committed Jun 19, 2024
1 parent 8e1eb66 commit 76fe8e5
Show file tree
Hide file tree
Showing 19 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const order: MarketOrderSpec = {
.getExeUnit()
.then((exe) => exe.run("echo Hello, Golem! 👋"))
.then((res) => console.log(res.stdout));
await rental.finalize();
await rental.stopAndFinalize();
} catch (err) {
console.error("Failed to run the example", err);
} finally {
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/override-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const order: MarketOrderSpec = {
.getExeUnit()
.then((exe) => exe.run("echo Hello, Golem! 👋"))
.then((res) => console.log(res.stdout));
await rental.finalize();
await rental.stopAndFinalize();
} catch (err) {
console.error("Failed to run the example", err);
} finally {
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/payment-filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const order: MarketOrderSpec = {
.getExeUnit()
.then((exe) => exe.run("echo Hello, Golem! 👋"))
.then((res) => console.log(res.stdout));
await rental.finalize();
await rental.stopAndFinalize();
} catch (err) {
console.error("Failed to run the example", err);
} finally {
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/proposal-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const order: MarketOrderSpec = {
.getExeUnit()
.then((exe) => exe.run(`echo [provider:${exe.provider.name}] Hello, Golem! 👋`))
.then((res) => console.log(res.stdout));
await rental.finalize();
await rental.stopAndFinalize();
} catch (err) {
console.error("Failed to run the example", err);
} finally {
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/proposal-predefined-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const order: MarketOrderSpec = {
.getExeUnit()
.then((exe) => exe.run(`echo [provider:${exe.provider.name}] Hello, Golem! 👋`))
.then((res) => console.log(res.stdout));
await rental.finalize();
await rental.stopAndFinalize();
} catch (err) {
console.error("Failed to run the example", err);
} finally {
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/proposal-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const order: MarketOrderSpec = {
.getExeUnit()
.then((exe) => exe.run(`echo [provider:${exe.provider.name}] Hello, Golem! 👋`))
.then((res) => console.log(res.stdout));
await rental.finalize();
await rental.stopAndFinalize();
} catch (err) {
console.error("Failed to run the example", err);
} finally {
Expand Down
4 changes: 2 additions & 2 deletions examples/advanced/reuse-allocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ import { pinoPrettyLogger } from "@golem-sdk/pino-logger";
.then((exe) => exe.run("echo Running on second rental"))
.then((res) => console.log(res.stdout));

await rental1.finalize();
await rental2.finalize();
await rental1.stopAndFinalize();
await rental2.stopAndFinalize();
await glm.payment.releaseAllocation(allocation);
} catch (err) {
console.error("Failed to run the example", err);
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import { pinoPrettyLogger } from "@golem-sdk/pino-logger";
.then((exe) => exe.run("echo Hello, Golem! 👋"))
.then((res) => console.log(res.stdout));

await rental.finalize();
await rental.stopAndFinalize();
} catch (err) {
console.error("Failed to run the example", err);
} finally {
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/one-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const order: MarketOrderSpec = {
.getExeUnit()
.then((exe) => exe.run("echo Hello, Golem! 👋"))
.then((res) => console.log(res.stdout));
await rental.finalize();
await rental.stopAndFinalize();
} catch (err) {
console.error("Failed to run the example", err);
} finally {
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/run-and-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const order: MarketOrderSpec = {
remoteProcess.stderr.on("data", (data) => console.error("stderr>", data));
await remoteProcess.waitForExit();

await rental.finalize();
await rental.stopAndFinalize();
} catch (err) {
console.error("Failed to run the example", err);
} finally {
Expand Down
4 changes: 2 additions & 2 deletions examples/basic/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ const order: MarketOrderSpec = {
console.log("File content: ");
console.log(await readFile("./results.txt", { encoding: "utf-8" }));

await rental1.finalize();
await rental2.finalize();
await rental1.stopAndFinalize();
await rental2.stopAndFinalize();
} catch (err) {
console.error("Failed to run the example", err);
} finally {
Expand Down
2 changes: 1 addition & 1 deletion examples/web/hello.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ <h3>Results</h3>
.then(async (exe) =>
appendResults("Reply: " + (await exe.run(`echo 'Hello Golem! 👋 from ${exe.provider.name}!'`)).stdout),
);
await rental.finalize();
await rental.stopAndFinalize();
appendResults("Finalized renting process");
} catch (err) {
console.error("Failed to run the example", err);
Expand Down
2 changes: 1 addition & 1 deletion src/experimental/job/job.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe("Job", () => {

await expect(job.waitForResult()).rejects.toThrow("Canceled");

verify(mockRental.finalize()).once();
verify(mockRental.stopAndFinalize()).once();
});
});
});
2 changes: 1 addition & 1 deletion src/experimental/job/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class Job<Output = unknown> {
this.events.emit("started");

const onAbort = async () => {
await rental.finalize();
await rental.stopAndFinalize();
this.events.emit("canceled");
};

Expand Down
8 changes: 4 additions & 4 deletions src/golem-network/golem-network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe("Golem Network", () => {
const mockResourceRental = mock(ResourceRental);
const mockResourceRentalInstance = instance(mockResourceRental);

when(mockResourceRental.finalize()).thenResolve();
when(mockResourceRental.stopAndFinalize()).thenResolve();
when(mockRental.createResourceRental(_, _, _)).thenReturn(mockResourceRentalInstance);

const draftProposal$ = new Subject<OfferProposal>();
Expand All @@ -93,15 +93,15 @@ describe("Golem Network", () => {

await glm.disconnect();

verify(mockResourceRental.finalize()).once();
verify(mockResourceRental.stopAndFinalize()).once();
verify(mockPayment.releaseAllocation(allocation)).once();
});
it("should not release the allocation if it was provided by the user", async () => {
const allocation = instance(mock(Allocation));

const mockResourceRental = mock(ResourceRental);
const mockResourceRentalInstance = instance(mockResourceRental);
when(mockResourceRental.finalize()).thenResolve();
when(mockResourceRental.stopAndFinalize()).thenResolve();
when(mockRental.createResourceRental(_, _, _)).thenReturn(mockResourceRentalInstance);

when(mockMarket.collectDraftOfferProposals(_)).thenReturn(new Subject<OfferProposal>());
Expand All @@ -121,7 +121,7 @@ describe("Golem Network", () => {

await glm.disconnect();

verify(mockResourceRental.finalize()).once();
verify(mockResourceRental.stopAndFinalize()).once();
verify(mockPayment.createAllocation(_)).never();
verify(mockPayment.releaseAllocation(allocation)).never();
});
Expand Down
4 changes: 2 additions & 2 deletions src/golem-network/golem-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export class GolemNetwork {
* .getExeUnit()
* .then((exe) => exe.run("echo Hello, Golem! 👋"))
* .then((res) => console.log(res.stdout));
* await rental.finalize();
* await rental.stopAndFinalize();
* ```
*
* @param order
Expand Down Expand Up @@ -402,7 +402,7 @@ export class GolemNetwork {
this.cleanupTasks.push(async () => {
// First finalize the rental (which will wait for all payments to be processed)
// and only then release the allocation
await rental.finalize().catch((err) => this.logger.error("Error while finalizing rental", err));
await rental.stopAndFinalize().catch((err) => this.logger.error("Error while finalizing rental", err));
if (order.network && networkNode) {
await this.network
.removeNetworkNode(order.network, networkNode)
Expand Down
2 changes: 1 addition & 1 deletion src/resource-rental/resource-rental-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export class ResourceRentalPool {
try {
this.borrowed.delete(resourceRental);
this.logger.debug("Destroying resource rental from the pool", { agreementId: resourceRental.agreement.id });
await Promise.all([resourceRental.finalize(), this.removeNetworkNode(resourceRental)]);
await Promise.all([resourceRental.stopAndFinalize(), this.removeNetworkNode(resourceRental)]);
this.events.emit("destroyed", resourceRental.agreement);
} catch (error) {
this.events.emit(
Expand Down
3 changes: 2 additions & 1 deletion src/resource-rental/resource-rental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ export class ResourceRental {
}

/**
* Terminates the activity and agreement (stopping any ongoing work) and finalizes the payment process.
* Resolves when the rental will be fully terminated and all pending business operations finalized.
* If the rental is already finalized, it will resolve immediately.
*/
async finalize() {
async stopAndFinalize() {
// Prevent this task from being performed more than once
if (!this.finalizePromise) {
this.finalizePromise = (async () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/resourceRentalPool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe("ResourceRentalPool", () => {
const exe = await resourceRental.getExeUnit();
return new Promise(async (res) => {
resourceRental.events.on("finalized", async () => res(true));
setTimeout(() => resourceRental.finalize(), 8_000);
setTimeout(() => resourceRental.stopAndFinalize(), 8_000);
await expect(exe.run("sleep 10 && echo Hello World")).rejects.toThrow(
new GolemAbortError("Execution of script has been aborted"),
);
Expand Down

0 comments on commit 76fe8e5

Please sign in to comment.