func(i *interruptListener)notifyAndFire() { ch := make(chan os.Signal, 1) signal.Notify(ch, // kill -SIGINT XXXX or Ctrl+c os.Interrupt, syscall.SIGINT, // register that too, it should be ok // os.Kill is equivalent with the syscall.SIGKILL os.Kill, syscall.SIGKILL, // register that too, it should be ok // kill -SIGTERM XXXX syscall.SIGTERM, ) select { case <-ch: i.FireNow() } }
// Before continue: // // Gracefully Shutdown on control+C/command+C or when kill command sent is ENABLED BY-DEFAULT. // // In order to manually manage what to do when app is interrupted, // We have to disable the default behavior with the option `WithoutInterruptHandler` // and register a new interrupt handler (globally, across all possible hosts). funcmain() { app := iris.New()
iris.RegisterOnInterrupt(func() { timeout := 5 * time.Second ctx, cancel := stdContext.WithTimeout(stdContext.Background(), timeout) defer cancel() // close all hosts app.Shutdown(ctx) })
app.Get("/", func(ctx iris.Context) { ctx.HTML(" <h1>hi, I just exist in order to see if the server is closed</h1>") })
Now listening on: http://localhost:8080 Application started. Press CMD+C to shut down. 2019/10/20 18:07:56 on interrupt 1 2019/10/20 18:07:56 on interrupt 2 2019/10/20 18:07:56 on interrupt 3 [ERRO] 2019/10/20 18:07 http: Server closed
app.Get("/", func(ctx iris.Context) { ctx.HTML("<h1>hi, I just exist in order to see if the server is closed</h1>") })
gofunc() { ch := make(chan os.Signal, 1) signal.Notify(ch, // kill -SIGINT XXXX or Ctrl+c os.Interrupt, syscall.SIGINT, // register that too, it should be ok // os.Kill is equivalent with the syscall.Kill os.Kill, syscall.SIGKILL, // register that too, it should be ok // kill -SIGTERM XXXX syscall.SIGTERM, ) select { case <-ch: println("shutdown...")
// Start the server and disable the default interrupt handler in order to // handle it clear and simple by our own, without any issues. app.Run(iris.Addr(":8080"), iris.WithoutInterruptHandler) }