fx icon indicating copy to clipboard operation
fx copied to clipboard

How to organize error handling at OnStart hook properly?

Open blank-teer opened this issue 4 years ago • 2 comments

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?

blank-teer avatar Aug 11 '21 17:08 blank-teer

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.

abhinav avatar Aug 11 '21 22:08 abhinav

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
   }
}

saturn4er avatar Apr 11 '22 14:04 saturn4er