How to organize error handling at OnStart hook properly?
If this hook is intended to call long-running goroutines like this, then why it waits for an error that never can be provided due to the locking nature of the call?
We only can return nil there, otherwise, if try to return s.ListenAndServe(), then we get the start timeout.
So, does fx assume to omit error handling here?
Hi there! Thanks for your question.
Background: OnStart supports returning an error if you have a means of knowing, at start up time, whether your background process started up successfully.
Normally, we don't ignore errors for cases like that. That example is imperfect to keep the example short and easy to explain. Internally, the code that starts our HTTP servers up like that blocks until the HTTP server is ready to begin accepting requests. If the HTTP server fails to start up, or if it is not ready to receive requests within the configured StartTimeout, the OnStart hook fails with a timeout and application start up, likewise, fails.
In such cases I add errors channel to container, inject it to provider function, and then run http server like:
go func(){
if err := s.ListenAndServer(); err != nil{
runtimeErr <- err
}
}