diff --git a/internal/agent/device_linux.go b/internal/agent/device_linux.go index 821c49865..6248b0f7e 100644 --- a/internal/agent/device_linux.go +++ b/internal/agent/device_linux.go @@ -28,7 +28,7 @@ import ( ) // workers is the list of sensor workers supported on Linux. -var workers = []func() (*linux.SensorWorker, error){ +var workers = []func(ctx context.Context) (*linux.SensorWorker, error){ apps.NewAppWorker, battery.NewBatteryWorker, cpu.NewLoadAvgWorker, @@ -58,11 +58,11 @@ func newDevice(_ context.Context) *linux.Device { // sensorWorkers initialises the list of workers for sensors and returns those // that are supported on this device. -func sensorWorkers() []Worker { +func sensorWorkers(ctx context.Context) []Worker { activeWorkers := make([]Worker, 0, len(workers)) for _, w := range workers { - worker, err := w() + worker, err := w(ctx) if err != nil { log.Warn().Err(err).Msg("Could not activate a worker.") diff --git a/internal/agent/runners.go b/internal/agent/runners.go index 1127fdbb6..629346cc7 100644 --- a/internal/agent/runners.go +++ b/internal/agent/runners.go @@ -59,7 +59,7 @@ type MQTTWorker interface { // runWorkers will call all the sensor worker functions that have been defined // for this device. func runWorkers(ctx context.Context, trk SensorTracker, reg sensor.Registry) { - workers := sensorWorkers() + workers := sensorWorkers(ctx) workers = append(workers, device.NewExternalIPUpdaterWorker(), device.NewVersionWorker()) outCh := make([]<-chan sensor.Details, 0, len(workers)) diff --git a/internal/linux/apps/apps.go b/internal/linux/apps/apps.go index dc5e2e006..a238ff6a0 100644 --- a/internal/linux/apps/apps.go +++ b/internal/linux/apps/apps.go @@ -103,7 +103,7 @@ func (w *worker) Sensors(ctx context.Context) ([]sensor.Details, error) { return sensors, nil } -func NewAppWorker() (*linux.SensorWorker, error) { +func NewAppWorker(_ context.Context) (*linux.SensorWorker, error) { // If we cannot find a portal interface, we cannot monitor the active app. portalDest, err := linux.FindPortal() if err != nil { diff --git a/internal/linux/battery/battery.go b/internal/linux/battery/battery.go index 3010706d8..6021552e5 100644 --- a/internal/linux/battery/battery.go +++ b/internal/linux/battery/battery.go @@ -515,7 +515,7 @@ func (w *worker) Events(ctx context.Context) chan sensor.Details { return sensor.MergeSensorCh(ctx, sensorCh...) } -func NewBatteryWorker() (*linux.SensorWorker, error) { +func NewBatteryWorker(_ context.Context) (*linux.SensorWorker, error) { return &linux.SensorWorker{ WorkerName: "Battery Sensors", WorkerDesc: "Sensors to track connected battery states.", diff --git a/internal/linux/cpu/loadAvgs.go b/internal/linux/cpu/loadAvgs.go index 2d3cac6bd..7608d8b28 100644 --- a/internal/linux/cpu/loadAvgs.go +++ b/internal/linux/cpu/loadAvgs.go @@ -70,7 +70,7 @@ func (w *loadAvgsSensorWorker) Sensors(ctx context.Context, _ time.Duration) ([] return sensors, nil } -func NewLoadAvgWorker() (*linux.SensorWorker, error) { +func NewLoadAvgWorker(_ context.Context) (*linux.SensorWorker, error) { return &linux.SensorWorker{ WorkerName: "Load Average Sensors", WorkerDesc: "The canonical 1min, 5min and 15min load averages.", diff --git a/internal/linux/cpu/usage.go b/internal/linux/cpu/usage.go index bf22732e3..538cde75d 100644 --- a/internal/linux/cpu/usage.go +++ b/internal/linux/cpu/usage.go @@ -51,7 +51,7 @@ func (w *usageWorker) Sensors(ctx context.Context, d time.Duration) ([]sensor.De return []sensor.Details{newSensor}, nil } -func NewUsageWorker() (*linux.SensorWorker, error) { +func NewUsageWorker(_ context.Context) (*linux.SensorWorker, error) { return &linux.SensorWorker{ WorkerName: "CPU Usage Sensor", WorkerDesc: "System CPU usage as a percentage.", diff --git a/internal/linux/desktop/desktop.go b/internal/linux/desktop/desktop.go index 39ec1fc28..78fc4a0b7 100644 --- a/internal/linux/desktop/desktop.go +++ b/internal/linux/desktop/desktop.go @@ -110,7 +110,7 @@ func (w *worker) Sensors(ctx context.Context) ([]sensor.Details, error) { return sensors, nil } -func NewDesktopWorker() (*linux.SensorWorker, error) { +func NewDesktopWorker(_ context.Context) (*linux.SensorWorker, error) { // If we cannot find a portal interface, we cannot monitor desktop settings. _, err := linux.FindPortal() if err != nil { diff --git a/internal/linux/disk/diskRates.go b/internal/linux/disk/diskRates.go index 478d9b154..01d02a761 100644 --- a/internal/linux/disk/diskRates.go +++ b/internal/linux/disk/diskRates.go @@ -245,7 +245,7 @@ func (w *ioWorker) Sensors(_ context.Context, duration time.Duration) ([]sensor. } //nolint:exhaustruct -func NewIOWorker() (*linux.SensorWorker, error) { +func NewIOWorker(_ context.Context) (*linux.SensorWorker, error) { worker := &ioWorker{ devices: make(map[diskstats.Device]*sensors), } diff --git a/internal/linux/disk/diskUsage.go b/internal/linux/disk/diskUsage.go index f79de2c2f..4d07f3bc6 100644 --- a/internal/linux/disk/diskUsage.go +++ b/internal/linux/disk/diskUsage.go @@ -93,7 +93,7 @@ func (w *usageWorker) Sensors(ctx context.Context, _ time.Duration) ([]sensor.De return sensors, nil } -func NewUsageWorker() (*linux.SensorWorker, error) { +func NewUsageWorker(_ context.Context) (*linux.SensorWorker, error) { return &linux.SensorWorker{ WorkerName: "Disk Usage Sensors", WorkerDesc: "Disk Space Usage.", diff --git a/internal/linux/location/location.go b/internal/linux/location/location.go index d3c8d25e2..5696c7b0d 100644 --- a/internal/linux/location/location.go +++ b/internal/linux/location/location.go @@ -8,6 +8,7 @@ package location import ( "context" + "fmt" "github.com/godbus/dbus/v5" "github.com/rs/zerolog/log" @@ -48,13 +49,6 @@ type worker struct { //nolint:exhaustruct func (w *worker) Setup(ctx context.Context) *dbusx.Watch { var err error - w.clientPath, err = dbusx.GetData[dbus.ObjectPath](ctx, dbusx.SystemBus, managerPath, geoclueInterface, getClientCall) - - if !w.clientPath.IsValid() || err != nil { - log.Error().Err(err).Msg("Could not set up a geoclue client.") - - return nil - } if err = dbusx.SetProp(ctx, dbusx.SystemBus, string(w.clientPath), geoclueInterface, desktopIDProp, preferences.AppID); err != nil { log.Error().Err(err).Msg("Could not set a geoclue client id.") @@ -123,12 +117,19 @@ func (w *worker) Sensors(_ context.Context) ([]sensor.Details, error) { return nil, linux.ErrUnimplemented } -//nolint:exhaustruct -func NewLocationWorker() (*linux.SensorWorker, error) { +func NewLocationWorker(ctx context.Context) (*linux.SensorWorker, error) { + clientPath, err := dbusx.GetData[dbus.ObjectPath](ctx, dbusx.SystemBus, managerPath, geoclueInterface, getClientCall) + + if !clientPath.IsValid() || err != nil { + return nil, fmt.Errorf("could not set up a geoclue client: %w", err) + } + return &linux.SensorWorker{ WorkerName: "Location Sensor", WorkerDesc: "Sensor for device location, from GeoClue.", - Value: &worker{}, + Value: &worker{ + clientPath: clientPath, + }, }, nil } diff --git a/internal/linux/mem/memUsage.go b/internal/linux/mem/memUsage.go index 2938e2f21..519ffd99d 100644 --- a/internal/linux/mem/memUsage.go +++ b/internal/linux/mem/memUsage.go @@ -136,7 +136,7 @@ func (w *usageWorker) Sensors(ctx context.Context, _ time.Duration) ([]sensor.De return sensors, nil } -func NewUsageWorker() (*linux.SensorWorker, error) { +func NewUsageWorker(_ context.Context) (*linux.SensorWorker, error) { return &linux.SensorWorker{ WorkerName: "Memory Usage Sensor", WorkerDesc: "System RAM (and swap if enabled) usage as a percentage.", diff --git a/internal/linux/net/networkConnection.go b/internal/linux/net/networkConnection.go index a40452bbd..0932d2d35 100644 --- a/internal/linux/net/networkConnection.go +++ b/internal/linux/net/networkConnection.go @@ -498,7 +498,7 @@ func (w *connectionsWorker) Events(ctx context.Context) chan sensor.Details { } //nolint:exhaustruct -func NewConnectionWorker() (*linux.SensorWorker, error) { +func NewConnectionWorker(_ context.Context) (*linux.SensorWorker, error) { return &linux.SensorWorker{ WorkerName: "Network Connection Sensors", WorkerDesc: "Sensors to track network connection states and other connection specific properties.", diff --git a/internal/linux/net/networkRates.go b/internal/linux/net/networkRates.go index 2fb98358b..4b010dc10 100644 --- a/internal/linux/net/networkRates.go +++ b/internal/linux/net/networkRates.go @@ -154,7 +154,7 @@ func (w *ratesWorker) Sensors(ctx context.Context, duration time.Duration) ([]se return []sensor.Details{w.bytesRx, w.bytesTx, w.bytesRxRate, w.bytesTxRate}, nil } -func NewRatesWorker() (*linux.SensorWorker, error) { +func NewRatesWorker(_ context.Context) (*linux.SensorWorker, error) { return &linux.SensorWorker{ WorkerName: "Network Rates Sensors", WorkerDesc: "Network transfer amount and speed sensors.", diff --git a/internal/linux/power/laptop.go b/internal/linux/power/laptop.go index 9301c9789..db3f8c116 100644 --- a/internal/linux/power/laptop.go +++ b/internal/linux/power/laptop.go @@ -165,7 +165,7 @@ func (w *laptopWorker) Sensors(ctx context.Context) ([]sensor.Details, error) { return sensors, nil } -func NewLaptopWorker() (*linux.SensorWorker, error) { +func NewLaptopWorker(_ context.Context) (*linux.SensorWorker, error) { if linux.Chassis() != "laptop" { return nil, ErrUnsupportedHardware } diff --git a/internal/linux/power/powerProfile.go b/internal/linux/power/powerProfile.go index 48668ac86..671107852 100644 --- a/internal/linux/power/powerProfile.go +++ b/internal/linux/power/powerProfile.go @@ -111,7 +111,7 @@ func (w *profileWorker) Sensors(ctx context.Context) ([]sensor.Details, error) { return []sensor.Details{newPowerSensor(linux.SensorPowerProfile, profile)}, nil } -func NewProfileWorker() (*linux.SensorWorker, error) { +func NewProfileWorker(_ context.Context) (*linux.SensorWorker, error) { return &linux.SensorWorker{ WorkerName: "Power Profile Sensor", WorkerDesc: "Sensor to track the current power profile.", diff --git a/internal/linux/power/powerState.go b/internal/linux/power/powerState.go index d982b0d37..ba0afe05a 100644 --- a/internal/linux/power/powerState.go +++ b/internal/linux/power/powerState.go @@ -142,7 +142,7 @@ func (w *stateWorker) Sensors(_ context.Context) ([]sensor.Details, error) { return []sensor.Details{newPowerState(shutdown, false)}, nil } -func NewStateWorker() (*linux.SensorWorker, error) { +func NewStateWorker(_ context.Context) (*linux.SensorWorker, error) { return &linux.SensorWorker{ WorkerName: "Power State Sensor", WorkerDesc: "Sensor to track the current power state of the device.", diff --git a/internal/linux/power/screenLock.go b/internal/linux/power/screenLock.go index a3b43b152..bd415cd28 100644 --- a/internal/linux/power/screenLock.go +++ b/internal/linux/power/screenLock.go @@ -100,7 +100,7 @@ func (w *screenLockWorker) Sensors(_ context.Context) ([]sensor.Details, error) return nil, linux.ErrUnimplemented } -func NewScreenLockWorker() (*linux.SensorWorker, error) { +func NewScreenLockWorker(_ context.Context) (*linux.SensorWorker, error) { return &linux.SensorWorker{ WorkerName: "Screen Lock Sensor", WorkerDesc: "Sensor to track whether the screen is currently locked.", diff --git a/internal/linux/problems/problems.go b/internal/linux/problems/problems.go index dd4206249..5b1490343 100644 --- a/internal/linux/problems/problems.go +++ b/internal/linux/problems/problems.go @@ -115,7 +115,7 @@ func (w *worker) Sensors(ctx context.Context, _ time.Duration) ([]sensor.Details return nil, nil } -func NewProblemsWorker() (*linux.SensorWorker, error) { +func NewProblemsWorker(_ context.Context) (*linux.SensorWorker, error) { return &linux.SensorWorker{ WorkerName: "ABRT Problems Sensor", WorkerDesc: "Count of problems detected by ABRT (with details in sensor attributes).", diff --git a/internal/linux/system/hwmon.go b/internal/linux/system/hwmon.go index 88d773c33..162e94269 100644 --- a/internal/linux/system/hwmon.go +++ b/internal/linux/system/hwmon.go @@ -129,7 +129,7 @@ func (w *hwMonWorker) Sensors(_ context.Context, _ time.Duration) ([]sensor.Deta return sensors, nil } -func NewHWMonWorker() (*linux.SensorWorker, error) { +func NewHWMonWorker(_ context.Context) (*linux.SensorWorker, error) { return &linux.SensorWorker{ WorkerName: "HWMon Sensors", WorkerDesc: "Sensors from the hwmon kernel interface (chip temps, fan speeds, etc.)", diff --git a/internal/linux/system/info.go b/internal/linux/system/info.go index 1bc9ecf2e..f3d07968e 100644 --- a/internal/linux/system/info.go +++ b/internal/linux/system/info.go @@ -49,7 +49,7 @@ func (w *infoWorker) Sensors(_ context.Context) ([]sensor.Details, error) { nil } -func NewInfoWorker() (*linux.SensorWorker, error) { +func NewInfoWorker(_ context.Context) (*linux.SensorWorker, error) { return &linux.SensorWorker{ WorkerName: "System Info Sensors", WorkerDesc: "Sensors for kernel version, and Distribution name and version.", diff --git a/internal/linux/system/time.go b/internal/linux/system/time.go index 62b83a92e..014966932 100644 --- a/internal/linux/system/time.go +++ b/internal/linux/system/time.go @@ -80,7 +80,7 @@ func (w *timeWorker) Sensors(ctx context.Context, _ time.Duration) ([]sensor.Det nil } -func NewTimeWorker() (*linux.SensorWorker, error) { +func NewTimeWorker(_ context.Context) (*linux.SensorWorker, error) { return &linux.SensorWorker{ WorkerName: "Time Sensors", WorkerDesc: "Sensors for uptime and boottime.", diff --git a/internal/linux/user/users.go b/internal/linux/user/users.go index 310020631..a341cd439 100644 --- a/internal/linux/user/users.go +++ b/internal/linux/user/users.go @@ -133,7 +133,7 @@ func (w *worker) Sensors(ctx context.Context) ([]sensor.Details, error) { return []sensor.Details{w.sensor}, err } -func NewUserWorker() (*linux.SensorWorker, error) { +func NewUserWorker(_ context.Context) (*linux.SensorWorker, error) { return &linux.SensorWorker{ WorkerName: "User count sensor", WorkerDesc: "Sensors for number of logged in users.",