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 ExitTimeOut support for MacOS launchd #330

Open
wants to merge 1 commit into
base: master
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: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/kardianos/service

go 1.12

require golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211
require (
github.com/google/go-cmp v0.5.8 // indirect
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211 h1:9UQO31fZ+0aKQOFldThf7BKPMJTiBfWycGh/u3UoO88=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2 changes: 2 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ const (
optionLogOutputDefault = false
optionPrefix = "Prefix"
optionPrefixDefault = "application"
optionExitTimeOut = "ExitTimeOut"
optionExitTimeOutDefault = 0

optionRunWait = "RunWait"
optionReloadSignal = "ReloadSignal"
Expand Down
11 changes: 10 additions & 1 deletion service_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package service
import (
"errors"
"fmt"
"io"
"os"
"os/signal"
"os/user"
Expand Down Expand Up @@ -176,6 +177,10 @@ func (s *darwinLaunchdService) Install() error {
}
defer f.Close()

return s.render(f)
}

func (s *darwinLaunchdService) render(wr io.Writer) error {
path, err := s.execPath()
if err != nil {
return err
Expand All @@ -190,6 +195,7 @@ func (s *darwinLaunchdService) Install() error {
SessionCreate bool
StandardOutPath string
StandardErrorPath string
ExitTimeOut int
}{
Config: s.Config,
Path: path,
Expand All @@ -198,9 +204,10 @@ func (s *darwinLaunchdService) Install() error {
SessionCreate: s.Option.bool(optionSessionCreate, optionSessionCreateDefault),
StandardOutPath: stdOutPath,
StandardErrorPath: stdErrPath,
ExitTimeOut: s.Option.int(optionExitTimeOut, optionExitTimeOutDefault),
}

return s.template().Execute(f, to)
return s.template().Execute(wr, to)
}

func (s *darwinLaunchdService) Uninstall() error {
Expand Down Expand Up @@ -315,6 +322,8 @@ var launchdConfig = `<?xml version='1.0' encoding='UTF-8'?>
<string>{{html .UserName}}</string>{{end}}
{{if .ChRoot}}<key>RootDirectory</key>
<string>{{html .ChRoot}}</string>{{end}}
{{if .ExitTimeOut}}<key>ExitTimeOut</key>
<integer>{{.ExitTimeOut}}</integer>{{end}}
{{if .WorkingDirectory}}<key>WorkingDirectory</key>
<string>{{html .WorkingDirectory}}</string>{{end}}
<key>SessionCreate</key>
Expand Down
112 changes: 112 additions & 0 deletions service_darwin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2015 Daniel Theophanes.
// Use of this source code is governed by a zlib-style
// license that can be found in the LICENSE file.

//go:build darwin
// +build darwin

package service

import (
"bytes"
"testing"
"text/template"

"github.com/google/go-cmp/cmp"
)

func TestDarwinLaunchdRender(t *testing.T) {

base := Config{
Name: "ServiceName",
DisplayName: "ServiceDisplayName",
Description: "ServiceDescription",
Executable: "/usr/local/bin/executable",
WorkingDirectory: "/usr/local/bin",
}

makeConfig := func(option KeyValue) *Config {
cfg := base // copy base
cfg.Option = option
return &cfg
}

makePlist := func(cfg *Config, m map[string]interface{}) string {
functions := template.FuncMap{
"bool": func(v bool) string {
if v {
return "true"
}
return "false"
},
}
cm := make(map[string]interface{})
for k, v := range m {
cm[k] = v
}
cm[optionSessionCreate] = optionSessionCreateDefault
cm[optionKeepAlive] = optionKeepAliveDefault
cm[optionRunAtLoad] = optionRunAtLoadDefault
cm["Name"] = cfg.Name
cm["Path"] = cfg.Executable
cm["WorkingDirectory"] = cfg.WorkingDirectory
cm["StandardOutPath"] = "/var/log/" + cfg.Name + ".out.log"
cm["StandardErrorPath"] = "/var/log/" + cfg.Name + ".err.log"

tmpl := template.Must(template.New("").Funcs(functions).Parse(launchdConfig))
var buf bytes.Buffer
err := tmpl.Execute(&buf, cm)
if err != nil {
t.Fatal(err)
}
return buf.String()
}

tests := []struct {
name string
cfg *Config
plist string
}{
{
name: "nil options",
cfg: makeConfig(nil),
plist: makePlist(makeConfig(nil), nil),
},
{
name: "empty options",
cfg: makeConfig(map[string]interface{}{}),
plist: makePlist(makeConfig(nil), nil),
},
{
name: "with ExitTimeOut",
cfg: makeConfig(map[string]interface{}{
"ExitTimeOut": 30,
}),
plist: makePlist(makeConfig(nil), map[string]interface{}{
"ExitTimeOut": 30,
}),
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
svc, err := New(nil, tc.cfg)
if err != nil {
t.Fatal(err)
}
dsvc := svc.(*darwinLaunchdService)

var buf bytes.Buffer
err = dsvc.render(&buf)
if err != nil {
t.Fatal(err)
}
plist := buf.String()
diff := cmp.Diff(tc.plist, plist)
if diff != "" {
t.Error(diff)
}
})
}

}