Skip to content

Commit 1ac580e

Browse files
author
Luca Bruno
authored
Merge pull request #336 from ardias/add-list-jobs-method
dbus: add ListJobs method
2 parents b51e752 + 15a173f commit 1ac580e

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

dbus/methods.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,3 +598,42 @@ func unitPath(name string) dbus.ObjectPath {
598598
func unitName(dpath dbus.ObjectPath) string {
599599
return pathBusUnescape(path.Base(string(dpath)))
600600
}
601+
602+
// Currently queued job definition
603+
type JobStatus struct {
604+
Id uint32 // The numeric job id
605+
Unit string // The primary unit name for this job
606+
JobType string // The job type as string
607+
Status string // The job state as string
608+
JobPath dbus.ObjectPath // The job object path
609+
UnitPath dbus.ObjectPath // The unit object path
610+
}
611+
612+
// ListJobs returns an array with all currently queued jobs
613+
func (c *Conn) ListJobs() ([]JobStatus, error) {
614+
return c.listJobsInternal()
615+
}
616+
617+
func (c *Conn) listJobsInternal() ([]JobStatus, error) {
618+
result := make([][]interface{}, 0)
619+
if err := c.sysobj.Call("org.freedesktop.systemd1.Manager.ListJobs", 0).Store(&result); err != nil {
620+
return nil, err
621+
}
622+
623+
resultInterface := make([]interface{}, len(result))
624+
for i := range result {
625+
resultInterface[i] = result[i]
626+
}
627+
628+
status := make([]JobStatus, len(result))
629+
statusInterface := make([]interface{}, len(status))
630+
for i := range status {
631+
statusInterface[i] = &status[i]
632+
}
633+
634+
if err := dbus.Store(resultInterface, statusInterface...); err != nil {
635+
return nil, err
636+
}
637+
638+
return status, nil
639+
}

dbus/methods_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ func runStopUnit(t *testing.T, conn *Conn, trTarget TrUnitProp) error {
132132
return nil
133133
}
134134

135+
func getJobStatusIfExists(jobs []JobStatus, jobName string) *JobStatus {
136+
for _, j := range jobs {
137+
if j.Unit == jobName {
138+
return &j
139+
}
140+
}
141+
return nil
142+
}
143+
144+
func isJobStatusEmpty(job JobStatus) bool {
145+
return job.Id == 0 && job.Unit == "" && job.JobType == "" && job.Status == "" && job.JobPath == "" && job.UnitPath == ""
146+
}
147+
135148
// Ensure that basic unit starting and stopping works.
136149
func TestStartStopUnit(t *testing.T) {
137150
target := "start-stop.service"
@@ -642,6 +655,54 @@ func TestEnableDisableUnit(t *testing.T) {
642655
}
643656
}
644657

658+
// Ensure that ListJobs works.
659+
func TestListJobs(t *testing.T) {
660+
service := "oneshot.service"
661+
662+
conn := setupConn(t)
663+
664+
setupUnit(service, conn, t)
665+
linkUnit(service, conn, t)
666+
667+
_, err := conn.StartUnit(service, "replace", nil)
668+
if err != nil {
669+
t.Fatal(err)
670+
}
671+
672+
jobs, err := conn.ListJobs()
673+
if err != nil {
674+
t.Skip(err)
675+
}
676+
677+
found := getJobStatusIfExists(jobs, service)
678+
if found == nil {
679+
t.Fatalf("%s job not found in list", service)
680+
}
681+
682+
if isJobStatusEmpty(*found) {
683+
t.Fatalf("empty %s job found in list", service)
684+
}
685+
686+
reschan := make(chan string)
687+
_, err = conn.StopUnit(service, "replace", reschan)
688+
if err != nil {
689+
t.Fatal(err)
690+
}
691+
692+
<-reschan
693+
694+
jobs, err = conn.ListJobs()
695+
696+
found = getJobStatusIfExists(jobs, service)
697+
if err != nil {
698+
t.Fatal(err)
699+
}
700+
701+
if found != nil {
702+
t.Fatalf("%s job found in list when it shouldn't", service)
703+
}
704+
}
705+
645706
// TestSystemState tests if system state is one of the valid states
646707
func TestSystemState(t *testing.T) {
647708
conn := setupConn(t)

fixtures/oneshot.service

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Unit]
2+
Description=start stop test
3+
4+
[Service]
5+
ExecStart=/bin/sh sleep 400
6+
ExecStopPost=/bin/sh sleep 1000
7+
Type=oneshot

0 commit comments

Comments
 (0)