f-15: fix: error in manage systemd
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
ff17645c62
commit
40b29657ae
1 changed files with 16 additions and 11 deletions
|
|
@ -11,6 +11,7 @@ import (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultTimeout = 5 * time.Second
|
defaultTimeout = 5 * time.Second
|
||||||
|
jobTimeout = 30 * time.Second
|
||||||
jobMode = "replace"
|
jobMode = "replace"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -28,10 +29,7 @@ type ServiceStatus struct {
|
||||||
|
|
||||||
// New crée une connexion D-Bus systemd (scope système)
|
// New crée une connexion D-Bus systemd (scope système)
|
||||||
func New() (*Manager, error) {
|
func New() (*Manager, error) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
|
conn, err := dbus.NewSystemConnectionContext(context.Background())
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
conn, err := dbus.NewSystemConnectionContext(ctx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -57,17 +55,17 @@ func (m *Manager) Stop(service string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) job(method, service string) error {
|
func (m *Manager) job(method, service string) error {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
|
callCtx, callCancel := context.WithTimeout(context.Background(), defaultTimeout)
|
||||||
defer cancel()
|
defer callCancel()
|
||||||
|
|
||||||
ch := make(chan string, 1)
|
ch := make(chan string, 1)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
switch method {
|
switch method {
|
||||||
case "StartUnit":
|
case "StartUnit":
|
||||||
_, err = m.conn.StartUnitContext(ctx, service, jobMode, ch)
|
_, err = m.conn.StartUnitContext(callCtx, service, jobMode, ch)
|
||||||
case "StopUnit":
|
case "StopUnit":
|
||||||
_, err = m.conn.StopUnitContext(ctx, service, jobMode, ch)
|
_, err = m.conn.StopUnitContext(callCtx, service, jobMode, ch)
|
||||||
default:
|
default:
|
||||||
return errors.New("unsupported job method")
|
return errors.New("unsupported job method")
|
||||||
}
|
}
|
||||||
|
|
@ -76,9 +74,16 @@ func (m *Manager) job(method, service string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
result := <-ch
|
waitCtx, waitCancel := context.WithTimeout(context.Background(), jobTimeout)
|
||||||
if result != "done" {
|
defer waitCancel()
|
||||||
return fmt.Errorf("%s %s failed: %s", method, service, result)
|
|
||||||
|
select {
|
||||||
|
case result := <-ch:
|
||||||
|
if result != "done" {
|
||||||
|
return fmt.Errorf("%s %s failed: %s", method, service, result)
|
||||||
|
}
|
||||||
|
case <-waitCtx.Done():
|
||||||
|
return fmt.Errorf("%s %s timed out after %s", method, service, jobTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue