예제 #1
0
파일: util.py 프로젝트: snarfed/bridgy
def add_task(queue, eta_seconds=None, **kwargs):
    """Adds a Cloud Tasks task for the given entity.

  Args:
    queue: string, queue name
    entity: Source or Webmentions instance
    eta_seconds: integer, optional
    kwargs: added to task's POST body (form-encoded)
  """
    params = {
        'app_engine_http_request': {
            'http_method': 'POST',
            'relative_uri': f'/_ah/queue/{queue}',
            'body': urllib.parse.urlencode(util.trim_nulls(kwargs)).encode(),
            # https://googleapis.dev/python/cloudtasks/latest/gapic/v2/types.html#google.cloud.tasks_v2.types.AppEngineHttpRequest.headers
            'headers': {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
        }
    }
    if eta_seconds:
        params['schedule_time'] = Timestamp(seconds=eta_seconds)

    queue_path = tasks_client.queue_path(APP_ID, TASKS_LOCATION, queue)
    if LOCAL:
        logger.info(f'Would add task: {queue_path} {params}')
    else:
        task = tasks_client.create_task(
            CreateTaskRequest(parent=queue_path, task=params))
        logger.info(f'Added {queue} task {task.name} with ETA {eta_seconds}')
예제 #2
0
    def expect_task(self, queue, eta_seconds=None, **kwargs):
        if not self.stubbed_create_task:
            self.mox.StubOutWithMock(tasks_client, 'create_task')
            self.stubbed_create_task = True

        def check_queue(path):
            if not path.endswith('/' + queue):
                # print("expect_task: %s doesn't end with /%s!" % (path, queue))
                return False
            return True

        def check_params(params):
            req = params['app_engine_http_request']
            if not check_queue(req['relative_uri']):
                return False

            # convert model objects and keys to url-safe key strings for comparison
            for name, val in kwargs.items():
                if isinstance(val, ndb.Model):
                    kwargs[name] = val.key.urlsafe().decode()
                elif isinstance(val, ndb.Key):
                    kwargs[name] = val.urlsafe().decode()

            got = set(urllib.parse.parse_qsl(req['body'].decode()))
            expected = set(kwargs.items())
            if got != expected:
                # print('expect_task: expected %s, got %s' % (expected, got))
                return False

            if eta_seconds is not None:
                got = params['schedule_time'].seconds - util.to_utc_timestamp(
                    util.now_fn())
                delta = eta_seconds * .2 + 10
                if not (got + delta >= eta_seconds >= got - delta):
                    # print('expect_task: expected schedule_time %r, got %r' % (eta_seconds, got))
                    return False

            return True

        return tasks_client.create_task(
            mox.Func(check_queue),
            mox.Func(check_params)).InAnyOrder().AndReturn(
                Task(name='my task'))
예제 #3
0
파일: testutil.py 프로젝트: kevincox/bridgy
    def expect_task(self, queue, eta_seconds=None, **kwargs):
        self.stub_create_task()

        def check_task(task):
            if not task.parent.endswith('/' + queue):
                # These can help for debugging, but can also be misleading, since many
                # tests insert multiple tasks, so check_task() runs on all of them (due
                # to InAnyOrder() below) until it finds one that matches.
                # print("expect_task: %s doesn't end with /%s!" % (task.parent, queue))
                return False

            req = task.task.app_engine_http_request
            if not req.relative_uri.endswith('/' + queue):
                # print("expect_task: relative_uri %s doesn't end with /%s!" % (
                #   req.relative_uri, queue))
                return False

            # convert model objects and keys to url-safe key strings for comparison
            for name, val in kwargs.items():
                if isinstance(val, ndb.Model):
                    kwargs[name] = val.key.urlsafe().decode()
                elif isinstance(val, ndb.Key):
                    kwargs[name] = val.urlsafe().decode()

            got = set(urllib.parse.parse_qsl(req.body.decode()))
            expected = set(kwargs.items())
            if got != expected:
                # print('expect_task: expected %s, got %s' % (expected, got))
                return False

            if eta_seconds is not None:
                got = (util.to_utc_timestamp(task.task.schedule_time) -
                       util.to_utc_timestamp(util.now_fn()))
                delta = eta_seconds * .2 + 10
                if not (got + delta >= eta_seconds >= got - delta):
                    # print('expect_task: expected schedule_time %r, got %r' % (eta_seconds, got))
                    return False

            return True

        return tasks_client.create_task(
            mox.Func(check_task)).InAnyOrder().AndReturn(Task(name='my task'))