foomo / simplecert

golang autocert library for letsencrypt
MIT License
212 stars 34 forks source link

Ctrl+C does not terminate process #16

Closed awnumar closed 3 years ago

awnumar commented 3 years ago

After simplecert has initialised and my own webserver has started, pressing Ctrl+C to terminate results in the following being outputted and nothing else happening:

^C2020/12/13 18:38:40 [INFO] simplecert: closed logfile handle
^C2020/12/13 18:38:41 [FATAL] simplecert: failed to close logfile handle:  close /etc/letsencrypt/live/.../simplecert.log: file already closed
2020/12/13 18:38:41 [INFO] simplecert: closed logfile handle
^C2020/12/13 18:38:42 [FATAL] simplecert: failed to close logfile handle:  close /etc/letsencrypt/live/.../simplecert.log: file already closed
2020/12/13 18:38:42 [INFO] simplecert: closed logfile handle

You can see where ^C was pressed. Can you provide guidance as to what could be causing this?

dreadl0ck commented 3 years ago

Sure,

simplecert kicks off a goroutine in the background to close the logfile handle when a termination signal is received.

You can execute additional logic by providing the cleanup hook in the Init call as such:

certReloader, err := simplecert.Init(cfg,  func() {
    // this function will be called upon receiving the syscall.SIGINT or syscall.SIGABRT signal
    // and can be used to stop your backend gracefully
    os.Exit(0)
})
if err != nil {
    log.Fatal("simplecert init failed: ", err)
}

In this example, your backend would exit after the logfiles have been closed.

In a bigger application you could use this hook to gracefully stop other components of your backend.

awnumar commented 3 years ago

That has solved my issue. Thank you.