Yadah Critical Section mixin
A mixin for Yadah Domain classes that provides a way to run asynchronous code which will be allowed to complete prior to a process exiting. This helps to ensure that data will be consistent across different services even in the event that a service is being restarted.
Usage
Add the mixin to a domain class and provide a callback function to the
criticalSection()
method. During a process shutdown, the domain class
shutdown()
method won't resolve until the callback resolves.
import CriticalSectionMixin from "@yadah/domain-critical-section";
import { Domain } from "@yadah/data-manager";
import { pipe } from "@yadah/mixin";
class MyDomain extends pipe(Domain, CriticalSectionMixin) {
async performRemoteMutation() {
await this.criticalSection(async () => {
const result = await remoteMutation();
await localMutation(result);
});
}
}
In this example, the remoteMutation()
function may take some time to complete,
and when it does the result will be stored somewhere using the localMutation()
method. It's desired to ensure that both functions will complete even if
the nodejs process is stopped (eg. via nodemon
or ECS task replacement).
The callback function may be a generator function returning an
async iterable.
The domain class shutdown()
method won't resolve until the iterator has
resolved.
import CriticalSectionMixin from "@yadah/domain-critical-section";
import { Domain } from "@yadah/data-manager";
import { pipe } from "@yadah/mixin";
class MyDomain extends pipe(Domain, CriticalSectionMixin) {
async performRemoteMutation(list) {
async function* generator() {
for await (const item of list) {
yield await remoteMutation(item);
}
}
const iterable = this.criticalSection(generator);
for await (const result of iterable) {
await localMutation(result);
}
}
}
A Promise may also be passed to criticalSection()
. Again, the shutdown()
method won't resolve until the promise is resolved.
import CriticalSectionMixin from "@yadah/domain-critical-section";
import { Domain } from "@yadah/data-manager";
import { pipe } from "@yadah/mixin";
class MyDomain extends pipe(Domain, CriticalSectionMixin) {
async performRemoteMutation() {
const promise = remoteMutation().then((result) => localMutation(result));
await this.criticalSection(promise);
}
}