Skip to content

Commit 7347450

Browse files
committed
systemdinstaller: add support for ServiceTypeNotify
1 parent 689b069 commit 7347450

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

os/systemdinstaller/systemdinstaller.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
type ServiceDefinition struct {
1414
serviceName string
15+
serviceType serviceType
1516
args []string
1617
description string
1718
docs []string
@@ -24,6 +25,14 @@ type ServiceDefinition struct {
2425
err error // if error reading selfAbsolutePath
2526
}
2627

28+
// affects how service manager gauges the service start success (which if fails, can make service manager restart the service)
29+
type serviceType string
30+
31+
const (
32+
ServiceTypeExec serviceType = "exec" // will consider the unit started immediately after the main service binary has been executed
33+
ServiceTypeNotify serviceType = "notify" // it is expected that the service sends a "READY=1" notification message
34+
)
35+
2736
type Option func(*ServiceDefinition)
2837

2938
func Service(serviceName string, description string, opts ...Option) ServiceDefinition {
@@ -40,6 +49,7 @@ func newService(serviceName string, description string, opts []Option, userServi
4049
selfAbsolutePath, err := currentExecutableNoFollowSymlink()
4150

4251
sf := ServiceDefinition{
52+
serviceType: ServiceTypeExec,
4353
userService: userService,
4454
serviceName: serviceName,
4555
description: description,
@@ -153,6 +163,7 @@ func serialize(sf ServiceDefinition) string {
153163
l("")
154164
l("[Service]")
155165
l("ExecStart=" + strings.Join(append([]string{sf.selfAbsolutePath}, sf.args...), " "))
166+
l("Type=" + string(sf.serviceType))
156167
l("WorkingDirectory=" + filepath.Dir(sf.selfAbsolutePath))
157168
l("Restart=always")
158169
l("RestartSec=10s")
@@ -181,6 +192,12 @@ func unitfilePath(sf ServiceDefinition) (string, error) {
181192
}
182193
}
183194

195+
func Type(typ serviceType) Option {
196+
return func(sf *ServiceDefinition) {
197+
sf.serviceType = typ
198+
}
199+
}
200+
184201
// FIXME(security): args are not shell escaped - DO NOT TAKE THIS FROM USER INPUT
185202
func Args(args ...string) Option {
186203
return func(sf *ServiceDefinition) {

os/systemdinstaller/systemdinstaller_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ WantedBy=multi-user.target
1919
2020
[Service]
2121
ExecStart=/home/dummy/testservice_amd64 start
22+
Type=exec
2223
WorkingDirectory=/home/dummy
2324
Restart=always
2425
RestartSec=10s
@@ -45,6 +46,7 @@ WantedBy=default.target
4546
4647
[Service]
4748
ExecStart=/home/dummy/testservice_amd64 start
49+
Type=exec
4850
WorkingDirectory=/home/dummy
4951
Restart=always
5052
RestartSec=10s
@@ -71,6 +73,7 @@ WantedBy=multi-user.target
7173
7274
[Service]
7375
ExecStart=/home/dummy/testservice_amd64 start
76+
Type=exec
7477
WorkingDirectory=/home/dummy
7578
Restart=always
7679
RestartSec=10s
@@ -90,6 +93,7 @@ WantedBy=multi-user.target
9093
9194
[Service]
9295
ExecStart=/home/dummy/testservice_amd64
96+
Type=exec
9397
WorkingDirectory=/home/dummy
9498
Restart=always
9599
RestartSec=10s
@@ -108,6 +112,7 @@ WantedBy=multi-user.target
108112
109113
[Service]
110114
ExecStart=/home/dummy/testservice_amd64
115+
Type=exec
111116
WorkingDirectory=/home/dummy
112117
Restart=always
113118
RestartSec=10s
@@ -129,6 +134,26 @@ WantedBy=multi-user.target
129134
130135
[Service]
131136
ExecStart=/home/dummy/testservice_amd64
137+
Type=exec
138+
WorkingDirectory=/home/dummy
139+
Restart=always
140+
RestartSec=10s
141+
`)
142+
}
143+
144+
func TestTypeNotify(t *testing.T) {
145+
sf := Service("testservice", "My cool service", Type(ServiceTypeNotify))
146+
sf = fixForTest(sf)
147+
148+
assert.Equal(t, serialize(sf), `[Unit]
149+
Description=My cool service
150+
151+
[Install]
152+
WantedBy=multi-user.target
153+
154+
[Service]
155+
ExecStart=/home/dummy/testservice_amd64
156+
Type=notify
132157
WorkingDirectory=/home/dummy
133158
Restart=always
134159
RestartSec=10s

0 commit comments

Comments
 (0)