// AddProcWatcher will add a watcher on proc. func (watcher *Watcher) AddProcWatcher(proc *process.Proc) { watcher.Lock() defer watcher.Unlock() if _, ok := watcher.watchProcs[proc.Name]; ok { log.Warnf("A watcher for this process already exists.") return } procWatcher := &ProcWatcher{ procStatus: make(chan *ProcStatus, 1), proc: proc, stopWatcher: make(chan bool, 1), } watcher.watchProcs[proc.Name] = procWatcher go func() { log.Infof("Starting watcher on proc %s", proc.Name) state, err := proc.Watch() procWatcher.procStatus <- &ProcStatus{ state: state, err: err, } }() go func() { defer delete(watcher.watchProcs, procWatcher.proc.Name) select { case procStatus := <-procWatcher.procStatus: log.Infof("Proc %s is dead, advising master...", procWatcher.proc.Name) log.Infof("State is %s", procStatus.state.String()) watcher.restartProc <- procWatcher.proc break case <-procWatcher.stopWatcher: break } }() }
func (master *Master) updateStatus(proc *process.Proc) { if proc.IsAlive() { proc.Status.SetStatus("running") } else { proc.Pid = -1 proc.Status.SetStatus("stopped") } }
// NOT thread safe method. Lock should be acquire before calling it. func (master *Master) start(proc *process.Proc) error { if !proc.IsAlive() { err := proc.Start() if err != nil { return err } master.Watcher.AddProcWatcher(proc) proc.Status.SetStatus("running") } return nil }
// NOT thread safe method. Lock should be acquire before calling it. func (master *Master) stop(proc *process.Proc) error { if proc.IsAlive() { waitStop := master.Watcher.StopWatcher(proc.Name) err := proc.GracefullyStop() if err != nil { return err } if waitStop != nil { <-waitStop proc.Pid = -1 proc.Status.SetStatus("stopped") } log.Infof("Proc %s sucessfully stopped.", proc.Name) } return nil }
func (master *Master) delete(proc *process.Proc) error { return proc.Delete() }