Example #1
0
// SendGroup triggers a group of parallel tasks
func (server *Server) SendGroup(group *Group) ([]*backends.AsyncResult, error) {
	// Make sure result backend is defined
	if server.backend == nil {
		return nil, errors.New("Result backend required")
	}

	asyncResults := make([]*backends.AsyncResult, len(group.Tasks))

	var wg sync.WaitGroup
	wg.Add(len(group.Tasks))
	errorsChan := make(chan error)

	// Init group
	server.backend.InitGroup(group.GroupUUID, group.GetUUIDs())

	for i, signature := range group.Tasks {
		go func(s *signatures.TaskSignature, index int) {
			defer wg.Done()

			// Set initial task states to PENDING
			if err := server.backend.SetStatePending(s); err != nil {
				errorsChan <- err
				return
			}

			// Publish task
			if err := server.broker.Publish(s); err != nil {
				errorsChan <- fmt.Errorf("Publish Message: %v", err)
				return
			}

			asyncResults[index] = backends.NewAsyncResult(s, server.backend)
		}(signature, i)
	}

	done := make(chan int)
	go func() {
		wg.Wait()
		done <- 1
	}()

	select {
	case err := <-errorsChan:
		return asyncResults, err
	case <-done:
		return asyncResults, nil
	}
}
Example #2
0
// SendTask publishes a task to the default queue
func (server *Server) SendTask(signature *signatures.TaskSignature) (*backends.AsyncResult, error) {
	// Make sure result backend is defined
	if server.backend == nil {
		return nil, errors.New("Result backend required")
	}

	// Auto generate a UUID if not set already
	if signature.UUID == "" {
		signature.UUID = fmt.Sprintf("task_%v", uuid.New())
	}

	// Set initial task state to PENDING
	if err := server.backend.SetStatePending(signature); err != nil {
		return nil, fmt.Errorf("Set State Pending: %v", err)
	}

	if err := server.broker.Publish(signature); err != nil {
		return nil, fmt.Errorf("Publish Message: %v", err)
	}

	return backends.NewAsyncResult(signature, server.backend), nil
}