Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbus: add helper methods for multiple start/stop with wait #415

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions dbus/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,39 @@ func (c *Conn) StartUnitContext(ctx context.Context, name string, mode string, c
return c.startJob(ctx, ch, "org.freedesktop.systemd1.Manager.StartUnit", name, mode)
}

// Starts Multiple units and waits for their success or failure before returning
func (c *Conn) StartMultipleUnitsAndWait(ctx context.Context, names []string, mode string) ([]string, []error) {
channels := make([]chan string, len(names))
res := make([]string, len(names))
errs := make([]error, len(names))
for i := range names {
channels[i] = make(chan string, 1)
}
for i, name := range names {
_, errs[i] = c.StartUnitContext(ctx, name, mode, channels[i])
if errs[i] != nil {
channels[i] = nil
}
}
for i, ch := range channels {
if ch == nil {
continue
}
select {
case status := <-ch:
res[i] = status
case <-ctx.Done():
errs[i] = fmt.Errorf("operation halted: %w", ctx.Err())
}
}
for _, err := range errs {
if err != nil {
return res, errs
}
}
return res, nil
}

// Deprecated: use StopUnitContext instead.
func (c *Conn) StopUnit(name string, mode string, ch chan<- string) (int, error) {
return c.StopUnitContext(context.Background(), name, mode, ch)
Expand All @@ -123,6 +156,39 @@ func (c *Conn) StopUnitContext(ctx context.Context, name string, mode string, ch
return c.startJob(ctx, ch, "org.freedesktop.systemd1.Manager.StopUnit", name, mode)
}

// Stops Multiple units and waits for their success or failure before returning
func (c *Conn) StopMultipleUnitsAndWait(ctx context.Context, names []string, mode string) ([]string, []error) {
channels := make([]chan string, len(names))
res := make([]string, len(names))
errs := make([]error, len(names))
for i := range names {
channels[i] = make(chan string, 1)
}
for i, name := range names {
_, errs[i] = c.StopUnitContext(ctx, name, mode, channels[i])
if errs[i] != nil {
channels[i] = nil
}
}
for i, ch := range channels {
if ch == nil {
continue
}
select {
case status := <-ch:
res[i] = status
case <-ctx.Done():
errs[i] = fmt.Errorf("operation halted: %w", ctx.Err())
}
}
for _, err := range errs {
if err != nil {
return res, errs
}
}
return res, nil
}

// Deprecated: use ReloadUnitContext instead.
func (c *Conn) ReloadUnit(name string, mode string, ch chan<- string) (int, error) {
return c.ReloadUnitContext(context.Background(), name, mode, ch)
Expand Down