-
Notifications
You must be signed in to change notification settings - Fork 290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support app shutdown with exit codes #808
Changes from 2 commits
f0230d9
e5a682a
c482a9a
85ed3ac
b3f67f4
afe8f0e
4b32dc6
ade54ab
978250b
5d714d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,18 @@ type ShutdownOption interface { | |
apply(*shutdowner) | ||
} | ||
|
||
type exitCodeOption int | ||
|
||
func (e exitCodeOption) apply(s *shutdowner) { | ||
sywhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
s.app.exitCode = int(e) | ||
} | ||
Comment on lines
+44
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I missed this before: The standard functional options pattern we have in our style guide uses an internal options struct for the options. For example, type shutdownOptions struct {
ExitCode int
}
type ShutdownOption interface{ apply(*shutdownOptions) }
func WithExitCode(int) ShutdownOption I feel like the functional options should resolve the exit code in the s.Shutdown call, and send the appropriate value into the channel from there. Then in Run, we'll use DoneWithCode and the returned exit code and all the state management for app.exitCode can be dropped. |
||
|
||
// WithExitCode allows the user to configure the exitCode upon application | ||
// shutdown. | ||
Comment on lines
+52
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about,
|
||
func WithExitCode(exitCode int) ShutdownOption { | ||
return exitCodeOption(exitCode) | ||
} | ||
|
||
type shutdowner struct { | ||
app *App | ||
} | ||
|
@@ -49,6 +61,10 @@ type shutdowner struct { | |
// In practice this means Shutdowner.Shutdown should not be called from an | ||
// fx.Invoke, but from a fx.Lifecycle.OnStart hook. | ||
func (s *shutdowner) Shutdown(opts ...ShutdownOption) error { | ||
// Apply the options to shutdowner | ||
manjari25 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for _, opt := range opts { | ||
opt.apply(s) | ||
} | ||
return s.app.broadcastSignal(_sigTERM) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is kinda ugly. Rather than doing this, can you change
exitCode
's default value to be 1 and changerun
to returnapp.exitCode
on error cases?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, will that not rewrite the exitCode set upon app shutdown?