Skip to content

Commit

Permalink
Tweak telemetry config daemon sync
Browse files Browse the repository at this point in the history
  • Loading branch information
ekerfelt committed May 23, 2024
1 parent 6cccd49 commit 0d64412
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 42 deletions.
35 changes: 16 additions & 19 deletions cli/cmd/encore/cmdutil/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ import (
daemonpb "encr.dev/proto/encore/daemon"
)

// DaemonOption is an option for connecting to the Encore daemon.
type DaemonOption func(*daemonOptions)
func IsDaemonRunning(ctx context.Context) bool {
socketPath, err := daemonSockPath()
if err != nil {
return false
}
if _, err := xos.SocketStat(socketPath); err == nil {
// The socket exists; check that it is responsive.
if cc, err := dialDaemon(ctx, socketPath); err == nil {
_ = cc.Close()
return true
}
// socket is not responding, remove it
_ = os.Remove(socketPath)
}
return false

type daemonOptions struct {
skipStart bool
}

var (
// SkipStart skips starting the daemon if it is not already running.
SkipStart DaemonOption = func(o *daemonOptions) {
o.skipStart = true
}
)

// ConnectDaemon returns a client connection to the Encore daemon.
// By default, it will start the daemon if it is not already running.
func ConnectDaemon(ctx context.Context, opts ...DaemonOption) daemonpb.DaemonClient {
var options daemonOptions
for _, o := range opts {
o(&options)
}
func ConnectDaemon(ctx context.Context) daemonpb.DaemonClient {
socketPath, err := daemonSockPath()
if err != nil {
fmt.Fprintln(os.Stderr, "fatal: ", err)
Expand Down Expand Up @@ -76,9 +76,6 @@ func ConnectDaemon(ctx context.Context, opts ...DaemonOption) daemonpb.DaemonCli
_ = os.Remove(socketPath)
}

if options.skipStart {
return nil
}
// Start the daemon.
if err := StartDaemonInBackground(ctx); err != nil {
Fatal("starting daemon: ", err)
Expand Down
30 changes: 13 additions & 17 deletions cli/cmd/encore/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,20 @@ func printTelemetryStatus() {
}

func updateTelemetry(ctx context.Context) {
err := func() error {
//
daemon := cmdutil.ConnectDaemon(ctx, cmdutil.SkipStart)
if daemon != nil {
// Update the telemetry config on the daemon if it is running
_, err := daemon.Telemetry(ctx, &daemonpb.TelemetryConfig{
AnonId: telemetry.GetAnonID(),
Enabled: telemetry.IsEnabled(),
Debug: telemetry.IsDebug(),
})
return err
} else {
// Update the telemetry config locally if the daemon is not running
return telemetry.SaveConfig()
// Update the telemetry config on the daemon if it is running
if cmdutil.IsDaemonRunning(ctx) {
daemon := cmdutil.ConnectDaemon(ctx)
_, err := daemon.Telemetry(ctx, &daemonpb.TelemetryConfig{
AnonId: telemetry.GetAnonID(),
Enabled: telemetry.IsEnabled(),
Debug: telemetry.IsDebug(),
})
if err != nil {
log.Debug().Err(err).Msgf("could not update daemon telemetry: %s", err)
}
}()
if err != nil {
log.Debug().Err(err).Msgf("could not update telemetry: %s", err)
}
if err := telemetry.SaveConfig(); err != nil {
log.Debug().Err(err).Msgf("could not save telemetry: %s", err)
}
}

Expand Down
7 changes: 1 addition & 6 deletions cli/daemon/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import (
)

func (s *Server) Telemetry(ctx context.Context, req *daemonpb.TelemetryConfig) (*emptypb.Empty, error) {
if telemetry.UpdateConfig(req.AnonId, req.Enabled, req.Debug) {
err := telemetry.SaveConfig()
if err != nil {
return nil, err
}
}
telemetry.UpdateConfig(req.AnonId, req.Enabled, req.Debug)
return new(emptypb.Empty), nil
}

0 comments on commit 0d64412

Please sign in to comment.