Skip to content

Commit

Permalink
refactor(linux): ⚡ simplify linux worker types
Browse files Browse the repository at this point in the history
- merge dbusType and eventType workers
  • Loading branch information
joshuar committed Jun 15, 2024
1 parent 988626a commit dee5815
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 159 deletions.
26 changes: 14 additions & 12 deletions internal/linux/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,21 @@ type worker struct {
}

//nolint:exhaustruct
func (w *worker) Setup(_ context.Context) (*dbusx.Watch, error) {
return &dbusx.Watch{
Bus: dbusx.SessionBus,
Path: appStateDBusPath,
Interface: appStateDBusInterface,
Names: []string{"RunningApplicationsChanged"},
},
nil
}

func (w *worker) Watch(ctx context.Context, triggerCh chan dbusx.Trigger) chan sensor.Details {
func (w *worker) Events(ctx context.Context) (chan sensor.Details, error) {
sensorCh := make(chan sensor.Details)

triggerCh, err := dbusx.WatchBus(ctx, &dbusx.Watch{
Bus: dbusx.SessionBus,
Path: appStateDBusPath,
Interface: appStateDBusInterface,
Names: []string{"RunningApplicationsChanged"},
})
if err != nil {
close(sensorCh)

return sensorCh, fmt.Errorf("could not watch D-Bus for app state events: %w", err)
}

sendSensors := func(ctx context.Context, sensorCh chan sensor.Details) {
appSensors, err := w.Sensors(ctx)
if err != nil {
Expand Down Expand Up @@ -80,7 +82,7 @@ func (w *worker) Watch(ctx context.Context, triggerCh chan dbusx.Trigger) chan s
sendSensors(ctx, sensorCh)
}()

return sensorCh
return sensorCh, nil
}

func (w *worker) Sensors(ctx context.Context) ([]sensor.Details, error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/linux/battery/battery.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ func (w *worker) Sensors(_ context.Context) ([]sensor.Details, error) {
}

//nolint:prealloc
func (w *worker) Events(ctx context.Context) chan sensor.Details {
func (w *worker) Events(ctx context.Context) (chan sensor.Details, error) {
batteryTracker := newBatteryTracker()

var sensorCh []<-chan sensor.Details
Expand All @@ -512,7 +512,7 @@ func (w *worker) Events(ctx context.Context) chan sensor.Details {
// Monitor for battery added/removed signals.
sensorCh = append(sensorCh, monitorBatteryChanges(ctx, batteryTracker))

return sensor.MergeSensorCh(ctx, sensorCh...)
return sensor.MergeSensorCh(ctx, sensorCh...), nil
}

func NewBatteryWorker() (*linux.SensorWorker, error) {
Expand Down
26 changes: 14 additions & 12 deletions internal/linux/desktop/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,21 @@ type desktopSettingSensor struct {
type worker struct{}

//nolint:exhaustruct
func (w *worker) Setup(_ context.Context) (*dbusx.Watch, error) {
return &dbusx.Watch{
Bus: dbusx.SessionBus,
Names: []string{settingsChangedSignal},
Interface: settingsPortalInterface,
Path: desktopPortalPath,
},
nil
}

func (w *worker) Watch(ctx context.Context, triggerCh chan dbusx.Trigger) chan sensor.Details {
func (w *worker) Events(ctx context.Context) (chan sensor.Details, error) {
sensorCh := make(chan sensor.Details)

triggerCh, err := dbusx.WatchBus(ctx, &dbusx.Watch{
Bus: dbusx.SessionBus,
Names: []string{settingsChangedSignal},
Interface: settingsPortalInterface,
Path: desktopPortalPath,
})
if err != nil {
close(sensorCh)

return sensorCh, fmt.Errorf("could not watch D-Bus for desktop settings updates: %w", err)
}

go func() {
defer close(sensorCh)

Expand Down Expand Up @@ -94,7 +96,7 @@ func (w *worker) Watch(ctx context.Context, triggerCh chan dbusx.Trigger) chan s
}
}()

return sensorCh
return sensorCh, nil
}

//nolint:mnd
Expand Down
22 changes: 18 additions & 4 deletions internal/linux/location/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type worker struct {
}

//nolint:exhaustruct
func (w *worker) Setup(ctx context.Context) (*dbusx.Watch, error) {
func (w *worker) setup(ctx context.Context) (*dbusx.Watch, error) {
var err error

// Check if we can create a client, bail if we can't.
Expand Down Expand Up @@ -88,9 +88,23 @@ func (w *worker) Setup(ctx context.Context) (*dbusx.Watch, error) {
nil
}

func (w *worker) Watch(ctx context.Context, triggerCh chan dbusx.Trigger) chan sensor.Details {
func (w *worker) Events(ctx context.Context) (chan sensor.Details, error) {
sensorCh := make(chan sensor.Details)

watch, err := w.setup(ctx)
if err != nil {
close(sensorCh)

return sensorCh, fmt.Errorf("could not setup D-Bus watch for location updates: %w", err)
}

triggerCh, err := dbusx.WatchBus(ctx, watch)
if err != nil {
close(sensorCh)

return sensorCh, fmt.Errorf("could not watch D-Bus for location updates: %w", err)
}

go func() {
defer close(sensorCh)

Expand All @@ -115,7 +129,7 @@ func (w *worker) Watch(ctx context.Context, triggerCh chan dbusx.Trigger) chan s
}
}()

return sensorCh
return sensorCh, nil
}

func (w *worker) Sensors(_ context.Context) ([]sensor.Details, error) {
Expand All @@ -126,7 +140,7 @@ func (w *worker) Sensors(_ context.Context) ([]sensor.Details, error) {
func NewLocationWorker() (*linux.SensorWorker, error) {
// Don't run this worker if we are not running on a laptop.
if linux.Chassis() != "laptop" {
return nil, linux.ErrUnsupportedHardware
return nil, fmt.Errorf("will not start location sensor: %w", linux.ErrUnsupportedHardware)
}

return &linux.SensorWorker{
Expand Down
9 changes: 4 additions & 5 deletions internal/linux/net/networkConnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package net

import (
"context"
"fmt"
"slices"
"sync"

Expand Down Expand Up @@ -432,7 +433,7 @@ func (w *connectionsWorker) Sensors(_ context.Context) ([]sensor.Details, error)
}

//nolint:exhaustruct,mnd
func (w *connectionsWorker) Events(ctx context.Context) chan sensor.Details {
func (w *connectionsWorker) Events(ctx context.Context) (chan sensor.Details, error) {
sensorCh := make(chan sensor.Details)
w.list = getActiveConnections(ctx)
handleConn := func(path dbus.ObjectPath) {
Expand All @@ -453,11 +454,9 @@ func (w *connectionsWorker) Events(ctx context.Context) chan sensor.Details {
Interface: dbusNMActiveConnIntr,
})
if err != nil {
log.Debug().Err(err).
Msg("Failed to create network connections D-Bus watch.")
close(sensorCh)

return sensorCh
return sensorCh, fmt.Errorf("failed to create network connections D-Bus watch: %w", err)
}

go func() {
Expand Down Expand Up @@ -494,7 +493,7 @@ func (w *connectionsWorker) Events(ctx context.Context) chan sensor.Details {
handleConn(path)
}

return sensorCh
return sensorCh, nil
}

//nolint:exhaustruct
Expand Down
41 changes: 20 additions & 21 deletions internal/linux/power/laptop.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,31 @@ func newLaptopEvent(prop string, state bool) *laptopSensor {
return sensorEvent
}

type laptopWorker struct {
sessionPath string
}
type laptopWorker struct{}

//nolint:cyclop,exhaustruct
func (w *laptopWorker) Events(ctx context.Context) (chan sensor.Details, error) {
sensorCh := make(chan sensor.Details)

//nolint:exhaustruct
func (w *laptopWorker) Setup(ctx context.Context) (*dbusx.Watch, error) {
// If we can't get a session path, we can't run.
sessionPath, err := dbusx.GetSessionPath(ctx)
if err != nil {
return nil, fmt.Errorf("could not create laptop worker: %w", err)
}
close(sensorCh)

w.sessionPath = sessionPath
return sensorCh, fmt.Errorf("could not create laptop worker: %w", err)
}

return &dbusx.Watch{
Bus: dbusx.SystemBus,
Names: []string{dbusx.PropChangedSignal},
Interface: managerInterface,
Path: w.sessionPath,
},
nil
}
triggerCh, err := dbusx.WatchBus(ctx, &dbusx.Watch{
Bus: dbusx.SystemBus,
Names: []string{dbusx.PropChangedSignal},
Interface: managerInterface,
Path: sessionPath,
})
if err != nil {
close(sensorCh)

func (w *laptopWorker) Watch(ctx context.Context, triggerCh chan dbusx.Trigger) chan sensor.Details {
sensorCh := make(chan sensor.Details)
return sensorCh, fmt.Errorf("could not watch D-Bus for laptop updates: %w", err)
}

go func() {
defer close(sensorCh)
Expand Down Expand Up @@ -150,7 +150,7 @@ func (w *laptopWorker) Watch(ctx context.Context, triggerCh chan dbusx.Trigger)
}
}()

return sensorCh
return sensorCh, nil
}

func (w *laptopWorker) Sensors(ctx context.Context) ([]sensor.Details, error) {
Expand All @@ -170,11 +170,10 @@ func (w *laptopWorker) Sensors(ctx context.Context) ([]sensor.Details, error) {
return sensors, nil
}

//nolint:exhaustruct
func NewLaptopWorker() (*linux.SensorWorker, error) {
// Don't run this worker if we are not running on a laptop.
if linux.Chassis() != "laptop" {
return nil, linux.ErrUnsupportedHardware
return nil, fmt.Errorf("will not create laptop sensors: %w", linux.ErrUnsupportedHardware)
}

return &linux.SensorWorker{
Expand Down
29 changes: 15 additions & 14 deletions internal/linux/power/powerProfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,34 @@ func newPowerSensor(sensorType linux.SensorTypeValue, sensorValue dbus.Variant)
type profileWorker struct{}

//nolint:exhaustruct
func (w *profileWorker) Setup(_ context.Context) (*dbusx.Watch, error) {
return &dbusx.Watch{
Bus: dbusx.SystemBus,
Names: []string{dbusx.PropChangedSignal},
Interface: dbusx.PropInterface,
Path: powerProfilesPath,
},
nil
}

func (w *profileWorker) Watch(ctx context.Context, triggerCh chan dbusx.Trigger) chan sensor.Details {
func (w *profileWorker) Events(ctx context.Context) (chan sensor.Details, error) {
sensorCh := make(chan sensor.Details)

// Check for power profile support, exit if not available. Otherwise, send
// an initial update.
sensors, err := w.Sensors(ctx)
if err != nil {
log.Warn().Err(err).Msg("Cannot monitor power profile.")
close(sensorCh)

return sensorCh
return sensorCh, fmt.Errorf("cannot retrieve power profile: %w", err)
}

go func() {
sensorCh <- sensors[0]
}()

triggerCh, err := dbusx.WatchBus(ctx, &dbusx.Watch{
Bus: dbusx.SystemBus,
Names: []string{dbusx.PropChangedSignal},
Interface: dbusx.PropInterface,
Path: powerProfilesPath,
})
if err != nil {
close(sensorCh)

return sensorCh, fmt.Errorf("could not watch D-Bus for power profile updates: %w", err)
}

// Watch for power profile changes.
go func() {
defer close(sensorCh)
Expand All @@ -96,7 +97,7 @@ func (w *profileWorker) Watch(ctx context.Context, triggerCh chan dbusx.Trigger)
}
}()

return sensorCh
return sensorCh, nil
}

func (w *profileWorker) Sensors(ctx context.Context) ([]sensor.Details, error) {
Expand Down
27 changes: 15 additions & 12 deletions internal/linux/power/powerState.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package power

import (
"context"
"fmt"

"github.com/rs/zerolog/log"

Expand Down Expand Up @@ -81,19 +82,21 @@ func newPowerState(signalName powerSignal, signalValue any) *powerStateSensor {
type stateWorker struct{}

//nolint:exhaustruct
func (w *stateWorker) Setup(_ context.Context) (*dbusx.Watch, error) {
return &dbusx.Watch{
Bus: dbusx.SystemBus,
Names: []string{sleepSignal, shutdownSignal},
Interface: managerInterface,
Path: loginBasePath,
},
nil
}

func (w *stateWorker) Watch(ctx context.Context, triggerCh chan dbusx.Trigger) chan sensor.Details {
func (w *stateWorker) Events(ctx context.Context) (chan sensor.Details, error) {
sensorCh := make(chan sensor.Details)

triggerCh, err := dbusx.WatchBus(ctx, &dbusx.Watch{
Bus: dbusx.SystemBus,
Names: []string{sleepSignal, shutdownSignal},
Interface: managerInterface,
Path: loginBasePath,
})
if err != nil {
close(sensorCh)

return sensorCh, fmt.Errorf("could not watch D-Bus for power state updates: %w", err)
}

// Watch for state changes.
go func() {
defer close(sensorCh)
Expand Down Expand Up @@ -133,7 +136,7 @@ func (w *stateWorker) Watch(ctx context.Context, triggerCh chan dbusx.Trigger) c
}
}()

return sensorCh
return sensorCh, nil
}

// Sensors returns the current sensors states. Assuming that if this is called,
Expand Down
Loading

0 comments on commit dee5815

Please sign in to comment.