Skip to content

Commit

Permalink
check if using interface is the way to go in Daemon.Run
Browse files Browse the repository at this point in the history
  • Loading branch information
nbari committed Jan 6, 2018
1 parent 56d5f85 commit 3880e2a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions daemon.go
Expand Up @@ -37,6 +37,9 @@ func (d *Daemon) Run(p Process) (*process, error) {
// increment count by 1
atomic.AddUint32(&d.count, 1)

// to print remaininig seconds to start cmd == nil
d.process = p.GetProcess()

time.Sleep(time.Duration(d.cfg.Wait) * time.Second)

if d.process, err = p.Start(); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions process.go
Expand Up @@ -15,6 +15,7 @@ type Process interface {
Pid() int
Signal(sig syscall.Signal) error
Start() (*process, error)
GetProcess() *process
}

type process struct {
Expand Down Expand Up @@ -166,6 +167,11 @@ func (p *process) Signal(sig syscall.Signal) error {
return syscall.Kill(p.cmd.Process.Pid, sig)
}

// GetProccess
func (p *process) GetProcess() *process {
return p
}

// NewProcess return process instance
func NewProcess(cfg *Config) *process {
qch := make(chan struct{})
Expand All @@ -179,5 +185,6 @@ func NewProcess(cfg *Config) *process {
},
errch: make(chan error, 1),
quit: qch,
sTime: time.Now(),
}
}
6 changes: 4 additions & 2 deletions socket.go
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"strings"
"sync/atomic"
"time"

"github.com/nbari/violetear"
)
Expand Down Expand Up @@ -46,7 +47,7 @@ func (d *Daemon) HandleStatus(w http.ResponseWriter, r *http.Request) {
}

// only if process is running
if d.process != nil {
if d.process.cmd != nil {
status.Fpid = d.fpid
status.Pid = d.process.Pid()
if d.process.eTime.IsZero() {
Expand All @@ -55,7 +56,8 @@ func (d *Daemon) HandleStatus(w http.ResponseWriter, r *http.Request) {
status.Down = AbsSince(d.process.eTime)
}
} else {
status.Status = fmt.Sprintf("Waiting %d seconds before starting", d.cfg.Wait)
startin := d.process.sTime.Add(time.Duration(d.cfg.Wait) * time.Second)
status.Status = fmt.Sprintf("Starting in %0.1f seconds", startin.Sub(time.Now()).Seconds())
}

// return status in json
Expand Down

0 comments on commit 3880e2a

Please sign in to comment.