Skip to content

Commit

Permalink
modified: cmd/immortal/main.go
Browse files Browse the repository at this point in the history
	deleted:    ctrl.go
	modified:   daemon.go
	modified:   fifo.go
  • Loading branch information
nbari committed Aug 2, 2016
1 parent 91ec09c commit eb0e8cf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 65 deletions.
11 changes: 4 additions & 7 deletions cmd/immortal/main.go
Expand Up @@ -44,16 +44,13 @@ func main() {

// create daemon
var daemon immortal.Immortal
daemon = immortal.New(cfg)

daemon.Fork()

// if ctrl create supervise dir
err = daemon.FiFo(&immortal.FIFO{})
daemon, err = immortal.New(cfg)
if err != nil {
log.Print(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

daemon.Fork()
daemon.Run()
daemon.Supervise()
}
21 changes: 0 additions & 21 deletions ctrl.go

This file was deleted.

63 changes: 35 additions & 28 deletions daemon.go
Expand Up @@ -23,7 +23,6 @@ type Immortal interface {
type Daemonizer interface {
Fork()
Run()
FiFo(f FIFOer) error
}

type Daemon struct {
Expand All @@ -39,31 +38,6 @@ type Daemon struct {
supDir string
}

func (self *Daemon) FiFo(f FIFOer) error {
if self.ctrl {
var ctrl = []string{"control", "ok"}
if self.Cwd != "" {
self.supDir = filepath.Join(self.Cwd, "supervise")
} else {
d, err := os.Getwd()
if err != nil {
return err
}
self.supDir = filepath.Join(d, "supervise")
}
for k, v := range ctrl {
ctrl[k] = filepath.Join(self.supDir, v)
}
for _, v := range ctrl {
err := f.Make(v)
if err != nil {
return err
}
}
}
return nil
}

func (self *Daemon) Run() {
if atomic.SwapUint32(&self.count, uint32(1)) != 0 {
log.Printf("PID: %d running", self.process.Pid)
Expand Down Expand Up @@ -168,7 +142,39 @@ func (self *Daemon) Run() {
}()
}

func New(cfg *Config) *Daemon {
func New(cfg *Config) (*Daemon, error) {
var supDir string
if cfg.Cwd != "" {
supDir = filepath.Join(cfg.Cwd, "supervise")
} else {
d, err := os.Getwd()
if err != nil {
return nil, err
}
supDir = filepath.Join(d, "supervise")
}

// if ctrl create supervise dir
if cfg.ctrl {
var ctrl = []string{"control", "ok"}
for _, v := range ctrl {
err := MakeFifo(filepath.Join(supDir, v))
if err != nil {
return nil, err
}
}

// lock
lock, err := os.Create(filepath.Join(supDir, "lock"))
if err != nil {
return nil, err
}
err = syscall.Flock(int(lock.Fd()), syscall.LOCK_EX+syscall.LOCK_NB)
if err != nil {
return nil, err
}
}

return &Daemon{
Config: cfg,
Control: &Control{
Expand All @@ -181,5 +187,6 @@ func New(cfg *Config) *Daemon {
logger: NewLogger(cfg),
},
Supervisor: &Sup{},
}
supDir: supDir,
}, nil
}
11 changes: 2 additions & 9 deletions fifo.go
Expand Up @@ -6,14 +6,7 @@ import (
"syscall"
)

type FIFOer interface {
Make(path string) error
Open(path string) (*os.File, error)
}

type FIFO struct{}

func (self *FIFO) Make(path string) error {
func MakeFifo(path string) error {
err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
if err != nil {
return err
Expand All @@ -26,7 +19,7 @@ func (self *FIFO) Make(path string) error {
return nil
}

func (self *FIFO) Open(path string) (*os.File, error) {
func OpenFifo(path string) (*os.File, error) {
f, err := os.OpenFile(path, os.O_RDWR, os.ModeNamedPipe)
if err != nil {
return nil, err
Expand Down

0 comments on commit eb0e8cf

Please sign in to comment.