Skip to content

Commit

Permalink
Merge branch 'release/v0.12.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
takama committed Apr 15, 2020
2 parents aa76b00 + 83159fd commit eee3878
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 9 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,41 @@ func main() {
}
```

### Service config file

Optionally, service config file can be retrieved or updated by calling
`GetTemplate() string` and `SetTemplate(string)` methods(except MS
Windows). Template will be a default Go Template(`"text/template"`).

If `SetTemplate` is not called, default template content will be used
while creating service.

|Variable | Description|
|---------|------------|
|Description| Description for service |
|Dependencies|Service dependencies|
|Name|Service name|
|Path|Path of service executable|
|Args|Arguments for service executable|

#### Example template(for linux systemv)

```ini
[Unit]
Description={{.Description}}
Requires={{.Dependencies}}
After={{.Dependencies}}

[Service]
PIDFile=/var/run/{{.Name}}.pid
ExecStartPre=/bin/rm -f /var/run/{{.Name}}.pid
ExecStart={{.Path}} {{.Args}}
Restart=on-failure

[Install]
WantedBy=multi-user.target
```

### Cron example

See `examples/cron/cron_job.go`
Expand Down
12 changes: 11 additions & 1 deletion daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.

/*
Package daemon 0.11.0 for use with Go (golang) services.
Package daemon v0.12.0 for use with Go (golang) services.
Package daemon provides primitives for daemonization of golang services.
This package is not provide implementation of user daemon,
Expand Down Expand Up @@ -157,8 +157,18 @@ package daemon

import "strings"

// Status constants.
const (
statNotInstalled = "Service not installed"
)

// Daemon interface has a standard set of methods/commands
type Daemon interface {
// GetTemplate - gets service config template
GetTemplate() string

// SetTemplate - sets service config template
SetTemplate(string) error

// Install the service into the system
Install(args ...string) (string, error)
Expand Down
13 changes: 12 additions & 1 deletion daemon_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (darwin *darwinRecord) Status() (string, error) {
}

if !darwin.isInstalled() {
return "Status could not defined", ErrNotInstalled
return statNotInstalled, ErrNotInstalled
}

statusAction, _ := darwin.checkRunning()
Expand All @@ -193,6 +193,17 @@ func (darwin *darwinRecord) Run(e Executable) (string, error) {
return runAction + " completed.", nil
}

// GetTemplate - gets service config template
func (linux *darwinRecord) GetTemplate() string {
return propertyList
}

// SetTemplate - sets service config template
func (linux *darwinRecord) SetTemplate(tplStr string) error {
propertyList = tplStr
return nil
}

var propertyList = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
Expand Down
13 changes: 12 additions & 1 deletion daemon_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (bsd *bsdRecord) Status() (string, error) {
}

if !bsd.isInstalled() {
return "Status could not defined", ErrNotInstalled
return statNotInstalled, ErrNotInstalled
}

statusAction, _ := bsd.checkRunning()
Expand All @@ -234,6 +234,17 @@ func (bsd *bsdRecord) Run(e Executable) (string, error) {
return runAction + " completed.", nil
}

// GetTemplate - gets service config template
func (linux *bsdRecord) GetTemplate() string {
return bsdConfig
}

// SetTemplate - sets service config template
func (linux *bsdRecord) SetTemplate(tplStr string) error {
bsdConfig = tplStr
return nil
}

var bsdConfig = `#!/bin/sh
#
# PROVIDE: {{.Name}}
Expand Down
13 changes: 12 additions & 1 deletion daemon_linux_systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (linux *systemDRecord) Status() (string, error) {
}

if !linux.isInstalled() {
return "Status could not defined", ErrNotInstalled
return statNotInstalled, ErrNotInstalled
}

statusAction, _ := linux.checkRunning()
Expand All @@ -199,6 +199,17 @@ func (linux *systemDRecord) Run(e Executable) (string, error) {
return runAction + " completed.", nil
}

// GetTemplate - gets service config template
func (linux *systemDRecord) GetTemplate() string {
return systemDConfig
}

// SetTemplate - sets service config template
func (linux *systemDRecord) SetTemplate(tplStr string) error {
systemDConfig = tplStr
return nil
}

