Skip to content

Commit b049d01

Browse files
committed
Merge branch 'release/0.9.2'
2 parents ac5a227 + 7c932cb commit b049d01

File tree

5 files changed

+111
-36
lines changed

5 files changed

+111
-36
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ func main() {
173173
}
174174
```
175175

176+
Cron example:
177+
```
178+
See example/cron_example.go
179+
```
180+
176181
## Contributors (unsorted)
177182

178183
- [Igor Dolzhikov](https://github.com/takama)

daemon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// license that can be found in the LICENSE file.
44

55
/*
6-
Package daemon 0.9.1 for use with Go (golang) services.
6+
Package daemon 0.9.2 for use with Go (golang) services.
77
88
Package daemon provides primitives for daemonization of golang services.
99
This package is not provide implementation of user daemon,

daemon_freebsd.go

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package daemon
22

3-
//#include <sys/types.h>
4-
//#include <sys/sysctl.h>
5-
import "C"
6-
73
import (
8-
"bytes"
94
"fmt"
105
"io/ioutil"
116
"os"
@@ -14,7 +9,6 @@ import (
149
"regexp"
1510
"strings"
1611
"text/template"
17-
"unsafe"
1812
)
1913

2014
// systemVRecord - standard record (struct) for linux systemV version of daemon package
@@ -76,36 +70,17 @@ func newDaemon(name, description string, dependencies []string) (Daemon, error)
7670
return &bsdRecord{name, description, dependencies}, nil
7771
}
7872

79-
// From https://github.com/VividCortex/godaemon/blob/master/daemon_freebsd.go (MIT)
80-
func execPath() (string, error) {
81-
PATH_MAX := 1024 // From <sys/syslimits.h>
82-
exePath := make([]byte, PATH_MAX)
83-
exeLen := C.size_t(len(exePath))
84-
85-
// Beware: sizeof(int) != sizeof(C.int)
86-
var mib [4]C.int
87-
// From <sys/sysctl.h>
88-
mib[0] = 1 // CTL_KERN
89-
mib[1] = 14 // KERN_PROC
90-
mib[2] = 12 // KERN_PROC_PATHNAME
91-
mib[3] = -1
92-
93-
status, err := C.sysctl((*C.int)(unsafe.Pointer(&mib[0])), 4, unsafe.Pointer(&exePath[0]), &exeLen, nil, 0)
94-
95-
if err != nil {
96-
return "", fmt.Errorf("sysctl: %v", err)
97-
}
98-
99-
// Not sure why this might happen with err being nil, but...
100-
if status != 0 {
101-
return "", fmt.Errorf("sysctl returned %d", status)
73+
func execPath() (name string, err error) {
74+
name = os.Args[0]
75+
if name[0] == '.' {
76+
name, err = filepath.Abs(name)
77+
if err == nil {
78+
name = filepath.Clean(name)
79+
}
80+
} else {
81+
name, err = exec.LookPath(filepath.Clean(name))
10282
}
103-
104-
// Convert from null-padded []byte to a clean string. (Can't simply cast!)
105-
exePathStringLen := bytes.Index(exePath, []byte{0})
106-
exePathString := string(exePath[:exePathStringLen])
107-
108-
return filepath.Clean(exePathString), nil
83+
return name, err
10984
}
11085

11186
// Check service is running

examples/cron/cron_job.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"os/signal"
8+
"syscall"
9+
"time"
10+
11+
"github.com/robfig/cron"
12+
"github.com/takama/daemon"
13+
)
14+
15+
const (
16+
// name of the service
17+
name = "cron_job"
18+
description = "Cron job service example"
19+
)
20+
21+
var stdlog, errlog *log.Logger
22+
23+
// Service is the daemon service struct
24+
type Service struct {
25+
daemon.Daemon
26+
}
27+
28+
func makeFile() {
29+
// create a simple file (current time).txt
30+
f, err := os.Create(fmt.Sprintf("%s/%s.txt", os.TempDir(), time.Now().Format(time.RFC3339)))
31+
if err != nil {
32+
log.Fatal(err)
33+
}
34+
defer f.Close()
35+
}
36+
37+
// Manage by daemon commands or run the daemon
38+
func (service *Service) Manage() (string, error) {
39+
40+
usage := "Usage: cron_job install | remove | start | stop | status"
41+
// If received any kind of command, do it
42+
if len(os.Args) > 1 {
43+
command := os.Args[1]
44+
switch command {
45+
case "install":
46+
return service.Install()
47+
case "remove":
48+
return service.Remove()
49+
case "start":
50+
return service.Start()
51+
case "stop":
52+
// No need to explicitly stop cron since job will be killed
53+
return service.Stop()
54+
case "status":
55+
return service.Status()
56+
default:
57+
return usage, nil
58+
}
59+
}
60+
// Set up channel on which to send signal notifications.
61+
// We must use a buffered channel or risk missing the signal
62+
// if we're not ready to receive when the signal is sent.
63+
interrupt := make(chan os.Signal, 1)
64+
signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM)
65+
66+
// Create a new cron manager
67+
c := cron.New()
68+
// Run makefile every min
69+
c.AddFunc("* * * * * *", makeFile)
70+
c.Start()
71+
// Waiting for interrupt by system signal
72+
killSignal := <-interrupt
73+
stdlog.Println("Got signal:", killSignal)
74+
return "Service exited", nil
75+
}
76+
77+
func init() {
78+
stdlog = log.New(os.Stdout, "", log.Ldate|log.Ltime)
79+
errlog = log.New(os.Stderr, "", log.Ldate|log.Ltime)
80+
}
81+
82+
func main() {
83+
srv, err := daemon.New(name, description)
84+
if err != nil {
85+
errlog.Println("Error: ", err)
86+
os.Exit(1)
87+
}
88+
service := &Service{srv}
89+
status, err := service.Manage()
90+
if err != nil {
91+
errlog.Println(status, "\nError: ", err)
92+
os.Exit(1)
93+
}
94+
fmt.Println(status)
95+
}

0 commit comments

Comments
 (0)