Skip to content

Commit 2cb20cd

Browse files
authored
Additional logging & local reader cleanup (#5238)
1 parent 4688c53 commit 2cb20cd

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

cli/module_build.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cli
33
import (
44
"archive/tar"
55
"compress/gzip"
6-
"context"
76
"fmt"
87
"io"
98
"net/url"
@@ -661,7 +660,7 @@ func ReloadModuleAction(c *cli.Context, args reloadModuleArgs) error {
661660
// reloadModuleAction is the testable inner reload logic.
662661
func reloadModuleAction(c *cli.Context, vc *viamClient, args reloadModuleArgs, logger logging.Logger) error {
663662
// TODO(RSDK-9727) it'd be nice for this to be a method on a viam client rather than taking one as an arg
664-
partID, err := resolvePartID(c.Context, args.PartID, args.CloudConfig)
663+
partID, err := resolvePartID(args.PartID, args.CloudConfig)
665664
if err != nil {
666665
return err
667666
}
@@ -831,14 +830,14 @@ func validateReloadableArchive(c *cli.Context, build *manifestBuildInfo) error {
831830
}
832831

833832
// resolvePartID takes an optional provided part ID (from partFlag), and an optional default viam.json, and returns a part ID to use.
834-
func resolvePartID(ctx context.Context, partIDFromFlag, cloudJSON string) (string, error) {
833+
func resolvePartID(partIDFromFlag, cloudJSON string) (string, error) {
835834
if len(partIDFromFlag) > 0 {
836835
return partIDFromFlag, nil
837836
}
838837
if len(cloudJSON) == 0 {
839838
return "", errors.New("no --part and no default json")
840839
}
841-
conf, err := config.ReadLocalConfig(ctx, cloudJSON, logging.NewLogger("config"))
840+
conf, err := config.ReadLocalConfig(cloudJSON, logging.NewLogger("config"))
842841
if err != nil {
843842
return "", err
844843
}

cli/module_reload_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ func TestRestartModule(t *testing.T) {
9393
func TestResolvePartId(t *testing.T) {
9494
c := newTestContext(t, map[string]any{})
9595
// empty flag, no path
96-
partID, err := resolvePartID(c.Context, c.String(generalFlagPartID), "")
96+
partID, err := resolvePartID(c.String(generalFlagPartID), "")
9797
test.That(t, err, test.ShouldNotBeNil)
9898
test.That(t, partID, test.ShouldBeEmpty)
9999

100100
// empty flag, fake path
101101
missingPath := filepath.Join(t.TempDir(), "MISSING.json")
102-
_, err = resolvePartID(c.Context, c.String(generalFlagPartID), missingPath)
102+
_, err = resolvePartID(c.String(generalFlagPartID), missingPath)
103103
test.That(t, err, test.ShouldNotBeNil)
104104

105105
// empty flag, valid path
@@ -108,13 +108,13 @@ func TestResolvePartId(t *testing.T) {
108108
test.That(t, err, test.ShouldBeNil)
109109
_, err = fi.WriteString(`{"cloud":{"app_address":"https://app.viam.com:443","id":"JSON-PART","secret":"SECRET"}}`)
110110
test.That(t, err, test.ShouldBeNil)
111-
partID, err = resolvePartID(c.Context, c.String(generalFlagPartID), path)
111+
partID, err = resolvePartID(c.String(generalFlagPartID), path)
112112
test.That(t, err, test.ShouldBeNil)
113113
test.That(t, partID, test.ShouldEqual, "JSON-PART")
114114

115115
// given flag, valid path
116116
c = newTestContext(t, map[string]any{generalFlagPartID: "FLAG-PART"})
117-
partID, err = resolvePartID(c.Context, c.String(generalFlagPartID), path)
117+
partID, err = resolvePartID(c.String(generalFlagPartID), path)
118118
test.That(t, err, test.ShouldBeNil)
119119
test.That(t, partID, test.ShouldEqual, "FLAG-PART")
120120
}

config/reader.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@ func Read(
329329

330330
// ReadLocalConfig reads a config from the given file but does not fetch any config from the remote servers.
331331
func ReadLocalConfig(
332-
ctx context.Context,
333332
filePath string,
334333
logger logging.Logger,
335334
) (*Config, error) {
@@ -338,7 +337,7 @@ func ReadLocalConfig(
338337
return nil, err
339338
}
340339

341-
return fromReader(ctx, filePath, bytes.NewReader(buf), logger, nil)
340+
return fromReaderLocal(filePath, bytes.NewReader(buf), logger)
342341
}
343342

344343
// FromReader reads a config from the given reader and specifies
@@ -353,14 +352,12 @@ func FromReader(
353352
return fromReader(ctx, originalPath, r, logger, conn)
354353
}
355354

356-
// fromReader reads a config from the given reader and specifies
355+
// fromReaderLocal reads a config from the given reader and specifies
357356
// where, if applicable, the file the reader originated from.
358-
func fromReader(
359-
ctx context.Context,
357+
func fromReaderLocal(
360358
originalPath string,
361359
r io.Reader,
362360
logger logging.Logger,
363-
conn rpc.ClientConn,
364361
) (*Config, error) {
365362
// First read and process config from disk
366363
unprocessedConfig := Config{
@@ -374,6 +371,23 @@ func fromReader(
374371
if err != nil {
375372
return nil, errors.Wrapf(err, "failed to process Config")
376373
}
374+
return cfgFromDisk, err
375+
}
376+
377+
// fromReader reads a config from the given reader and specifies
378+
// where, if applicable, the file the reader originated from.
379+
func fromReader(
380+
ctx context.Context,
381+
originalPath string,
382+
r io.Reader,
383+
logger logging.Logger,
384+
conn rpc.ClientConn,
385+
) (*Config, error) {
386+
// First read and process config from disk
387+
cfgFromDisk, err := fromReaderLocal(originalPath, r, logger)
388+
if err != nil {
389+
return nil, err
390+
}
377391

378392
if conn != nil && cfgFromDisk.Cloud != nil {
379393
cfg, err := readFromCloud(ctx, cfgFromDisk, nil, true, true, logger, conn)

web/server/entrypoint.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,10 @@ func RunServer(ctx context.Context, args []string, _ logging.Logger) (err error)
187187
var appConn rpc.ClientConn
188188

189189
// Read the config from disk and use it to initialize the remote logger.
190-
initialReadCtx, cancel := context.WithTimeout(ctx, time.Second*5)
191-
cfgFromDisk, err := config.ReadLocalConfig(initialReadCtx, argsParsed.ConfigFile, logger.Sublogger("config"))
190+
cfgFromDisk, err := config.ReadLocalConfig(argsParsed.ConfigFile, logger.Sublogger("config"))
192191
if err != nil {
193-
cancel()
194192
return err
195193
}
196-
cancel()
197194

198195
if argsParsed.OutputTelemetry {
199196
exporter := perf.NewDevelopmentExporter()
@@ -261,13 +258,14 @@ func RunServer(ctx context.Context, args []string, _ logging.Logger) (err error)
261258
// runServer is an entry point to starting the web server after the local config is read. Once the local config
262259
// is read the logger may be initialized to remote log. This ensure we capture errors starting up the server and report to the cloud.
263260
func (s *robotServer) runServer(ctx context.Context) error {
264-
initialReadCtx, cancel := context.WithTimeout(ctx, time.Second*5)
265-
cfg, err := config.Read(initialReadCtx, s.args.ConfigFile, s.logger, s.conn)
261+
if s.conn != nil {
262+
s.logger.CInfo(ctx, "Getting up-to-date config from cloud...")
263+
}
264+
// config.Read will add a timeout using contextutils.GetTimeoutCtx, so no need to add a separate timeout.
265+
cfg, err := config.Read(ctx, s.args.ConfigFile, s.logger, s.conn)
266266
if err != nil {
267-
cancel()
268267
return err
269268
}
270-
cancel()
271269
config.UpdateFileConfigDebug(cfg.Debug)
272270

273271
err = s.serveWeb(ctx, cfg)
@@ -503,7 +501,7 @@ func (s *robotServer) serveWeb(ctx context.Context, cfg *config.Config) (err err
503501
slowWatcherCancel()
504502
<-slowWatcher
505503
}()
506-
504+
s.logger.CInfo(ctx, "Processing initial robot config...")
507505
fullProcessedConfig, err := s.processConfig(cfg)
508506
if err != nil {
509507
return err

0 commit comments

Comments
 (0)