@@ -2,6 +2,7 @@ package backend
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "net"
78 "os"
@@ -108,55 +109,48 @@ func Serve(opts ServeOpts) error {
108109 return grpcplugin .Serve (pluginOpts )
109110}
110111
111- // StandaloneServe starts a gRPC server that is not managed by hashicorp.
112- // Deprecated: use GracefulStandaloneServe instead.
113- func StandaloneServe (dsopts ServeOpts , address string ) error {
114- // GracefulStandaloneServe has a new signature, this function keeps the old
115- // signature for existing plugins for backwards compatibility.
116- // Create a new standalone.Args and disable all the standalone-file-related features.
117- return GracefulStandaloneServe (dsopts , standalone.Args {Address : address })
118- }
119-
120112// GracefulStandaloneServe starts a gRPC server that is not managed by hashicorp.
121113// The provided standalone.Args must have an Address set, or the function returns an error.
122114// The function handles creating/cleaning up the standalone address file, and graceful GRPC server termination.
123115// The function returns after the GRPC server has been terminated.
124- func GracefulStandaloneServe (dsopts ServeOpts , info standalone.Args ) error {
116+ func GracefulStandaloneServe (dsopts ServeOpts , info standalone.ServerSettings ) error {
125117 // We must have an address if we want to run the plugin in standalone mode
126118 if info .Address == "" {
127- return fmt . Errorf ("standalone address must be specified" )
119+ return errors . New ("standalone address must be specified" )
128120 }
129121
130- // Write the address to the local file
131- if info .Debugger {
132- log .DefaultLogger .Info ("Creating standalone address and pid files" )
133- if err := standalone .CreateStandaloneAddressFile (info ); err != nil {
134- return fmt .Errorf ("create standalone address file: %w" , err )
122+ if info .Dir == "" {
123+ return errors .New ("directory must be specified" )
124+ }
125+
126+ // Write the address and PID to local files
127+ log .DefaultLogger .Info ("Creating standalone address and pid files" , "dir" , info .Dir )
128+ if err := standalone .CreateStandaloneAddressFile (info .Address , info .Dir ); err != nil {
129+ return fmt .Errorf ("create standalone address file: %w" , err )
130+ }
131+ if err := standalone .CreateStandalonePIDFile (os .Getpid (), info .Dir ); err != nil {
132+ return fmt .Errorf ("create standalone pid file: %w" , err )
133+ }
134+
135+ // sadly vs-code can not listen to shutdown events
136+ // https://github.com/golang/vscode-go/issues/120
137+
138+ // Cleanup function that deletes standalone.txt and pid.txt, if it exists. Fails silently.
139+ // This is so the address file is deleted when the plugin shuts down gracefully, if possible.
140+ defer func () {
141+ log .DefaultLogger .Info ("Cleaning up standalone address and pid files" )
142+ if err := standalone .CleanupStandaloneAddressFile (info ); err != nil {
143+ log .DefaultLogger .Error ("Error while cleaning up standalone address file" , "error" , err )
135144 }
136- if err := standalone .CreateStandalonePIDFile (info ); err != nil {
137- return fmt . Errorf ( "create standalone pid file: %w " , err )
145+ if err := standalone .CleanupStandalonePIDFile (info ); err != nil {
146+ log . DefaultLogger . Error ( "Error while cleaning up standalone pid file" , "error " , err )
138147 }
139-
140- // sadly vs-code can not listen to shutdown events
141- // https://github.com/golang/vscode-go/issues/120
142-
143- // Cleanup function that deletes standalone.txt and pid.txt, if it exists. Fails silently.
144- // This is so the address file is deleted when the plugin shuts down gracefully, if possible.
145- defer func () {
146- log .DefaultLogger .Info ("Cleaning up standalone address and pid files" )
147- if err := standalone .CleanupStandaloneAddressFile (info ); err != nil {
148- log .DefaultLogger .Error ("Error while cleaning up standalone address file" , "error" , err )
149- }
150- if err := standalone .CleanupStandalonePIDFile (info ); err != nil {
151- log .DefaultLogger .Error ("Error while cleaning up standalone pid file" , "error" , err )
152- }
153- // Kill the dummy locator so Grafana reloads the plugin
154- standalone .FindAndKillCurrentPlugin (info .Dir )
155- }()
156-
157- // When debugging, be sure to kill the running instances, so we reconnect
148+ // Kill the dummy locator so Grafana reloads the plugin
158149 standalone .FindAndKillCurrentPlugin (info .Dir )
159- }
150+ }()
151+
152+ // When debugging, be sure to kill the running instances, so that we can reconnect
153+ standalone .FindAndKillCurrentPlugin (info .Dir )
160154
161155 // Start GRPC server
162156 pluginOpts := asGRPCServeOpts (dsopts )
@@ -242,21 +236,15 @@ func Manage(pluginID string, serveOpts ServeOpts) error {
242236 }
243237 }()
244238
245- info , err := standalone .GetInfo (pluginID )
246- if err != nil {
247- return err
248- }
249-
250- if info .Standalone {
239+ if s , enabled := standalone .ServerModeEnabled (pluginID ); enabled {
251240 // Run the standalone GRPC server
252- return GracefulStandaloneServe (serveOpts , info )
241+ return GracefulStandaloneServe (serveOpts , s )
253242 }
254243
255- if info .Address != "" && standalone .CheckPIDIsRunning (info .PID ) {
256- // Grafana is trying to run the dummy plugin locator to connect to the standalone
257- // GRPC server (separate process)
258- Logger .Debug ("Running dummy plugin locator" , "addr" , info .Address , "pid" , strconv .Itoa (info .PID ))
259- standalone .RunDummyPluginLocator (info .Address )
244+ if s , enabled := standalone .ClientModeEnabled (pluginID ); enabled {
245+ // Grafana is trying to run the dummy plugin locator to connect to the standalone GRPC server (separate process)
246+ Logger .Debug ("Running dummy plugin locator" , "addr" , s .TargetAddress , "pid" , strconv .Itoa (s .TargetPID ))
247+ standalone .RunDummyPluginLocator (s .TargetAddress )
260248 return nil
261249 }
262250
0 commit comments