-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor command-running to run oneshots after startup
This refactors the command-running utilities to take the fx.Options as variadic arguments, so that fxapps.OneShot can add its own fx.Option. It uses a bit of reflection to convince Fx to gather the arguments to a function without actually calling that function immediately, allowing the oneShot function to run after `app.Start` has finished, when we're sure that all components have started. This works around the startup ordering bug in uber-go/fx#918.
- Loading branch information
Showing
8 changed files
with
97 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
package fxapps | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
|
||
"go.uber.org/fx" | ||
) | ||
|
||
var errorInterface = reflect.TypeOf((*error)(nil)).Elem() | ||
|
||
// OneShot runs the given function in an fx.App using the supplied options. | ||
// The function's arguments are supplied by Fx and can be any provided type. | ||
// The function must return `error. | ||
// | ||
// The resulting app starts all components, then invokes the function, then | ||
// immediately shuts down. This is typically used for command-line tools like | ||
// `agent status`. | ||
func OneShot(oneShotFunc interface{}, opts ...fx.Option) error { | ||
ftype := reflect.TypeOf(oneShotFunc) | ||
if ftype == nil || ftype.Kind() != reflect.Func { | ||
panic("OneShot requires a function as its first argument") | ||
} | ||
|
||
// verify it returns error | ||
if ftype.NumOut() != 1 || !ftype.Out(0).Implements(errorInterface) { | ||
panic("OneShot function must return error or nothing") | ||
} | ||
|
||
// build an function with the same signature as oneShotFunc that will | ||
// capture the args and do nothing. | ||
var oneShotArgs []reflect.Value | ||
captureArgs := reflect.MakeFunc( | ||
ftype, | ||
func(args []reflect.Value) []reflect.Value { | ||
oneShotArgs = args | ||
// return a single nil value of type error | ||
return []reflect.Value{reflect.Zero(errorInterface)} | ||
}) | ||
// fx.Invoke that function to capture the args at startup | ||
opt := fx.Invoke(captureArgs.Interface()) | ||
opts = append(opts, opt) | ||
app := fx.New(opts...) | ||
|
||
startCtx, cancel := context.WithTimeout(context.Background(), app.StartTimeout()) | ||
defer cancel() | ||
|
||
if err := app.Start(startCtx); err != nil { | ||
return err | ||
} | ||
|
||
// call the original oneShotFunc with the args captured during | ||
// app startup | ||
res := reflect.ValueOf(oneShotFunc).Call(oneShotArgs) | ||
if !res[0].IsNil() { | ||
err := res[0].Interface().(error) | ||
return err | ||
} | ||
|
||
stopCtx, cancel := context.WithTimeout(context.Background(), app.StopTimeout()) | ||
defer cancel() | ||
|
||
if err := app.Stop(stopCtx); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters