Skip to content

Commit

Permalink
organize config
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksanford committed Jul 12, 2024
1 parent 146876a commit 03e6342
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 48 deletions.
27 changes: 16 additions & 11 deletions data/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,36 @@ func NewCaptureManager(logger logging.Logger, clk clock.Clock) *CaptureManager {
}
}

// Config is the capture manager config.
type Config struct {
// CaptureConfig is the capture manager config.
type CaptureConfig struct {
CaptureDisabled bool
CaptureDir string
Tags []string
MaximumCaptureFileSizeBytes int64
}

// Reconfigure reconfigures the capture manager.
func (cm *CaptureManager) Reconfigure(ctx context.Context, deps resource.Dependencies, resConfig resource.Config, dataConfig Config) error {
captureConfigs, err := cm.updateDataCaptureConfigs(deps, resConfig, dataConfig.CaptureDir)
// ReconfigureCapture reconfigures the capture manager.
func (cm *CaptureManager) ReconfigureCapture(
ctx context.Context,
deps resource.Dependencies,
config resource.Config,
captureConfig CaptureConfig,
) error {
captureConfigs, err := cm.updateDataCaptureConfigs(deps, config, captureConfig.CaptureDir)
if err != nil {
return err
}

if !utils.IsTrustedEnvironment(ctx) && dataConfig.CaptureDir != "" && dataConfig.CaptureDir != viamCaptureDotDir {
if !utils.IsTrustedEnvironment(ctx) && captureConfig.CaptureDir != "" && captureConfig.CaptureDir != viamCaptureDotDir {
return ErrCaptureDirectoryConfigurationDisabled
}

if dataConfig.CaptureDir != "" {
cm.captureDir = dataConfig.CaptureDir
if captureConfig.CaptureDir != "" {
cm.captureDir = captureConfig.CaptureDir
} else {
cm.captureDir = viamCaptureDotDir
}
cm.captureDisabled = dataConfig.CaptureDisabled
cm.captureDisabled = captureConfig.CaptureDisabled
// Service is disabled, so close all collectors and clear the map so we can instantiate new ones if we enable this service.
if cm.captureDisabled {
cm.CloseCollectors()
Expand Down Expand Up @@ -154,13 +159,13 @@ func (cm *CaptureManager) Reconfigure(ctx context.Context, deps resource.Depende
// without it, we will be logging the same message over and over for no reason
cm.componentMethodFrequencyHz[componentMethodMetadata] = resConf.CaptureFrequencyHz

maxCaptureFileSize := dataConfig.MaximumCaptureFileSizeBytes
maxCaptureFileSize := captureConfig.MaximumCaptureFileSizeBytes
if maxCaptureFileSize == 0 {
maxCaptureFileSize = defaultMaxCaptureSize
}
if !resConf.Disabled && (resConf.CaptureFrequencyHz > 0 || cm.maxCaptureFileSize != maxCaptureFileSize) {
// We only use service-level tags.
resConf.Tags = dataConfig.Tags
resConf.Tags = captureConfig.Tags

maxFileSizeChanged := cm.maxCaptureFileSize != maxCaptureFileSize
cm.maxCaptureFileSize = maxCaptureFileSize
Expand Down
76 changes: 39 additions & 37 deletions services/datamanager/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,20 @@ var (

// Config describes how to configure the service.
type Config struct {
CaptureDir string `json:"capture_dir"`
AdditionalSyncPaths []string `json:"additional_sync_paths"`
SyncIntervalMins float64 `json:"sync_interval_mins"`
CaptureDisabled bool `json:"capture_disabled"`
ScheduledSyncDisabled bool `json:"sync_disabled"`
Tags []string `json:"tags"`
FileLastModifiedMillis int `json:"file_last_modified_millis"`
SelectiveSyncerName string `json:"selective_syncer_name"`
MaximumNumSyncThreads int `json:"maximum_num_sync_threads"`
DeleteEveryNthWhenDiskFull int `json:"delete_every_nth_when_disk_full"`
MaximumCaptureFileSizeBytes int64 `json:"maximum_capture_file_size_bytes"`
// Sync & Capture
CaptureDir string `json:"capture_dir"`
Tags []string `json:"tags"`
// Capture
CaptureDisabled bool `json:"capture_disabled"`
DeleteEveryNthWhenDiskFull int `json:"delete_every_nth_when_disk_full"`
MaximumCaptureFileSizeBytes int64 `json:"maximum_capture_file_size_bytes"`
// Sync
AdditionalSyncPaths []string `json:"additional_sync_paths"`
FileLastModifiedMillis int `json:"file_last_modified_millis"`
MaximumNumSyncThreads int `json:"maximum_num_sync_threads"`
ScheduledSyncDisabled bool `json:"sync_disabled"`
SelectiveSyncerName string `json:"selective_syncer_name"`
SyncIntervalMins float64 `json:"sync_interval_mins"`
}

// Validate returns components which will be depended upon weakly due to the above matcher.
Expand Down Expand Up @@ -105,39 +108,38 @@ func readyToSync(ctx context.Context, s selectiveSyncer, logger logging.Logger)

// builtIn initializes and orchestrates data capture collectors for registered component/methods.
type builtIn struct {
// Live for lifetime of builtIn
resource.Named
logger logging.Logger
lock sync.Mutex
closedCancelFn context.CancelFunc
closedCtx context.Context
lock sync.Mutex

// Capture
tags []string
captureManager *data.CaptureManager

// Sync
cloudConn rpc.ClientConn
cloudConnSvc cloud.ConnectionService
datasyncBackgroundWorkers sync.WaitGroup
fileLastModifiedMillis int
filesToSync chan string
maxSyncThreads int
propagateDataSyncConfigWG sync.WaitGroup
selectiveSyncEnabled bool
syncConfigUpdated bool
syncDisabled bool
syncIntervalMins float64
syncPaths []string
syncRoutineCancelFn context.CancelFunc
syncSensor selectiveSyncer
syncTicker *clk.Ticker
syncer datasync.Manager
syncerConstructor datasync.ManagerConstructor
syncerNeedsToBeReInitialized bool

fileDeletionRoutineCancelFn context.CancelFunc
cloudConn rpc.ClientConn
cloudConnSvc cloud.ConnectionService
datasyncBackgroundWorkers sync.WaitGroup
fileDeletionBackgroundWorkers *sync.WaitGroup

captureManager *data.CaptureManager
fileDeletionRoutineCancelFn context.CancelFunc
fileLastModifiedMillis int
filesToSync chan string
maxSyncThreads int
propagateDataSyncConfigWG sync.WaitGroup
selectiveSyncEnabled bool
syncConfigUpdated bool
syncDisabled bool
syncIntervalMins float64
syncPaths []string
syncRoutineCancelFn context.CancelFunc
syncSensor selectiveSyncer
syncTicker *clk.Ticker
syncer datasync.Manager
syncerConstructor datasync.ManagerConstructor
syncerNeedsToBeReInitialized bool
tags []string
}

// NewBuiltIn returns a new data manager service for the given robot.
Expand Down Expand Up @@ -264,13 +266,13 @@ func (svc *builtIn) Reconfigure(
return err
}

dataConfig := data.Config{
captureConfig := data.CaptureConfig{
CaptureDisabled: svcConfig.CaptureDisabled,
CaptureDir: svcConfig.CaptureDir,
Tags: svcConfig.Tags,
MaximumCaptureFileSizeBytes: svcConfig.MaximumCaptureFileSizeBytes,
}
if err = svc.captureManager.Reconfigure(ctx, deps, conf, dataConfig); err != nil {
if err = svc.captureManager.ReconfigureCapture(ctx, deps, conf, captureConfig); err != nil {
svc.logger.Warnw("DataCapture reconfigure error", "err", err)
return err
}
Expand Down

0 comments on commit 03e6342

Please sign in to comment.