Skip to content

Commit

Permalink
Update Python Task Queue code sample. (#1128)
Browse files Browse the repository at this point in the history
* Update Python Task Queue code sample.

* fix: pyfmt
  • Loading branch information
jonathanedey authored Mar 11, 2024
1 parent cd8938b commit a8b1ab8
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions Python/taskqueues-backup-images/functions/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import json
import pathlib
from urllib.parse import urlparse
from firebase_admin import initialize_app, storage
from firebase_admin import initialize_app, storage, functions
from firebase_functions import https_fn, tasks_fn, params
import google.auth
from google.auth.transport.requests import AuthorizedSession
Expand Down Expand Up @@ -96,9 +96,7 @@ def backupapod(req: tasks_fn.CallableRequest) -> str:
@https_fn.on_request()
def enqueuebackuptasks(_: https_fn.Request) -> https_fn.Response:
"""Adds backup tasks to a Cloud Tasks queue."""
tasks_client = tasks_v2.CloudTasksClient()
task_queue = tasks_client.queue_path(params.PROJECT_ID.value, SupportedRegion.US_CENTRAL1,
"backupapod")
task_queue = functions.task_queue("backupapod")
target_uri = get_function_url("backupapod")

for i in range(BACKUP_COUNT):
Expand All @@ -108,19 +106,14 @@ def enqueuebackuptasks(_: https_fn.Request) -> https_fn.Response:
schedule_delay = timedelta(hours=batch)
schedule_time = datetime.now() + schedule_delay

dispatch_deadline_seconds = 60 * 5 # 5 minutes

backup_date = BACKUP_START_DATE + timedelta(days=i)
body = {"data": {"date": backup_date.isoformat()[:10]}}
task = tasks_v2.Task(http_request={
"http_method": tasks_v2.HttpMethod.POST,
"url": target_uri,
"headers": {
"Content-type": "application/json"
},
"body": json.dumps(body).encode()
},
schedule_time=schedule_time)
tasks_client.create_task(parent=task_queue, task=task)

task_options = functions.TaskOptions(schedule_time=schedule_time,
dispatch_deadline_seconds=dispatch_deadline_seconds,
uri=target_uri)
task_queue.enqueue(body, task_options)
return https_fn.Response(status=200, response=f"Enqueued {BACKUP_COUNT} tasks")
# [END v2EnqueueTasks]

Expand Down

0 comments on commit a8b1ab8

Please sign in to comment.