Beispiel #1
0
func sleep(stmt *Stmt) (func(*Context) (interface{}, error), error) {
	return func(context *Context) (interface{}, error) {
		duration := time.Duration(util.MaybeInt(stmt.Arg("sleep", context))) * time.Millisecond
		timer := time.NewTimer(duration)
		for {
			select {
			case <-timer.C:
				return nil, nil
			case f := <-context.VM.StopChan:
				timer.Stop()
				f()
				return nil, nil
			}
		}
	}, nil
}
Beispiel #2
0
//NewStmt makes gohan statement from yaml node
func NewStmt(FileName string, node *yaml.Node) (stmt *Stmt, err error) {
	if node == nil {
		return nil, nil
	}
	stmt = &Stmt{}
	stmt.RawNode = MappingNodeToMap(node)
	stmt.Line = node.Line + 1
	stmt.Column = node.Column
	var rawData interface{}
	yaml.UnmarshalNode(node, &rawData)
	stmt.RawData = util.MaybeMap(convertMapformat(rawData))
	stmt.Name = util.MaybeString(stmt.RawData["name"])
	stmt.File, _ = filepath.Abs(FileName)
	stmt.Dir = filepath.Dir(stmt.File)
	stmt.Always, err = MakeStmts(FileName, stmt.RawNode["always"])
	if err != nil {
		return nil, stmt.Error(err)
	}
	stmt.Rescue, err = MakeStmts(FileName, stmt.RawNode["rescue"])
	if err != nil {
		return nil, stmt.Error(err)
	}
	stmt.ElseStmt, err = MakeStmts(FileName, stmt.RawNode["else"])
	if err != nil {
		return nil, stmt.Error(err)
	}
	stmt.Retry = util.MaybeInt(stmt.RawData["retries"])
	stmt.Worker = util.MaybeInt(stmt.RawData["worker"])
	stmt.LoopVar = util.MaybeString(stmt.RawData["loop_var"])
	if stmt.LoopVar == "" {
		stmt.LoopVar = "item"
	}
	if stmt.Retry == 0 {
		stmt.Retry = 1
	}
	stmt.Delay = util.MaybeInt(stmt.RawData["delay"])
	stmt.WithItems, err = NewValue(stmt.RawData["with_items"])
	if err != nil {
		return nil, stmt.Error(err)
	}
	stmt.WithDict, err = NewValue(stmt.RawData["with_dict"])
	if err != nil {
		return nil, stmt.Error(err)
	}
	if stmt.RawData["when"] != nil {
		stmt.When, err = CompileExpr(util.MaybeString(stmt.RawData["when"]))
		if err != nil {
			return nil, stmt.Error(err)
		}
	}
	if stmt.RawData["until"] != nil {
		stmt.Until, err = CompileExpr(util.MaybeString(stmt.RawData["until"]))
		if err != nil {
			return nil, stmt.Error(err)
		}
	}
	stmt.Register = util.MaybeString(stmt.RawData["register"])
	stmt.Vars, err = MapToValue(util.MaybeMap(stmt.RawData["vars"]))
	if err != nil {
		return nil, stmt.Error(err)
	}
	return stmt, nil
}
Beispiel #3
0
// MaybeInt returns a int
func (context *Context) MaybeInt(key string) int {
	return util.MaybeInt(context.MayGet(key))
}