Exemplo n.º 1
0
func (vs *VirtualServer) SendToQueue(f interface{}, args ...interface{}) error {
	rnd, err := rand.Chars(10, rand.NumberLetters, "go")
	if err != nil {
		return e.Forward(err)
	}

	job := job.NewSimple(vs.VirtualServerName+rnd, vs.Prio, f, args...)

	vs.status.Log(status.Verbose, "Add job %v for %v to queue %v.", job.Name(), vs.VirtualServerName, vs.Prio)

	err = vs.Queue.Add(job)
	if err != nil {
		return e.Forward(err)
	}

	vs.status.Log(status.Verbose, "Waiting job %v return.", job.Name())

	job.Returns(&err)
	if err != nil {
		return e.Forward(err)
	}

	vs.status.Log(status.Verbose, "Job %v returned.", job.Name())
	return nil
}
Exemplo n.º 2
0
func (d *Daemon) monitor() {
	go func() {
		d.monitoring = true
		var tries int
		unmonitor := true
	F:
		for {
			select {
			case <-time.After(d.Sleep):
				if d.Tries != 0 && tries > d.Tries+1 {
					d.monitoring = false
					if unmonitor {
						d.status.LogAndEmail(status.Normal, "", "Unmonitor daemon %v.", d.DaemonName)
						unmonitor = false
						continue F
					}
					continue F
				}
				d.changePid()
				if d.pid == -1 {
					d.status.LogAndEmail(status.Normal, "", "No pid file %v for %v.", d.PidFile, d.DaemonName)
					d.running = false
				} else {
					err := syscall.Kill(d.pid, 0)
					if err != nil && err.Error() == "no such process" {
						d.status.LogAndEmail(status.Normal, "", "%v process is gone.", d.DaemonName)
						d.running = false
					} else if err != nil {
						d.status.Log(status.Verbose, "Monitor failed for %v with error: %v", d.DaemonName, e.Trace(e.New(err)))
						d.running = false
					} else {
						d.running = true
					}
				}
				if d.running {
					err := d.ping()
					if e.Equal(err, ErrPingFailed) {
						d.running = false
					} else if err != nil && !e.Equal(err, ErrProtoNotSupported) && !e.Equal(err, ErrUrlIsEmpty) {
						d.status.Log(status.Verbose, "Monitor ping failed for %v with error: %v", d.DaemonName, e.Trace(e.Forward(err)))
						d.running = false
					}
					p, err := event.ProbeProcess(d.pid)
					if e.Equal(err, "process not found") {
						d.status.LogAndEmail(status.Normal, "", "%v process is gone.", d.DaemonName)
						d.running = false
					} else if err != nil {
						d.status.Log(status.Verbose, "ProbeCurrentProcess failed with error: %v", err)
					} else {
						d.SendEvent(p)
					}
				}
				if !d.running {
					tries++
					rnd, err := rand.Chars(10, rand.NumberLetters, "go")
					if err != nil {
						d.status.Log(status.Verbose, "Can't generate the job name for %v. Error: %v", d.DaemonName, e.Trace(e.Forward(err)))
						continue
					}
					job := job.NewSimple(d.DaemonName+rnd, d.Prio, func() error {
						err := d.restartProcess()
						if err != nil {
							d.status.LogAndEmail(status.Normal, "", "Can't restart process %v.", d.DaemonName)
							return e.Forward(err)
						}
						return nil
					})
					d.status.Log(status.Verbose, "Add job %v for %v to queue %v.", job.Name(), d.DaemonName, d.Prio)
					d.Queue.Add(job)
					d.status.Log(status.Verbose, "Waiting job %v return.", job.Name())
					job.Returns(&err)
					if err != nil {
						continue
					}
					d.status.LogAndEmail(status.Normal, "", "Restart command sent to %v.", d.DaemonName)
				} else {
					d.status.Log(status.Verbose, "%v process is running.", d.DaemonName)
					tries = 0
				}
			case <-d.killMonitor:
				d.status.Log(status.Normal, "Monitor %v stopped.", d.DaemonName)
				return
			}
		}
	}()
}