Skip to content

Commit fb64ed1

Browse files
committed
Add how-to on beforeExit
1 parent 30af98e commit fb64ed1

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,21 @@ app.post("/delta", bodyParser.json({ limit: '50mb' }), function(req, res) {
163163
}
164164
```
165165

166+
### Clean up on service exit
167+
Most services just want to stop answering questions when the service exits, some need to clean up state. Cleaning up state can be done by supplying an async fincution to `beforeExit`. The service will stop accepting connections but will execute this async function before gracefully exiting. Most setups provide up to 10 seconds for the container to cleanly exit.
168+
169+
An example
170+
171+
```javascript
172+
import { beforeExit } from 'mu';
173+
174+
beforeExit( async () => {
175+
console.log("Cleaning up...");
176+
await new Promise( (acc) => setTimeout(acc, 1000) );
177+
console.log("Finished cleaning up.");
178+
});
179+
```
180+
166181
## Reference
167182
### Framework
168183
The mu-javascript-template is built on ExpressJS. Check [Express' Getting Started guide](https://expressjs.com/en/starter/basic-routing.html) to learn how to build a REST API in Express.

0 commit comments

Comments
 (0)