Skip to content

Commit

Permalink
broken
Browse files Browse the repository at this point in the history
  • Loading branch information
nbari committed Aug 27, 2016
1 parent a6d719d commit 3b0b22b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 38 deletions.
3 changes: 1 addition & 2 deletions cmd/immortal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,5 @@ func main() {

log.Printf("%c %d", immortal.Logo(), os.Getpid())

daemon.Run(immortal.NewProcess(cfg))
immortal.Supervise(&immortal.Sup{}, daemon)
immortal.Supervise(daemon)
}
2 changes: 1 addition & 1 deletion daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type Return struct {
}

type Daemon struct {
count uint64
cfg *Config
count uint64
done chan error
fifo chan Return
fifo_control *os.File
Expand Down
22 changes: 10 additions & 12 deletions info.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"
)

func (self *Sup) Info(ch <-chan os.Signal, d *Daemon) {
func (d *Daemon) Info(ch <-chan os.Signal) {
for {
select {
case <-ch:
Expand All @@ -22,23 +22,21 @@ Frees: %d
Seconds in GC: %d
Started on: %v
Uptime: %v
Process uptime: %v
Process count: %d`
runtime.NumGoroutine()
s := new(runtime.MemStats)
runtime.ReadMemStats(s)
r := new(runtime.MemStats)
runtime.ReadMemStats(r)
log.Printf(status,
runtime.NumGoroutine(),
s.Alloc,
s.TotalAlloc,
s.Sys,
s.Lookups,
s.Mallocs,
s.Frees,
s.PauseTotalNs/1000000000,
r.Alloc,
r.TotalAlloc,
r.Sys,
r.Lookups,
r.Mallocs,
r.Frees,
r.PauseTotalNs/1000000000,
d.sTime.Format(time.RFC3339),
time.Since(d.sTime),
"d.Process.Uptime()",
d.count)
}
}
Expand Down
62 changes: 39 additions & 23 deletions supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package immortal

import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
Expand All @@ -10,6 +11,7 @@ import (
"os/signal"
"strconv"
"strings"
"sync/atomic"
"syscall"
"time"
)
Expand Down Expand Up @@ -76,42 +78,59 @@ func (self *Sup) ReadFifoControl(fifo *os.File, ch chan<- Return) {
}()
}

func Supervise(s Supervisor, d *Daemon) {
// listen on control for signals
if d.cfg.ctrl {
s.ReadFifoControl(d.fifo_control, d.fifo)
func Supervise(d *Daemon) {
p, err := d.Run(NewProcess(d.cfg))
if err != nil {
log.Fatal(err)
}

// info channel
// Info
info := make(chan os.Signal)
signal.Notify(info, syscall.SIGQUIT)
go s.Info(info, d)
go d.Info(info)

s := &Sup{p}

for {
time.Sleep(time.Second)
fmt.Println("waiting...")
}

// run loop
// listen on control for signals
if d.cfg.ctrl {
s.ReadFifoControl(d.fifo_control, d.fifo)
}

//run
run := make(chan struct{}, 1)

for {
select {
case <-d.quit:
return
case <-run:
time.Sleep(time.Second)
d.Run(NewProcess(d.cfg))
p, err := d.Run(NewProcess(d.cfg))
if err != nil {
log.Print(err)
}
s = &Sup{p}
default:
select {
case state := <-d.done:
if state != nil {
if exitError, ok := state.(*exec.ExitError); ok {
case err := <-p.errch:
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
atomic.StoreUint32(&d.lock, d.lock_once)
log.Printf("PID %d terminated, %s [%v user %v sys %s up]\n",
exitError.Pid(),
exitError,
exitError.UserTime(),
exitError.SystemTime(),
"d.process.Uptime()")
} else if state.Error() == "EXIT" {
//log.Printf("PID: %d Exited", s.p.Pid())
println("fix this")
time.Since(p.sTime),
)
} else if err.Error() == "EXIT" {
log.Printf("PID: %d Exited", p.Pid())
} else {
log.Print(state)
log.Print(err)
}
}

Expand All @@ -124,11 +143,8 @@ func Supervise(s Supervisor, d *Daemon) {
run <- struct{}{}
} else {
// check if pid in file is valid
// if pid > 1 && pid != p.Pid() && s.IsRunning(pid) {
if pid > 1 {
// set pid to new pid in file
// d.pid = pid fix this
// log.Printf("Watching pid %d on file: %s", d.pid, d.cfg.Pid.Follow)
if pid > 1 && pid != p.Pid() && s.IsRunning(pid) {
log.Printf("Watching pid %d on file: %s", pid, d.cfg.Pid.Follow)
go s.WatchPid(pid, d.done)
} else {
// if cmd exits or process is kill
Expand All @@ -142,7 +158,7 @@ func Supervise(s Supervisor, d *Daemon) {
if fifo.err != nil {
log.Printf("control error: %s", fifo.err)
}
go s.HandleSignals(fifo.msg, d)
s.HandleSignals(fifo.msg, d)
}
}
}
Expand Down

0 comments on commit 3b0b22b

Please sign in to comment.