Skip to content

Commit

Permalink
Merge branch 'release/0.9.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
takama committed Oct 29, 2017
2 parents ac5a227 + 7c932cb commit b049d01
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 36 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ func main() {
}
```

Cron example:
```
See example/cron_example.go
```

## Contributors (unsorted)

- [Igor Dolzhikov](https://github.com/takama)
Expand Down
2 changes: 1 addition & 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.9.1 for use with Go (golang) services.
Package daemon 0.9.2 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
45 changes: 10 additions & 35 deletions daemon_freebsd.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package daemon

//#include <sys/types.h>
//#include <sys/sysctl.h>
import "C"

import (
"bytes"
"fmt"
"io/ioutil"
"os"
Expand All @@ -14,7 +9,6 @@ import (
"regexp"
"strings"
"text/template"
"unsafe"
)

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

// From https://github.com/VividCortex/godaemon/blob/master/daemon_freebsd.go (MIT)
func execPath() (string, error) {
PATH_MAX := 1024 // From <sys/syslimits.h>
exePath := make([]byte, PATH_MAX)
exeLen := C.size_t(len(exePath))

// Beware: sizeof(int) != sizeof(C.int)
var mib [4]C.int
// From <sys/sysctl.h>
mib[0] = 1 // CTL_KERN
mib[1] = 14 // KERN_PROC
mib[2] = 12 // KERN_PROC_PATHNAME
mib[3] = -1

status, err := C.sysctl((*C.int)(unsafe.Pointer(&mib[0])), 4, unsafe.Pointer(&exePath[0]), &exeLen, nil, 0)

if err != nil {
return "", fmt.Errorf("sysctl: %v", err)
}

// Not sure why this might happen with err being nil, but...
if status != 0 {
return "", fmt.Errorf("sysctl returned %d", status)
func execPath() (name string, err error) {
name = os.Args[0]
if name[0] == '.' {
name, err = filepath.Abs(name)
if err == nil {
name = filepath.Clean(name)
}
} else {
name, err = exec.LookPath(filepath.Clean(name))
}

// Convert from null-padded []byte to a clean string. (Can't simply cast!)
exePathStringLen := bytes.Index(exePath, []byte{0})
exePathString := string(exePath[:exePathStringLen])

return filepath.Clean(exePathString), nil
return name, err
}

// Check service is running
Expand Down
95 changes: 95 additions & 0 deletions examples/cron/cron_job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"

"github.com/robfig/cron"
"github.com/takama/daemon"
)

const (
// name of the service
name = "cron_job"
description = "Cron job service example"
)

var stdlog, errlog *log.Logger

// Service is the daemon service struct
type Service struct {
daemon.Daemon
}

func makeFile() {
// create a simple file (current time).txt
f, err := os.Create(fmt.Sprintf("%s/%s.txt", os.TempDir(), time.Now().Format(time.RFC3339)))
if err != nil {
log.Fatal(err)
}
defer f.Close()
}

// Manage by daemon commands or run the daemon
func (service *Service) Manage() (string, error) {

usage := "Usage: cron_job install | remove | start | stop | status"
// If received any kind of command, do it
if len(os.Args) > 1 {
command := os.Args[1]
switch command {
case "install":
return service.Install()
case "remove":
return service.Remove()
case "start":
return service.Start()
case "stop":
// No need to explicitly stop cron since job will be killed
return service.Stop()
case "status":
return service.Status()
default:
return usage, nil
}
}
// Set up channel on which to send signal notifications.
// We must use a buffered channel or risk missing the signal
// if we're not ready to receive when the signal is sent.
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM)

// Create a new cron manager
c := cron.New()
// Run makefile every min
c.AddFunc("* * * * * *", makeFile)
c.Start()
// Waiting for interrupt by system signal
killSignal := <-interrupt
stdlog.Println("Got signal:", killSignal)
return "Service exited", nil
}

func init() {
stdlog = log.New(os.Stdout, "", log.Ldate|log.Ltime)
errlog = log.New(os.Stderr, "", log.Ldate|log.Ltime)
}

func main() {
srv, err := daemon.New(name, description)
if err != nil {
errlog.Println("Error: ", err)
os.Exit(1)
}
service := &Service{srv}
status, err := service.Manage()
if err != nil {
errlog.Println(status, "\nError: ", err)
os.Exit(1)
}
fmt.Println(status)
}
File renamed without changes.

0 comments on commit b049d01

Please sign in to comment.