Skip to content

Commit a5cf58b

Browse files
tych0Luap99
authored andcommitted
dbus: add CancelJob()
We'd like to be able to cancel jobs using this API, so add a wrapper for it. Signed-off-by: Tycho Andersen <tycho@tycho.pizza>
1 parent 903cabb commit a5cf58b

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

dbus/methods.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,11 @@ func (c *Conn) ListJobsContext(ctx context.Context) ([]JobStatus, error) {
718718
return storeSlice[JobStatus](c.sysobj.CallWithContext(ctx, "org.freedesktop.systemd1.Manager.ListJobs", 0).Store)
719719
}
720720

721+
// CancelJob cancels the specified job ID.
722+
func (c *Conn) CancelJob(ctx context.Context, id uint32) error {
723+
return c.sysobj.CallWithContext(ctx, "org.freedesktop.systemd1.Manager.CancelJob", 0, id).Store()
724+
}
725+
721726
// FreezeUnit freezes the cgroup associated with the unit.
722727
// Note that FreezeUnit and [Conn.ThawUnit] are only supported on systems running with cgroup v2.
723728
func (c *Conn) FreezeUnit(ctx context.Context, unit string) error {

dbus/methods_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,3 +1884,40 @@ func TestStopUnitReentrant(t *testing.T) {
18841884
t.Fatal("JobListener jobs leaked")
18851885
}
18861886
}
1887+
1888+
func TestCancel(t *testing.T) {
1889+
target := "cancelme.service"
1890+
conn := setupConn(t)
1891+
defer conn.Close()
1892+
1893+
setupUnit(target, conn, t)
1894+
linkUnit(target, conn, t)
1895+
1896+
reschan := make(chan string)
1897+
_, err := conn.StartUnit(target, "replace", reschan)
1898+
if err != nil {
1899+
t.Fatal(err)
1900+
}
1901+
1902+
units, err := conn.ListUnitsByNamesContext(t.Context(), []string{target})
1903+
if err != nil {
1904+
t.Fatal("couldn't list units ", err)
1905+
}
1906+
1907+
if err := conn.CancelJob(t.Context(), units[0].JobId); err != nil {
1908+
t.Fatal("couldn't cancel job ", err)
1909+
}
1910+
1911+
units, err = conn.ListUnitsByNamesContext(t.Context(), []string{target})
1912+
if err != nil {
1913+
t.Fatal("couldn't list units after cancel ", err)
1914+
}
1915+
if units[0].JobId != 0 {
1916+
t.Fatal("expected no active job after cancel, got JobId:", units[0].JobId)
1917+
}
1918+
1919+
job := <-reschan
1920+
if job != "canceled" {
1921+
t.Fatal("Job is not canceled:", job)
1922+
}
1923+
}

fixtures/cancelme.service

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[Unit]
2+
Description=cancel unit test
3+
4+
[Service]
5+
ExecStartPre=/bin/sleep 400
6+
ExecStart=/bin/sleep 400

0 commit comments

Comments
 (0)