Skip to content

Commit

Permalink
hooks: Ensure that plugins are stopped when tusd exits
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut committed Aug 22, 2023
1 parent ad074c8 commit e10a102
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/tusd/cli/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

tushandler "github.com/tus/tusd/v2/pkg/handler"
"github.com/tus/tusd/v2/pkg/hooks"
"github.com/tus/tusd/v2/pkg/hooks/plugin"
)

const (
Expand Down Expand Up @@ -236,6 +237,9 @@ func setupSignalHandler(server *http.Server, handler *tushandler.Handler) <-chan
stderr.Printf("Failed to shutdown gracefully: %s\n", err)
}

// Make sure that the plugins exit properly.
plugin.CleanupPlugins()

close(shutdownComplete)
}()

Expand Down
10 changes: 10 additions & 0 deletions pkg/hooks/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type PluginHook struct {
handlerImpl hooks.HookHandler
}

// Setup initiates the connection to the plugin. Note: When the main process ends,
// you must call CleanupClients() to ensure that the subprocess is properly cleaned up.
func (h *PluginHook) Setup() error {
// We're a host! Start by launching the plugin process.
client := plugin.NewClient(&plugin.ClientConfig{
Expand All @@ -31,6 +33,8 @@ func (h *PluginHook) Setup() error {
Cmd: exec.Command(h.Path),
SyncStdout: os.Stdout,
SyncStderr: os.Stderr,
// We use a managed client, so we can use plugin.CleanupClients() to shut it down.
Managed: true,
//Logger: logger,
})
//defer client.Kill()
Expand Down Expand Up @@ -58,6 +62,12 @@ func (h *PluginHook) InvokeHook(req hooks.HookRequest) (hooks.HookResponse, erro
return h.handlerImpl.InvokeHook(req)
}

// CleanupPlugins closes the connections to all plugins and ensures that their processes are
// properly stopped. You must call this function when the main process exits.
func CleanupPlugins() {
plugin.CleanupClients()
}

// handshakeConfigs are used to just do a basic handshake between
// a plugin and host. If the handshake fails, a user friendly error is shown.
// This prevents users from executing bad plugins or executing a plugin
Expand Down

0 comments on commit e10a102

Please sign in to comment.