cookie-cutter
cookie-cutter copied to clipboard
Allow Application to Exit without Shutting Down the Process
Cookie Cutter has a mechanism to detect if an application is hanging and won't shutdown gracefully. That is done with a simple timer that kills the process if the process hasn't exited by itself already. This mechanism can get in the way if a service wants to stop and restart its application without exiting the process. An example for that could be hot-reloading of configuration changes without rescheduling the pod in Kubernetes.
We should make this behavior configurable via the application behavior that is passed into the run
function.
import { Application, ConsoleLogger, sleep } from "@walmartlabs/cookie-cutter-core";
import { intervalSource } from "@walmartlabs/cookie-cutter-timer";
class Output { }
const handler = {
onInterval: (msg, ctx) => {
ctx.publish(Output, { ...msg });
}
};
const app = () => Application.create()
.logger(new ConsoleLogger())
.input()
.add(
intervalSource({
timeout: 1000,
})
)
.done()
.dispatch(handler)
.run();
// this only works with this hack, which makes
// the CC application believe it's in test mode
// (global as any).it = () => { };
async function main() {
for (let i=0; i<10; i++) {
console.log("starting app");
const a = app();
await sleep(1000);
a.cancel();
await a;
}
}
main();