示例#1
0
// Add adds the task to a named queue.
// An empty queue name means that the default queue will be used.
// Add returns an equivalent Task with defaults filled in, including setting
// the task's Name field to the chosen name if the original was empty.
func Add(c appengine.Context, task *Task, queueName string) (*Task, error) {
	if queueName == "" {
		queueName = "default"
	}
	req := &taskqueue_proto.TaskQueueAddRequest{
		QueueName: []byte(queueName),
		TaskName:  []byte(task.Name),
		EtaUsec:   proto.Int64(time.Nanoseconds()/1e3 + task.Delay),
	}
	method := task.Method
	if method == "" {
		method = "POST"
	}
	if method == "PULL" {
		// Pull-based task
		req.Body = task.Payload
		req.Mode = taskqueue_proto.NewTaskQueueMode_Mode(taskqueue_proto.TaskQueueMode_PULL)
		// TODO: More fields will need to be set.
	} else {
		// HTTP-based task
		if v, ok := taskqueue_proto.TaskQueueAddRequest_RequestMethod_value[method]; ok {
			req.Method = taskqueue_proto.NewTaskQueueAddRequest_RequestMethod(
				taskqueue_proto.TaskQueueAddRequest_RequestMethod(v))
		} else {
			return nil, fmt.Errorf("taskqueue: bad method %q", method)
		}
		req.Url = []byte(task.Path)
		for k, vs := range task.Header {
			for _, v := range vs {
				req.Header = append(req.Header, &taskqueue_proto.TaskQueueAddRequest_Header{
					Key:   []byte(k),
					Value: []byte(v),
				})
			}
		}
		if method == "POST" || method == "PUT" {
			req.Body = task.Payload
		}
	}
	res := &taskqueue_proto.TaskQueueAddResponse{}
	if err := c.Call("taskqueue", "Add", req, res, nil); err != nil {
		return nil, err
	}
	resultTask := *task
	resultTask.Method = method
	if task.Name == "" {
		resultTask.Name = string(res.ChosenTaskName)
	}
	return &resultTask, nil
}
示例#2
0
// Add adds the task to a named queue.
// An empty queue name means that the default queue will be used.
func Add(c appengine.Context, task *Task, queueName string) os.Error {
	if queueName == "" {
		queueName = "default"
	}
	req := &taskqueue_proto.TaskQueueAddRequest{
		QueueName: []byte(queueName),
		TaskName:  []byte(task.Name),
		EtaUsec:   proto.Int64(0),
	}
	method := task.Method
	if method == "" {
		method = "POST"
	}
	if method == "PULL" {
		// Pull-based task
		req.Body = task.Payload
		req.Mode = taskqueue_proto.NewTaskQueueMode_Mode(taskqueue_proto.TaskQueueMode_PULL)
		// TODO: More fields will need to be set.
	} else {
		// HTTP-based task
		if v, ok := taskqueue_proto.TaskQueueAddRequest_RequestMethod_value[method]; ok {
			req.Method = taskqueue_proto.NewTaskQueueAddRequest_RequestMethod(v)
		} else {
			// TODO: return error
		}
		req.Url = []byte(task.Path)
		for k, vs := range task.Header {
			for _, v := range vs {
				req.Header = append(req.Header, &taskqueue_proto.TaskQueueAddRequest_Header{
					Key:   []byte(k),
					Value: []byte(v),
				})
			}
		}
		if method == "POST" || method == "PUT" {
			req.Body = task.Payload
		}
	}
	res := &taskqueue_proto.TaskQueueAddResponse{}
	if err := c.Call("taskqueue", "Add", req, res); err != nil {
		return err
	}
	return nil
}