Exemple #1
0
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
}
Exemple #2
0
func transactionFunc(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
	stmts, err := gohanscript.MakeStmts(stmt.File, stmt.RawNode["transaction"])
	if err != nil {
		return nil, err
	}
	runner, err := gohanscript.StmtsToFunc("transaction", stmts)
	if err != nil {
		return nil, err
	}
	return func(context *gohanscript.Context) (interface{}, error) {
		dbVar := util.MaybeString(stmt.RawData["db"])
		if dbVar == "" {
			dbVar = "db"
		}
		rawDB, err := context.Get(dbVar)
		if err != nil {
			return nil, err
		}
		connection := rawDB.(db.DB)
		tx, err := connection.Begin()
		if err != nil {
			return nil, err
		}
		transactionVar := util.MaybeString(stmt.RawData["transaction_var"])
		if transactionVar == "" {
			transactionVar = "transaction"
		}
		context.Set(transactionVar, tx)
		value, err := runner(context)
		if err == nil {
			tx.Commit()
		}
		tx.Close()
		return value, err
	}, nil
}