Example #1
0
File: job.go Project: vozhyk-/gohan
func backgroundJob(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
	stmts, err := gohanscript.MakeStmts(stmt.File, stmt.RawNode["job"])
	if err != nil {
		return nil, stmt.Errorf("background code error: %s", err)
	}
	queueArg, err := gohanscript.NewValue(stmt.RawData["queue"])
	if err != nil {
		return nil, stmt.Errorf("background code error: %s", err)
	}
	f, err := gohanscript.StmtsToFunc("job", stmts)
	if err != nil {
		return nil, err
	}
	return func(context *gohanscript.Context) (interface{}, error) {
		queue := queueArg.Value(context).(*job.Queue)
		newContext := context.Extend(map[string]interface{}{})
		queue.Add(
			job.NewJob(func() {
				f(newContext)
				newContext.VM.Stop()
			}))
		return nil, nil
	}, nil
}