Skip to content

Commit

Permalink
Merge pull request #37 from TheCacophonyProject/add-dbus-timeout
Browse files Browse the repository at this point in the history
Start service earlier in the boot process and add timeout for service…
  • Loading branch information
CameronRP authored Jul 12, 2024
2 parents f33422a + 741c9bd commit 977ba8f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion _release/event-reporter.service
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[Unit]
Description=Cacophony Project Event Reporter
After=multi-user.target
Before=network.target

[Service]
Type=simple
Expand Down
23 changes: 21 additions & 2 deletions eventclient/eventclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,30 @@ func UploadEvents() error {
}

func eventsDbusCall(method string, params ...interface{}) ([]interface{}, error) {

// Retry mechanism with a maximum wait time of 10 seconds
maxWaitTime := 10 * time.Second
startTime := time.Now()

conn, err := dbus.SystemBus()
if err != nil {
return nil, err
}
obj := conn.Object("org.cacophony.Events", "/org/cacophony/Events")
call := obj.Call(method, 0, params...)
return call.Body, call.Err
for {
call := obj.Call(method, 0, params...)

if call.Err == nil {
return call.Body, call.Err
}

if dbusErr, ok := call.Err.(dbus.Error); ok && dbusErr.Name == "org.freedesktop.DBus.Error.ServiceUnknown" {
if time.Since(startTime) > maxWaitTime {
return nil, errors.New("dbus service not available within the timeout period")
}
time.Sleep(500 * time.Millisecond)
} else {
return nil, call.Err
}
}
}

0 comments on commit 977ba8f

Please sign in to comment.