Code Monkey home page Code Monkey logo

Comments (3)

ltagliamonte-dd avatar ltagliamonte-dd commented on August 28, 2024

as per doc using beforeShutdown hook i can wait but i don't understand the timeout option at this point

const http = require('http');
const { createTerminus } = require('@godaddy/terminus');

function onSignal () {
  console.log('server is starting cleanup');
}

function beforeShutdown () {
  console.log('waiting');
  return new Promise(resolve => {
    setTimeout(resolve, 30000)
  })
}

function onShutdown () {
  console.log('cleanup finished, server is shutting down');
}

const server = http.createServer((request, response) => {
  response.end(
    `<html>
      <body>
        <h1>Hello, World!</h1>
       </body>
     </html>`
   );
})

const options = {
  timeout: 30000,
  onSignal: onSignal,                       
  onShutdown: onShutdown,
  beforeShutdown: beforeShutdown          
};

createTerminus(server,options);

server.listen(8080);

from terminus.

MichaelSitter avatar MichaelSitter commented on August 28, 2024

I forked the repo to do a little experimentation with the timeout handling. afaik timeout does work, I don't think your test is exercising the feature correctly b/c you aren't creating any connections before stopping the server. It might be helpful if there was a test case to verify timeout is respected. You can see my findings here:
MichaelSitter#1

from terminus.

ltagliamonte-dd avatar ltagliamonte-dd commented on August 28, 2024

thank you @MichaelSitter I confirm that with requests flowing the following snippet works:

const http = require('http');
const { createTerminus } = require('@godaddy/terminus');

function onSignal () {
  console.log('server is starting cleanup');
}

function beforeShutdown () {
  console.log('waiting for k8s environment to completely drain this service');
  return new Promise(resolve => {
    setTimeout(resolve, 10000)
  })
}

function onShutdown () {
  console.log('cleanup finished, server is shutting down');
}

const server = http.createServer(
  (req, res) => {
    setTimeout(() => {
      res.end('hello')
    }, 5000)
  })

const options = {
  timeout: 10000,
  onSignal: onSignal,                       
  onShutdown: onShutdown,
  beforeShutdown: beforeShutdown       
};

createTerminus(server,options);

server.listen(8080);

from terminus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.