Skip to content

Commit

Permalink
fix: 🐛 pass a context to worker creation functions
Browse files Browse the repository at this point in the history
- New* worker creation functions now take a context, that can be used as part of initialisation
- in Linux location sensor, check if a client can be created first
  • Loading branch information
joshuar committed Jun 13, 2024
1 parent 61ff2af commit aeca410
Show file tree
Hide file tree
Showing 22 changed files with 34 additions and 33 deletions.
6 changes: 3 additions & 3 deletions internal/agent/device_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.")

Expand Down
2 changes: 1 addition & 1 deletion internal/agent/runners.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/battery/battery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/cpu/loadAvgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/cpu/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/desktop/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/disk/diskRates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/disk/diskUsage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
21 changes: 11 additions & 10 deletions internal/linux/location/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package location

import (
"context"
"fmt"

"github.com/godbus/dbus/v5"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/mem/memUsage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/net/networkConnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/net/networkRates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/power/laptop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/power/powerProfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/power/powerState.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/power/screenLock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/problems/problems.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).",
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/system/hwmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.)",
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/system/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/system/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/user/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down

0 comments on commit aeca410

Please sign in to comment.