Executing synchronous tasks upon exit/shutdown

  • Is there a NodeBB hook that I could use to execute certain synchronous operations when the forum is restarting or even crashing?

    I already use the hooks static:app.load and static:app.preload so I guess I'm looking for a static:app.unload.

    Right now I'm using process.on("exit") which seems to do what I want, but apparently it is executed after NodeBB has already closed all connections:

    2019-11-10T14:58:34.617Z [8081/17108] - info: [app] Shutdown (SIGTERM/SIGINT) Initialised.
    2019-11-10T14:58:34.784Z [8081/17108] - info: [app] Web server closed to connections.
    2019-11-10T14:58:34.787Z [8081/17108] - info: [app] Live analytics saved.
    2019-11-10 14:58:34:807 -  server.js:71:5) -  socketCleanup close 81.165.Y.X:33352 Total 10 > myplugin
    2019-11-10 14:58:34:809 -  server.js:71:5) -  socketCleanup close 108.41.Y.X:33300 Total 9 > myplugin
    2019-11-10 14:58:34:811 -  server.js:71:5) -  socketCleanup close 81.165.Y.X:33284 Total 8 > myplugin
    2019-11-10 14:58:35:685 -  server.js:71:5) -  socketCleanup close 123.186.Y.X:58106 Total 1 > myplugin
    2019-11-10 14:58:35:685 -  server.js:71:5) -  socketCleanup close 123.186.Y.X:58066 Total 0 > myplugin
    2019-11-10T14:58:35.705Z [8081/17108] - info: [app] Database connection closed.
    2019-11-10T14:58:35.708Z [8081/17108] - info: [app] Shutdown complete.
    2019-11-10 14:58:35:708 -  app.js:260:5) -  process.on("exit") with code: 0 > myplugin

  • Global Moderator

    What are you trying to do?

  • My plugin does a lot of caching that I want to save before the server restarts/crashes. Re-building this cache causes high CPU usage for like 30min-1h on my t2.micro so I'm using fs.writeFileSync + JSON.stringify upon process exit and then fs.readFile + JSON.parse when it initializes in order to avoid rebuilding the cache.

    I thought NodeBB offered a hook on shutdown, but I guess I can use process.on("exit").

  • Global Moderator

    I think a better option is to save portions of the cache periodically or as they are created. Then if your server crashes or is killed you still get to keep it.

    You could additionally use the database to persist the cache which would be more lightweight.

Log in to reply

Suggested Topics

| |