-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use commit signals and pending values
- Loading branch information
Showing
4 changed files
with
139 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { apps } from "./services/databases/mongodb"; | ||
import "./services/ementas"; | ||
import { CommitSignal } from "./utils/pending"; | ||
|
||
apps.commit(); | ||
CommitSignal.globalParent.resolve(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import * as pulumi from "@pulumi/pulumi"; | ||
|
||
type CommitSignalOptions = { | ||
resource?: pulumi.Resource; | ||
rejectIfNotCommitted?: boolean; | ||
} | ||
|
||
const warnMessage = "This may affect the deployment of certain resources."; | ||
|
||
export class CommitSignal { | ||
public static globalParent = new CommitSignal(); | ||
|
||
private committed = false; | ||
private action = Promise.withResolvers<void>(); | ||
|
||
public resource; | ||
|
||
constructor(opts?: CommitSignalOptions) { | ||
this.resource = opts?.resource; | ||
|
||
this.action.promise.finally(() => { | ||
if (this.committed) | ||
return; | ||
|
||
pulumi.log.error(`Commit was not committed. ${warnMessage}`, this.resource); | ||
}); | ||
|
||
if (this !== CommitSignal.globalParent) | ||
this.attachTo(CommitSignal.globalParent, opts?.rejectIfNotCommitted); | ||
} | ||
|
||
public waitForCommit() { | ||
return this.action.promise; | ||
} | ||
|
||
public resolve() { | ||
if (this.committed) { | ||
pulumi.log.error(`Commit resolved after being committed. ${warnMessage}`, this.resource); | ||
return; | ||
} | ||
|
||
this.committed = true; | ||
this.action.resolve(); | ||
} | ||
|
||
public reject(error: Error) { | ||
if (this.committed) { | ||
pulumi.log.error(`Commit rejected after being committed. ${warnMessage}`, this.resource); | ||
return; | ||
} | ||
|
||
this.committed = true; | ||
this.action.reject(error); | ||
} | ||
|
||
public attachTo(parent: CommitSignal, rejectIfNotCommitted?: boolean) { | ||
if (parent.committed) { | ||
pulumi.log.warn(`Commit attached when parent was already committed. ${warnMessage}`, this.resource); | ||
return; | ||
} | ||
|
||
rejectIfNotCommitted ??= false; | ||
|
||
parent.action.promise | ||
.then(() => { | ||
if (this.committed) | ||
return; | ||
|
||
if (rejectIfNotCommitted) { | ||
this.reject(new Error("Commit was not committed before parent.")); | ||
} else { | ||
this.resolve(); | ||
} | ||
}) | ||
.catch(err => { | ||
if (this.committed) | ||
return; | ||
|
||
this.reject(err); | ||
}); | ||
} | ||
} | ||
|
||
type PendingValueOptions = { commitSignal?: CommitSignal } | ||
|
||
export class PendingValue<T> { | ||
|
||
public readonly commit; | ||
|
||
constructor(private value: T, opts?: PendingValueOptions) { | ||
this.commit = opts?.commitSignal ?? new CommitSignal(); | ||
} | ||
|
||
public run(func: (value: T) => void) { | ||
func(this.get()); | ||
} | ||
|
||
public get() { | ||
return this.value; | ||
} | ||
|
||
public asOutput() { | ||
return pulumi.output(this.waitForValue()); | ||
} | ||
|
||
public async waitForValue() { | ||
await this.commit.waitForCommit(); | ||
return this.value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters