Example #1
0
// Get returns task result (synchronous blocking call)
func (asyncResult *AsyncResult) Get() (reflect.Value, error) {
	if asyncResult.backend == nil {
		return reflect.Value{}, errors.New("Result backend not configured")
	}

	for {
		asyncResult.GetState()

		// Purge state if we are using AMQP backend
		_, isAMQPBackend := asyncResult.backend.(*AMQPBackend)
		if isAMQPBackend && asyncResult.taskState.IsCompleted() {
			asyncResult.backend.PurgeState(asyncResult.taskState.TaskUUID)
		}

		if asyncResult.taskState.IsSuccess() {
			return utils.ReflectValue(
				asyncResult.taskState.Result.Type,
				asyncResult.taskState.Result.Value,
			)
		}

		if asyncResult.taskState.IsFailure() {
			return reflect.Value{}, errors.New(asyncResult.taskState.Error)
		}
	}
}
Example #2
0
// Get returns task result limited in time(synchronous blocking call)
func (asyncResult *AsyncResult) GetWithTimeout(timeoutD, sleepD time.Duration) (reflect.Value, error) {
	if asyncResult.backend == nil {
		return reflect.Value{}, errors.New("Result backend not configured")
	}

	timeout := time.NewTimer(timeoutD)

	for {
		select {
		case <-timeout.C:
			return reflect.Value{}, errors.New("Timeout reached")
		default:
			asyncResult.GetState()

			// Purge state if we are using AMQP backend
			_, isAMQPBackend := asyncResult.backend.(*AMQPBackend)
			if isAMQPBackend && asyncResult.taskState.IsCompleted() {
				asyncResult.backend.PurgeState(asyncResult.taskState.TaskUUID)
			}

			if asyncResult.taskState.IsSuccess() {
				return utils.ReflectValue(
					asyncResult.taskState.Result.Type,
					asyncResult.taskState.Result.Value,
				)
			}

			if asyncResult.taskState.IsFailure() {
				return reflect.Value{}, errors.New(asyncResult.taskState.Error)
			}
			time.Sleep(sleepD)
		}
	}
}
Example #3
0
// ReflectArgs converts []TaskArg to []reflect.Value
func ReflectArgs(args []signatures.TaskArg) ([]reflect.Value, error) {
	argValues := make([]reflect.Value, len(args))

	for i, arg := range args {
		argValue, err := utils.ReflectValue(arg.Type, arg.Value)
		if err != nil {
			return nil, err
		}
		argValues[i] = argValue
	}

	return argValues, nil
}