Skip to content

Commit 6d3149c

Browse files
authored
feat: add RestartService function to manage service restarts (#69)
1 parent 909dcbd commit 6d3149c

File tree

1 file changed

+94
-16
lines changed

1 file changed

+94
-16
lines changed

utils/systemctl/systemctl.go

Lines changed: 94 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ type Service struct {
5050
Running bool
5151
}
5252

53-
func ListServices(pattern string) ([]Service, error) {
53+
func ListServices(pattern string, wait ...time.Duration) ([]Service, error) {
54+
timeout := 30 * time.Second
55+
if len(wait) > 0 {
56+
timeout = wait[0]
57+
}
58+
5459
// connect to systemd
55-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
60+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
5661
defer cancel()
5762

5863
conn, err := dbus.NewSystemdConnectionContext(ctx)
@@ -95,9 +100,14 @@ func ListServices(pattern string) ([]Service, error) {
95100
return services, nil
96101
}
97102

98-
func IsServiceEnabled(name string) (bool, error) {
103+
func IsServiceEnabled(name string, wait ...time.Duration) (bool, error) {
104+
timeout := 30 * time.Second
105+
if len(wait) > 0 {
106+
timeout = wait[0]
107+
}
108+
99109
// connect to systemd
100-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
110+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
101111
defer cancel()
102112

103113
conn, err := dbus.NewSystemdConnectionContext(ctx)
@@ -119,9 +129,14 @@ func IsServiceEnabled(name string) (bool, error) {
119129
return false, nil
120130
}
121131

122-
func IsServiceRunning(name string) (bool, error) {
132+
func IsServiceRunning(name string, wait ...time.Duration) (bool, error) {
133+
timeout := 30 * time.Second
134+
if len(wait) > 0 {
135+
timeout = wait[0]
136+
}
137+
123138
// connect to systemd
124-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
139+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
125140
defer cancel()
126141

127142
conn, err := dbus.NewSystemdConnectionContext(ctx)
@@ -139,9 +154,14 @@ func IsServiceRunning(name string) (bool, error) {
139154
return property.Value.Value() == "active", nil
140155
}
141156

142-
func EnableService(nameOrPath string) error {
157+
func EnableService(nameOrPath string, wait ...time.Duration) error {
158+
timeout := 30 * time.Second
159+
if len(wait) > 0 {
160+
timeout = wait[0]
161+
}
162+
143163
// connect to systemd
144-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
164+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
145165
defer cancel()
146166

147167
conn, err := dbus.NewSystemdConnectionContext(ctx)
@@ -171,9 +191,14 @@ func EnableService(nameOrPath string) error {
171191
return nil
172192
}
173193

174-
func DisableService(name string) error {
194+
func DisableService(name string, wait ...time.Duration) error {
195+
timeout := 30 * time.Second
196+
if len(wait) > 0 {
197+
timeout = wait[0]
198+
}
199+
175200
// connect to systemd
176-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
201+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
177202
defer cancel()
178203

179204
conn, err := dbus.NewSystemdConnectionContext(ctx)
@@ -201,9 +226,14 @@ func DisableService(name string) error {
201226
return nil
202227
}
203228

204-
func StartService(name string) error {
229+
func StartService(name string, wait ...time.Duration) error {
230+
timeout := 30 * time.Second
231+
if len(wait) > 0 {
232+
timeout = wait[0]
233+
}
234+
205235
// connect to systemd
206-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
236+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
207237
defer cancel()
208238

209239
conn, err := dbus.NewSystemdConnectionContext(ctx)
@@ -232,9 +262,14 @@ func StartService(name string) error {
232262
return nil
233263
}
234264

235-
func StopService(name string) error {
265+
func StopService(name string, wait ...time.Duration) error {
266+
timeout := 30 * time.Second
267+
if len(wait) > 0 {
268+
timeout = wait[0]
269+
}
270+
236271
// connect to systemd
237-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
272+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
238273
defer cancel()
239274

240275
conn, err := dbus.NewSystemdConnectionContext(ctx)
@@ -263,9 +298,52 @@ func StopService(name string) error {
263298
return nil
264299
}
265300

266-
func ReloadDaemon() error {
301+
func RestartService(name string, wait ...time.Duration) error {
302+
timeout := 30 * time.Second
303+
if len(wait) > 0 {
304+
timeout = wait[0]
305+
}
306+
307+
// connect to systemd
308+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
309+
defer cancel()
310+
311+
conn, err := dbus.NewSystemdConnectionContext(ctx)
312+
if err != nil {
313+
return err
314+
}
315+
316+
defer conn.Close()
317+
318+
ch := make(chan string)
319+
_, err = conn.ReloadOrRestartUnitContext(ctx, name, "replace", ch)
320+
if err != nil {
321+
return err
322+
}
323+
324+
result := <-ch
325+
if result != ResultDone {
326+
err, ok := ErrorMap[result]
327+
if !ok {
328+
return ErrorUnknown
329+
}
330+
331+
return err
332+
}
333+
334+
_ = ReloadDaemon()
335+
336+
return nil
337+
}
338+
339+
func ReloadDaemon(wait ...time.Duration) error {
340+
timeout := 30 * time.Second
341+
if len(wait) > 0 {
342+
timeout = wait[0]
343+
}
344+
267345
// connect to systemd
268-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
346+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
269347
defer cancel()
270348

271349
conn, err := dbus.NewSystemdConnectionContext(ctx)

0 commit comments

Comments
 (0)