예제 #1
0
// pingDevicesAsync schedules len(endpoints) tasks of /ping-device.
// d specifies the duration the tasker must wait before executing the task.
// If scheduling fails for some endpoints, those will be in the returned values
// along with a non-nil error.
func pingDevicesAsync(c context.Context, uid string, endpoints []string, d time.Duration) ([]string, error) {
	if len(endpoints) == 0 {
		return nil, nil
	}
	p := path.Join(config.Prefix, "/task/ping-device")
	jobs := make([]*taskqueue.Task, 0, len(endpoints))
	for _, endpoint := range endpoints {
		t := taskqueue.NewPOSTTask(p, url.Values{
			"uid":      {uid},
			"endpoint": {endpoint},
		})
		t.Delay = d
		jobs = append(jobs, t)
	}

	_, err := taskqueue.AddMulti(c, jobs, "")
	merr, mok := err.(appengine.MultiError)
	if !mok {
		return nil, err
	}

	errEndpoints := make([]string, 0)
	for i, e := range merr {
		if e == nil {
			continue
		}
		errEndpoints = append(errEndpoints, endpoints[i])
	}
	if len(errEndpoints) == 0 {
		return nil, nil
	}
	return errEndpoints, fmt.Errorf("pingDevicesAsync: %v", err)
}
예제 #2
0
func (t tqImpl) AddMulti(tasks []*tq.Task, queueName string, cb tq.RawTaskCB) error {
	realTasks, err := taskqueue.AddMulti(t.aeCtx, tqMF2R(tasks), queueName)
	if err != nil {
		if me, ok := err.(appengine.MultiError); ok {
			for i, err := range me {
				tsk := (*taskqueue.Task)(nil)
				if realTasks != nil {
					tsk = realTasks[i]
				}
				cb(tqR2F(tsk), err)
			}
			err = nil
		}
	} else {
		for _, tsk := range realTasks {
			cb(tqR2F(tsk), nil)
		}
	}
	return err
}