var systemDConfig = `[Unit]
Description={{.Description}}
Requires={{.Dependencies}}
Expand Down
13 changes: 12 additions & 1 deletion daemon_linux_systemv.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (linux *systemVRecord) Status() (string, error) {
}

if !linux.isInstalled() {
return "Status could not defined", ErrNotInstalled
return statNotInstalled, ErrNotInstalled
}

statusAction, _ := linux.checkRunning()
Expand All @@ -207,6 +207,17 @@ func (linux *systemVRecord) Run(e Executable) (string, error) {
return runAction + " completed.", nil
}

// GetTemplate - gets service config template
func (linux *systemVRecord) GetTemplate() string {
return systemVConfig
}

// SetTemplate - sets service config template
func (linux *systemVRecord) SetTemplate(tplStr string) error {
systemVConfig = tplStr
return nil
}

var systemVConfig = `#! /bin/sh
#
# /etc/rc.d/init.d/{{.Name}}
Expand Down
13 changes: 12 additions & 1 deletion daemon_linux_upstart.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (linux *upstartRecord) Status() (string, error) {
}

if !linux.isInstalled() {
return "Status could not defined", ErrNotInstalled
return statNotInstalled, ErrNotInstalled
}

statusAction, _ := linux.checkRunning()
Expand All @@ -185,6 +185,17 @@ func (linux *upstartRecord) Run(e Executable) (string, error) {
return runAction + " completed.", nil
}

// GetTemplate - gets service config template
func (linux *upstartRecord) GetTemplate() string {
return upstatConfig
}

// SetTemplate - sets service config template
func (linux *upstartRecord) SetTemplate(tplStr string) error {
upstatConfig = tplStr
return nil
}

var upstatConfig = `# {{.Name}} {{.Description}}
description "{{.Description}}"
Expand Down
40 changes: 37 additions & 3 deletions daemon_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (windows *windowsRecord) Install(args ...string) (string, error) {
s, err := m.OpenService(windows.name)
if err == nil {
s.Close()
return installAction + failed, err
return installAction + failed, ErrAlreadyRunning
}

s, err = m.CreateService(windows.name, execp, mgr.Config{
Expand All @@ -65,6 +65,30 @@ func (windows *windowsRecord) Install(args ...string) (string, error) {
}
defer s.Close()

// set recovery action for service
// restart after 5 seconds for the first 3 times
// restart after 1 minute, otherwise
r := []mgr.RecoveryAction{
mgr.RecoveryAction{
Type: mgr.ServiceRestart,
Delay: 5000 * time.Millisecond,
},
mgr.RecoveryAction{
Type: mgr.ServiceRestart,
Delay: 5000 * time.Millisecond,
},
mgr.RecoveryAction{
Type: mgr.ServiceRestart,
Delay: 5000 * time.Millisecond,
},
mgr.RecoveryAction{
Type: mgr.ServiceRestart,
Delay: 60000 * time.Millisecond,
},
}
// set reset period as a day
s.SetRecoveryActions(r, uint32(86400))

return installAction + " completed.", nil
}

Expand Down Expand Up @@ -254,7 +278,7 @@ type serviceHandler struct {
executable Executable
}

func (sh *serviceHandler) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
func (sh *serviceHandler) Execute(args []string, r <-chan svc.ChangeRequest, changes chan <- svc.Status) (ssec bool, errno uint32) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue
changes <- svc.Status{State: svc.StartPending}

Expand All @@ -265,7 +289,7 @@ func (sh *serviceHandler) Execute(args []string, r <-chan svc.ChangeRequest, cha
sh.executable.Start()
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}

loop:
loop:
for {
select {
case <-tick:
Expand Down Expand Up @@ -318,3 +342,13 @@ func (windows *windowsRecord) Run(e Executable) (string, error) {

return runAction + " completed.", nil
}

// GetTemplate - gets service config template
func (linux *windowsRecord) GetTemplate() string {
return ""
}

// SetTemplate - sets service config template
func (linux *windowsRecord) SetTemplate(tplStr string) error {
return errors.New(fmt.Sprintf("templating is not supported for windows"))
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/takama/daemon

go 1.14

require (
github.com/robfig/cron v1.2.0
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4 h1:opSr2sbRXk5X5/givKrrKj9HXxFpW2sdCiP8MJSKLQY=
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

0 comments on commit eee3878

Please sign in to comment.