Skip to content

Commit

Permalink
test 2
Browse files Browse the repository at this point in the history
Signed-off-by: Daichi Sakaue <[email protected]>
  • Loading branch information
yokaze committed Aug 8, 2024
1 parent ecd4b9a commit ec09de8
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,17 @@ func RestartService(ctx context.Context, name string) error {
return well.CommandContext(ctx, "systemctl", "restart", name+".service").Run()
}

// waitServiceStateChange waits until a service finishes its transient state.
func waitServiceStateChange(ctx context.Context, name string) error {
// waitServiceJob waits until a service finishes its transient state.
func waitServiceJob(ctx context.Context, name string) error {
var state []byte
var err error
// wait for a minute
for i := 0; i < 60; i++ {
state, err = well.CommandContext(ctx, "systemctl", "show", "-p", "ActiveState", "--value", name+".service").Output()
state, err = well.CommandContext(ctx, "systemctl", "list-jobs", name+".service", "--legend=false").Output()
if err != nil {
return err
}
switch strings.TrimSpace(string(state)) {
case "activating":
case "deactivating":
default:
if !strings.Contains(string(state), name+".service") {
return nil
}
select {
Expand All @@ -68,11 +65,15 @@ func waitServiceStateChange(ctx context.Context, name string) error {

// StopService stops the service.
func StopService(ctx context.Context, name string) error {
err := waitServiceStateChange(ctx, name)
err := waitServiceJob(ctx, name)
if err != nil {
return err
}
return well.CommandContext(ctx, "systemctl", "stop", name+".service").Run()
output, err := well.CommandContext(ctx, "systemctl", "stop", name+".service").CombinedOutput()
if err != nil {
return fmt.Errorf("output: %s, err: %w", string(output), err)
}
return nil
}

// DisableService disables the service.
Expand Down

0 comments on commit ec09de8

Please sign in to comment.