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

Set explicit ExitTimeOut for MacOS agent launchd plist #594

Merged
merged 3 commits into from
Jun 23, 2022
Merged
Changes from 1 commit
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
63 changes: 61 additions & 2 deletions internal/pkg/agent/install/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package install

import (
"path/filepath"
"runtime"

"github.com/kardianos/service"

Expand All @@ -18,6 +19,12 @@ const (

// ServiceDescription is the description for the service.
ServiceDescription = "Elastic Agent is a unified agent to observe, monitor and protect your system."

// Set the launch daemon ExitTimeOut to 60 seconds in order to allow the agent to shutdown gracefully
// At the moment the version 8.3 & 8.4 of the agent are taking about 11 secs to shutdown
// and the launchd sends SIGKILL after 5 secs which causes the beats processes to be left running orphant
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// and the launchd sends SIGKILL after 5 secs which causes the beats processes to be left running orphant
// and the launchd sends SIGKILL after 5 secs which causes the beats processes to be left running orphaned

// depending on the shutdown timing.
darwinServiceExitTimeout = 60
)

// ExecutablePath returns the path for the installed Agents executable.
Expand All @@ -30,7 +37,7 @@ func ExecutablePath() string {
}

func newService() (service.Service, error) {
return service.New(nil, &service.Config{
cfg := &service.Config{
Name: paths.ServiceName,
DisplayName: ServiceDisplayName,
Description: ServiceDescription,
Expand All @@ -45,5 +52,57 @@ func newService() (service.Service, error) {
"OnFailureDelayDuration": "1s",
"OnFailureResetPeriod": 10,
},
})
}

if runtime.GOOS == "darwin" {
// The github.com/kardianos/service library doesn't support ExitTimeOut in their prebuilt template.
// This option allows to pass our own template for the launch daemon plist, which is a copy
// of the prebuilt template with added ExitTimeOut option
cfg.Option["LaunchdConfig"] = darwinLaunchdConfig
cfg.Option["ExitTimeOut"] = darwinServiceExitTimeout
}

return service.New(nil, cfg)
}

// A copy of the launchd plist template from github.com/kardianos/service
// with added .Config.Option.ExitTimeOut option
var darwinLaunchdConfig = `<?xml version='1.0' encoding='UTF-8'?>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a const?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep. the original lib had it as var heh

<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
<plist version='1.0'>
<dict>
<key>Label</key>
<string>{{html .Name}}</string>
<key>ProgramArguments</key>
<array>
<string>{{html .Path}}</string>
{{range .Config.Arguments}}
<string>{{html .}}</string>
{{end}}
</array>
{{if .UserName}}<key>UserName</key>
<string>{{html .UserName}}</string>{{end}}
{{if .ChRoot}}<key>RootDirectory</key>
<string>{{html .ChRoot}}</string>{{end}}
{{if .Config.Option.ExitTimeOut}}<key>ExitTimeOut</key>
<integer>{{html .Config.Option.ExitTimeOut}}</integer>{{end}}
{{if .WorkingDirectory}}<key>WorkingDirectory</key>
<string>{{html .WorkingDirectory}}</string>{{end}}
<key>SessionCreate</key>
<{{bool .SessionCreate}}/>
<key>KeepAlive</key>
<{{bool .KeepAlive}}/>
<key>RunAtLoad</key>
<{{bool .RunAtLoad}}/>
<key>Disabled</key>
<false/>

<key>StandardOutPath</key>
<string>/usr/local/var/log/{{html .Name}}.out.log</string>
<key>StandardErrorPath</key>
<string>/usr/local/var/log/{{html .Name}}.err.log</string>

</dict>
</plist>
`