Example #1
0
// WaitForStart delays until Docker appears to be up and running.
//
// Params:
// 	- client (*docker.Client): Docker client.
// 	- timeout (time.Duration): Time after which to give up.
//
// Returns:
// 	- boolean true if the server is up.
func WaitForStart(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
	if ok, missing := p.RequiresValue("client"); !ok {
		return nil, &cookoo.FatalError{"Missing required fields: " + strings.Join(missing, ", ")}
	}
	cli := p.Get("client", nil).(*docli.Client)
	timeout := p.Get("timeout", 30*time.Second).(time.Duration)

	keepon := true
	timer := time.AfterFunc(timeout, func() {
		keepon = false
	})

	for keepon == true {
		if err := cli.Ping(); err == nil {
			timer.Stop()
			log.Infof(c, "Docker is running.")
			return true, nil
		}
		time.Sleep(time.Second)
	}
	return false, fmt.Errorf("Docker timed out after waiting %s for server.", timeout)
}
Example #2
0
// Wait waits for a sync.WaitGroup to finish.
//
// Params:
// 	- wg (Waiter): The thing to wait for.
// 	- msg (string): The message to print when done. If this is empty, nothing is sent.
// 	- waiting (string): String to tell what we're waiting for. If empty, nothing is displayed.
// 	- failures (int): The number of failures that occurred while waiting.
//
// Returns:
//  Nothing.
func Wait(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
	ok, missing := p.RequiresValue("wg")
	if !ok {
		return nil, &cookoo.FatalError{"Missing required fields: " + strings.Join(missing, ", ")}
	}
	wg := p.Get("wg", nil).(Waiter)
	msg := p.Get("msg", "").(string)
	fails := p.Get("failures", 0).(int)
	waitmsg := p.Get("waiting", "").(string)

	if len(waitmsg) > 0 {
		log.Info(c, waitmsg)
	}

	wg.Wait()
	if len(msg) > 0 {
		log.Info(c, msg)
	}

	if fails > 0 {
		return nil, fmt.Errorf("There were %d failures while waiting.", fails)
	}
	return nil, nil
}