예제 #1
0
파일: watcher.go 프로젝트: banduk/apm
// 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
		}
	}()
}
예제 #2
0
파일: master.go 프로젝트: banduk/apm
func (master *Master) updateStatus(proc *process.Proc) {
	if proc.IsAlive() {
		proc.Status.SetStatus("running")
	} else {
		proc.Pid = -1
		proc.Status.SetStatus("stopped")
	}
}
예제 #3
0
파일: master.go 프로젝트: banduk/apm
// 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
}
예제 #4
0
파일: master.go 프로젝트: banduk/apm
// 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
}
예제 #5
0
파일: master.go 프로젝트: banduk/apm
func (master *Master) delete(proc *process.Proc) error {
	return proc.Delete()
}