Use Beacons to delay shutdown handlers
mainBeacons are used to prevent the shutdown routine from executing until specific asynchronous tasks are complete.
How Beacons work:
- A beacon is created via
lightship.createBeacon(). - A beacon is considered 'live' upon creation.
- Shutdown handlers are suspended as long as there is at least one live beacon.
- To signal a task is finished, call
beacon.die(). Once all beacons are dead, the shutdown handlers run. - Beacons can accept an optional
contextobject for better logging.
Use Case: Use beacons when processing jobs in a loop to ensure the process doesn't terminate while a job is in progress.
for (const job of jobs) {
if (lightship.isServerShuttingDown()) {
break;
}
const beacon = lightship.createBeacon({ jobId: job.id });
// ... perform the job ...
await beacon.die();
}