Esempio n. 1
0
def create_task(project, location, queue):
    # [START taskqueues_new_task]
    client = tasks.CloudTasksClient()

    # TODO(developer): Uncomment these lines and replace with your values.
    # project = 'my-project-id'
    # location = 'us- central1'
    # queue = 'default'
    amount = 10

    parent = client.queue_path(project, location, queue)

    task = {
        'app_engine_http_request': {
            'http_method': 'POST',
            'relative_uri': '/update_counter',
            'app_engine_routing': {
                'service': 'worker'
            },
            'body': str(amount).encode()
        }
    }

    response = client.create_task(parent, task)
    eta = response.schedule_time.ToDatetime().strftime("%m/%d/%Y, %H:%M:%S")
    print('Task {} enqueued, ETA {}.'.format(response.name, eta))
    # [END taskqueues_new_task]
    return response
Esempio n. 2
0
def push_to_email_queue(user_data):
    client = tasks.CloudTasksClient()

    project = environ.get('PROJECT_ID')
    location = environ.get('EMAIL_NOTIFICATION_QUEUE_LOCATION')
    email_queue = environ.get('EMAIL_NOTIFICATION_QUEUE')

    parent = client.queue_path(project, location, email_queue)

    for user in user_data:
        task = {
            'app_engine_http_request': {
                'http_method': 'POST',
                'relative_uri': '/send-email',
                'app_engine_routing': {
                    'service': 'email-notification-service'
                },
                'headers': {
                    'Content-Type': 'application/json'
                },
                'body': json.dumps({
                    'user_id': user
                }).encode()
            }
        }

        response = client.create_task(parent, task)
        print(response)
Esempio n. 3
0
def send_to_queue(user):
    client = tasks.CloudTasksClient()

    project = environ.get('PROJECT_ID')
    location = environ.get('NOTIFICATION_QUEUE_LOCATION')
    notification_queue = environ.get('NOTIFICATION_QUEUE')

    parent = client.queue_path(project, location, notification_queue)
    task = {
        'app_engine_http_request': {
            'http_method': 'POST',
            'relative_uri': '/generate-notification',
            'app_engine_routing': {
                'service': 'notification-service'
            },
            'headers': {
                'Content-Type': 'application/json'
            },
            'body': json.dumps({
                'user_id': user
            }).encode()
        }
    }

    response = client.create_task(parent, task)
    print(response)
Esempio n. 4
0
def delete_task(project, location, queue):
    # [START taskqueues_setup]
    client = tasks.CloudTasksClient()

    # TODO(developer): Uncomment these lines and replace with your values.
    # project = 'my-project-id'
    # location = 'us- central1'
    # queue = 'queue1'
    # [END taskqueues_setup]

    # [START taskqueues_deleting_tasks]
    task_path = client.task_path(project, location, queue, 'foo')
    response = client.delete_task(task_path)
    # [END taskqueues_deleting_tasks]

    # [START taskqueues_purging_tasks]
    queue_path = client.queue_path(project, location, queue)
    response = client.purge_queue(queue_path)
    # [END taskqueues_purging_tasks]

    # [START taskqueues_pause_queue]
    queue_path = client.queue_path(project, location, queue)
    response = client.pause_queue(queue_path)
    # [END taskqueues_pause_queues]
    return response
Esempio n. 5
0
    def _set_client(self):
        if isinstance(self.client, tasks.CloudTasksClient):
            return

        if self.use_local_task_emulator:
            return

        self.client = tasks.CloudTasksClient()
Esempio n. 6
0
def pause_queue(project, location, queue):
    # [START taskqueues_pause_queue]
    client = tasks.CloudTasksClient()

    # TODO(developer): Uncomment these lines and replace with your values.
    # project = 'my-project-id'
    # location = 'us- central1'
    # queue = 'queue1'

    queue_path = client.queue_path(project, location, queue)
    response = client.pause_queue(queue_path)
    # [END taskqueues_pause_queues]
    return response
