From 741c9bdb9c2d188d13d15498b97bd79ff4ce9a9e Mon Sep 17 00:00:00 2001 From: cam Date: Fri, 12 Jul 2024 15:20:03 +1200 Subject: [PATCH] Start service earlier in the boot process and add timeout for service if not found --- _release/event-reporter.service | 2 +- eventclient/eventclient.go | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/_release/event-reporter.service b/_release/event-reporter.service index ecf6acf..bad09e5 100644 --- a/_release/event-reporter.service +++ b/_release/event-reporter.service @@ -1,6 +1,6 @@ [Unit] Description=Cacophony Project Event Reporter -After=multi-user.target +Before=network.target [Service] Type=simple diff --git a/eventclient/eventclient.go b/eventclient/eventclient.go index 9841b7f..fc4e1c9 100644 --- a/eventclient/eventclient.go +++ b/eventclient/eventclient.go @@ -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 + } + } }