Example #1
0
func run(name string, cmd []string, w ...io.Writer) error {
	var c, q = context.WithTimeout(context.Background(), *timeout)
	defer q()

	p, er := process.New(name, strings.Join(cmd, " "), w...)
	if er != nil {
		return er
	}
	logger.Debugf("cmd: %s", cmd)
	if er := p.Execute(c); er != nil {
		return er
	}
	select {
	case <-c.Done():
		return fmt.Errorf("cmd %v timed out", p)
	case <-p.Exited():
		return p.Error()
	}
}
Example #2
0
func workers(conf *config.Config, c context.Context, quit context.CancelFunc) {
	var (
		port    = time.NewTicker(portInterval)
		restart = time.NewTicker(restartInterval)
		check   = time.NewTicker(checkInterval)
	)

	logger.Infof("Port update will run once every hour")
	logger.Infof("VPN restart will run once every day")

	trans, er := process.New("transmission", conf.Transmission.Command, os.Stdout)
	if er != nil {
		quit()
		logger.Fatalf(er.Error())
	}
	vpn, er := process.New("openvpn", conf.OpenVPN.Command, os.Stdout)
	if er != nil {
		quit()
		logger.Fatalf(er.Error())
	}

	if er := startProcesses(trans, vpn, conf, c); er != nil {
		quit()
		logger.Fatalf(er.Error())
	}
	portUpdate(conf, c)

	for {
		select {
		case <-c.Done():
			port.Stop()
			restart.Stop()
			trans.Stop()
			vpn.Stop()
			return
		case t := <-check.C:
			logger.Debugf("Checking transmission port at %v", t)
			if er := portCheck(trans, conf, c); er != nil {
				if er := restartProcesses(trans, vpn, conf, c); er != nil {
					port.Stop()
					restart.Stop()
					trans.Stop()
					vpn.Stop()
					logger.Fatalf(er.Error())
				}
			}
		case t := <-port.C:
			logger.Infof("Update of Transmission port at %v", t)
			if er := portUpdate(conf, c); er != nil {
				if er := restartProcesses(trans, vpn, conf, c); er != nil {
					port.Stop()
					restart.Stop()
					trans.Stop()
					vpn.Stop()
					logger.Fatalf(er.Error())
				}
			}
		case t := <-restart.C:
			logger.Infof("Restarting Transmission and OpenVPN at %v", t)
			if er := restartProcesses(trans, vpn, conf, c); er != nil {
				port.Stop()
				restart.Stop()
				trans.Stop()
				vpn.Stop()
				logger.Fatalf(er.Error())
			}
		}
	}
}