Esempio n. 7
0
def delete_queue(project, location, queue):
    # [START cloud_tasks_taskqueues_deleting_queues]
    client = tasks.CloudTasksClient()

    # TODO(developer): Uncomment these lines and replace with your values.
    # project = 'my-project-id'
    # location = 'us- central1'
    # queue = 'queue1'

    queue_path = client.queue_path(project, location, queue)
    response = client.delete_queue(name=queue_path)
    # [END cloud_tasks_taskqueues_deleting_queues]
    return response
Esempio n. 8
0
def create_tasks_with_data(project, location, queue):
    # [START cloud_tasks_taskqueues_passing_data]
    import json
    client = tasks.CloudTasksClient()

    # TODO(developer): Uncomment these lines and replace with your values.
    # project = 'my-project-id'
    # location = 'us- central1'
    # queue = 'default'

    parent = client.queue_path(project, location, queue)

    task1 = {
        'app_engine_http_request': {
            'http_method': tasks.HttpMethod.POST,
            'relative_uri': '/update_counter?key=blue',
            'app_engine_routing': {
                'service': 'worker'
            }
        }
    }

    task2 = {
        'app_engine_http_request': {
            'http_method': tasks.HttpMethod.POST,
            'relative_uri': '/update_counter',
            'app_engine_routing': {
                'service': 'worker'
            },
            'headers': {
                'Content-Type': 'application/json'
            },
            'body': json.dumps({
                'key': 'blue'
            }).encode()
        }
    }

    response = client.create_task(parent=parent, task=task1)
    print(response)
    response = client.create_task(parent=parent, task=task2)
    print(response)
    # [END cloud_tasks_taskqueues_passing_data]
    return response
Esempio n. 9
0
File: main.py Progetto: jtangney/mtg
def add_tasks(event, context):
    """Triggered by upload of decklist file to a Cloud Storage bucket.
    A Task is added to fetch-parse-write each individual deck in the list
    """
    file = event
    # only interested in lists files
    if "decklists" not in file["name"]:
        return

    logging.info('Processing GCS file: ' + file['name'])

    # cloud tasks
    tasks_client = tasks.CloudTasksClient()
    parent = tasks_client.queue_path("jt-mtg", "europe-west1", "deck-queue")

    # lazy init
    global gcs
    if not gcs:
        gcs = storage.Client()

    # get the file from GCS
    bucket = storage.Bucket(name=file['bucket'], client=gcs)
    blob = bucket.blob(file['name'])
    contents = blob.download_as_string().decode('utf-8')
    fileparts = file['name'].split('/')
    source = fileparts[0]
    # print(contents)
    string_io = StringIO(contents)
    dict_reader = csv.DictReader(string_io)
    count = 0
    for row in dict_reader:
        uri = "/{}/deck?deckUrl={}".format(source, row["DeckUrl"])
        if len(fileparts) == 4:
            uri += "&commander=" + fileparts[2]
        task = {
            "app_engine_http_request": {
                "http_method": "GET",
                "relative_uri": uri,
                # "scheduleTime": None,
            }
        }
        response = tasks_client.create_task(parent=parent, task=task)
        count += 1
    logging.info("Added {} {} tasks".format(count, source))
Esempio n. 10
0
def update_queue(project, location, queue):
    # [START taskqueues_processing_rate]
    client = tasks.CloudTasksClient()

    # TODO(developer): Uncomment these lines and replace with your values.
    # project = 'my-project-id'
    # location = 'us- central1'
    # queue = 'queue-blue'

    # Get queue object
    queue_path = client.queue_path(project, location, queue)
    queue = client.get_queue(queue_path)

    # Update queue object
    queue.rate_limits.max_dispatches_per_second = 20
    queue.rate_limits.max_concurrent_dispatches = 10

    response = client.update_queue(queue)
    print(response)
    # [END taskqueues_processing_rate]
    return response
Esempio n. 11
0
def create_task_with_name(project, location, queue, task_name):
    # [START taskqueues_naming_tasks]
    client = tasks.CloudTasksClient()

    # TODO(developer): Uncomment these lines and replace with your values.
    # project = 'my-project-id'
    # location = 'us- central1'
    # queue = 'default'
    # task_name = 'first-try'

    parent = client.queue_path(project, location, queue)

    task = {
        'name': client.task_path(project, location, queue, task_name),
        'app_engine_http_request': {
            'http_method': 'GET',
            'relative_uri': '/url/path'
        }
    }
    response = client.create_task(parent, task)
    print(response)
    # [END taskqueues_naming_tasks]
    return response
