in src/google/appengine/api/taskqueue/taskqueue.py [0:0]
def __init__(self, payload=None, **kwargs):
"""Initializer.
Args:
payload: Optional; the payload data for this task. This argument is only
allowed for `POST` and `PUT` methods and pull tasks. In push queues,
the payload is delivered to the webhook or backend in the body of an
HTTP request. In pull queues, the payload is fetched by workers as
part of the response from `lease_tasks()`.
name: Optional; the name to give the task. If you do not specify a name,
a name is auto-generated when added to a queue and assigned to this
object. The name must match the `_TASK_NAME_PATTERN` regular
expression. Avoid sequential names, such as counters or timestamps, as
these names can lead to decreased availability and performance.
method: Optional; the HTTP method to use when accessing the webhook. The
default value is `POST`. This argument is not used for pull queues.
url: Optional; the relative URL where the webhook that should handle
this task is located for this application. You can include a query
string in this value unless it is being used in a `POST` method. You
cannot specify a URL for pull tasks.
headers: Optional; a dictionary of headers to pass to the webhook. The
values in the dictionary can be iterable to indicate repeated header
fields. You cannot specify headers for pull tasks. In a push task,
if you do not specify a `Content-Type` header, the default value of
`text/plain` will be used. In a push task, if you specify a `Host`
header, you cannot use the `target` keyword argument. Any headers that
use the `X-AppEngine` prefix will also be dropped.
params: Optional; a dictionary of parameters to use for the task. For
`POST` requests and PULL tasks, these parameters are encoded as
`application/x-www-form-urlencoded` and set to the payload. For both
`POST` and pull requests, you cannot specify parameters if you
already specified a `payload`. In `PUT` requests, parameters are
converted to a query string if the URL contains a query string, or if
the task already has a `payload`. Do not specify parameters if the URL
contains a query string and the method is `GET`.
countdown: Optional; time in seconds into the future that this task
should run or be leased. The default value is zero. Do not specify a
countdown if you also specified an `eta`, as it sets the ETA to a
value of now + `countdown`.
eta: Optional; a `datetime.datetime` specifying the absolute time at
which the task should be run or leased. The `eta` argument must not
be specified if `countdown` is specified. This value can be time
zone-aware or time zone-naive. If the value is set to None, the
default value is now. For pull tasks, no worker will be able to
lease this task before the time indicated by the `eta` argument.
retry_options: Optional; a `TaskRetryOptions` object used to control
how the task will be retried if it fails. For pull tasks, only the
`task_retry_limit` option is allowed. For push tasks, you can use
the `min_backoff_seconds`, `max_backoff_seconds`, `task_age_limit`,
`max_doublings`, and `task_retry_limit` options.
target: Optional; a string that names a module or version, a frontend
version, or a backend on which to run the task. The string is
prepended to the domain name of your app. If you set the `target`,
do not specify a `Host` header in the dictionary for the `headers`
argument. For pull tasks, do not specify a target.
tag: Optional; the tag to be used when grouping by tag (pull tasks only).
dispatch_deadline_usec: A duration of time that serves as a limit on the
dispatch duration; if the task's end point does not respond to the
request within this deadline, the task is canceled (and retried
depending on the config).
Raises:
InvalidDispatchDeadlineError: If the `dispatch_deadline_usec` is not
within valid limits.
InvalidEtaError: If the `eta` is too far into the future.
InvalidTagError: If the tag is too long.
InvalidTaskError: If any of the parameters are invalid.
InvalidTaskNameError: If the task name is invalid.
InvalidUrlError: If the task URL is invalid or too long.
TaskTooLargeError: If the task with its associated payload is too large.
"""
args_diff = set(six.iterkeys(kwargs)) - self.__CONSTRUCTOR_KWARGS
if args_diff:
raise TypeError('Invalid arguments: %s' % ', '.join(args_diff))
self.__name = kwargs.get('name')
if self.__name and not _TASK_NAME_RE.match(self.__name):
raise InvalidTaskNameError(
'The task name does not match expression "%s"; found %s' %
(_TASK_NAME_PATTERN, self.__name))
self.__default_url, self.__relative_url, query = Task.__determine_url(
kwargs.get('url', ''))
self.__headers = urlfetch._CaselessDict()
self.__headers.update(kwargs.get('headers', {}))
self.__method = kwargs.get('method', 'POST').upper()
self.__tag = kwargs.get('tag')
self.__payload = None
self.__retry_count = 0
self.__queue_name = None
self.__dispatch_deadline_usec = kwargs.get('dispatch_deadline_usec')
size_check = kwargs.get('_size_check', True)
params = kwargs.get('params', {})
apps_namespace = namespace_manager.google_apps_namespace()
if apps_namespace is not None:
self.__headers.setdefault('X-AppEngine-Default-Namespace', apps_namespace)
self.__headers.setdefault('X-AppEngine-Current-Namespace',
namespace_manager.get_namespace())
if query and params:
raise InvalidTaskError('Query string and parameters both present; '
'only one of these can be supplied')
if self.__method != 'PULL' and self.__tag is not None:
raise InvalidTaskError('tag can only be specified for pull tasks')
if self.__method == 'PULL':
if not self.__default_url:
raise InvalidTaskError('url must not be specified for pull tasks')
if kwargs.get('headers'):
raise InvalidTaskError('headers must not be specified for pull tasks')
if kwargs.get('target'):
raise InvalidTaskError('target must not be specified for pull tasks')
if params:
if payload:
raise InvalidTaskError(
'Message body and parameters both present for '
'PULL method; only one of these can be supplied')
payload = Task.__encode_params(params)
if payload is None:
raise InvalidTaskError('payload must be specified for pull task')
self.__payload = Task.__convert_payload(payload, self.__headers)
elif self.__method == 'POST':
if payload and params:
raise InvalidTaskError('Message body and parameters both present for '
'POST method; only one of these can be '
'supplied')
elif query:
raise InvalidTaskError('POST method cannot have a query string; '
'use the "params" keyword argument instead')
elif params:
self.__payload = Task.__encode_params(params)
self.__headers.setdefault(
'content-type', 'application/x-www-form-urlencoded')
elif payload is not None:
self.__payload = Task.__convert_payload(payload, self.__headers)
elif self.__method in _NON_POST_HTTP_METHODS:
if payload and self.__method not in _BODY_METHODS:
raise InvalidTaskError(
'Payload can only be specified for methods %s' %
', '.join(_BODY_METHODS))
if payload:
self.__payload = Task.__convert_payload(payload, self.__headers)
if params:
query = Task.__encode_params(params)
if query:
self.__relative_url = '%s?%s' % (self.__relative_url, query)
else:
raise InvalidTaskError('Invalid method: %s' % self.__method)
self.__target = kwargs.get('target')
self.__resolve_hostname_and_target()
self.__headers_list = _flatten_params(self.__headers)
self.__eta_posix = Task.__determine_eta_posix(
kwargs.get('eta'), kwargs.get('countdown'))
self.__eta = None
self.__retry_options = kwargs.get('retry_options')
self.__enqueued = False
self.__deleted = False
if self.__eta_posix - time.time() > _MAX_COUNTDOWN_SECONDS:
raise InvalidEtaError('ETA too far in the future')
if size_check:
if self.__method == 'PULL':
max_task_size_bytes = MAX_PULL_TASK_SIZE_BYTES
else:
max_task_size_bytes = MAX_PUSH_TASK_SIZE_BYTES
if self.size > max_task_size_bytes:
raise TaskTooLargeError('Task size must be less than %d; found %d' %
(max_task_size_bytes, self.size))
if self.__tag and len(self.__tag) > MAX_TAG_LENGTH:
raise InvalidTagError(
'Tag must be <= %d bytes. Got a %d byte tag.' % (
MAX_TAG_LENGTH, len(self.__tag)))
if self.__dispatch_deadline_usec is not None:
_ValidateDispatchDeadline(self.__dispatch_deadline_usec)