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

add CancelJob() #426

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions dbus/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,11 @@ func (c *Conn) listJobsInternal(ctx context.Context) ([]JobStatus, error) {
return status, nil
}

// Cancel the specified job ID.
func (c *Conn) CancelJobContext(ctx context.Context, id uint32) error {
return c.sysobj.CallWithContext(ctx, "org.freedesktop.systemd1.Manager.CancelJob", 0, id).Store()
}

// Freeze the cgroup associated with the unit.
// Note that FreezeUnit and ThawUnit are only supported on systems running with cgroup v2.
func (c *Conn) FreezeUnit(ctx context.Context, unit string) error {
Expand Down
35 changes: 35 additions & 0 deletions dbus/methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1689,3 +1689,38 @@ func TestFreezer(t *testing.T) {

runStopUnit(t, conn, TrUnitProp{target, nil})
}

func TestCancel(t *testing.T) {
target := "cancelme.service"
conn := setupConn(t)
defer conn.Close()

setupUnit(target, conn, t)
linkUnit(target, conn, t)

reschan := make(chan string)
_, err := conn.StartUnit(target, "replace", reschan)
if err != nil {
t.Fatal(err)
}

ctx := context.Background()
units, err := conn.ListUnitsByNamesContext(ctx, []string{target})
if err != nil {
t.Fatal("couldn't list units ", err)
}

if err := conn.CancelJobContext(ctx, units[0].JobId); err != nil {
t.Fatal("couldn't cancel job ", err)
}

units, err = conn.ListUnitsByNamesContext(ctx, []string{target})
if err != nil {
t.Fatal("couldn't list units after cancel ", err)
}

job := <-reschan
if job != "canceled" {
t.Fatal("Job is not canceled:", job)
}
}
6 changes: 6 additions & 0 deletions fixtures/cancelme.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[Unit]
Description=cancel unit test

[Service]
ExecStartPre=/bin/sleep 400
ExecStart=/bin/sleep 400