Esempio n. 12
0
def create_queue(project, location, queue_blue_name, queue_red_name):
    # [START taskqueues_using_yaml]
    client = tasks.CloudTasksClient()

    # TODO(developer): Uncomment these lines and replace with your values.
    # project = 'my-project-id'
    # location = 'us- central1'
    # queue_blue_name = 'queue-blue'
    # queue_red_name = 'queue-red'

    parent = client.location_path(project, location)

    queue_blue = {
        'name': client.queue_path(project, location, queue_blue_name),
        'rate_limits': {
            'max_dispatches_per_second': 5
        },
        'app_engine_routing_override': {
            'version': 'v2',
            'service': 'task-module'
        }
    }

    queue_red = {
        'name': client.queue_path(project, location, queue_red_name),
        'rate_limits': {
            'max_dispatches_per_second': 1
        }
    }

    queues = [queue_blue, queue_red]
    for queue in queues:
        response = client.create_queue(parent, queue)
        print(response)
    # [END taskqueues_using_yaml]
    return response
Esempio n. 13
0
import json
import time

from google.cloud import tasks
from google.protobuf import duration_pb2
from google.protobuf import timestamp_pb2

client = tasks.CloudTasksClient()

DEFAULT_LOCATION = 'us-central1'


def push(queue, target, payload, location=DEFAULT_LOCATION, **kwargs):
    # fully qualified queue name
    project = kwargs['project']
    parent = client.queue_path(project, location, queue)

    encoded_payload = json.dumps(payload).encode()
    # default response deadline is 15 minutes
    # this is how long task handler has before task system
    # marks task as DEADLINE_EXCEEDED
    response_deadline = kwargs.get('response_deadline', 15 * 60)
    deadline = duration_pb2.Duration().FromSeconds(response_deadline)
    task = {
        'http_request': {
            'http_method': 'POST',
            'url': target,
            'body': encoded_payload,
            'headers': {
                'Content-Type': 'application/json'
            }
Esempio n. 14
0
def client():
    global _client
    if not _client:
        _client = tasks.CloudTasksClient()
    return _client
Esempio n. 15
0
def retry_task(project, location, fooqueue, barqueue, bazqueue):
    # [START taskqueues_retrying_tasks]
    from google.protobuf import duration_pb2

    client = tasks.CloudTasksClient()

    # TODO(developer): Uncomment these lines and replace with your values.
    # project = 'my-project-id'
    # location = 'us- central1'
    # fooqueue = 'fooqueue'
    # barqueue = 'barqueue'
    # bazqueue = 'bazqueue'

    parent = client.location_path(project, location)

    max_retry = duration_pb2.Duration()
    max_retry.seconds = 2*60*60*24

    foo = {
        'name': client.queue_path(project, location, fooqueue),
        'rate_limits': {
            'max_dispatches_per_second': 1
        },
        'retry_config': {
            'max_attempts': 7,
            'max_retry_duration': max_retry
        }
    }

    min = duration_pb2.Duration()
    min.seconds = 10

    max = duration_pb2.Duration()
    max.seconds = 200

    bar = {
        'name': client.queue_path(project, location, barqueue),
        'rate_limits': {
            'max_dispatches_per_second': 1
        },
        'retry_config': {
            'min_backoff': min,
            'max_backoff': max,
            'max_doublings': 0
        }
    }

    max.seconds = 300
    baz = {
        'name': client.queue_path(project, location, bazqueue),
        'rate_limits': {
            'max_dispatches_per_second': 1
        },
        'retry_config': {
            'min_backoff': min,
            'max_backoff': max,
            'max_doublings': 3
        }
    }

    queues = [foo, bar, baz]
    for queue in queues:
        response = client.create_queue(parent, queue)
        print(response)
    # [END taskqueues_retrying_tasks]
    return response