Home > Back-end >  Cloud Run: endpoint that runs a function as background job
Cloud Run: endpoint that runs a function as background job

Time:11-24

I am trying to deploy a rest api in cloud run where one endpoint launches an async job. The job is defined inside a function in the code.

It seems one way to do it is to use Cloud Task, but this would mean to make a self-call to another endpoint of the deployed api. Specifically, to create an auxiliary endpoint that contains the job code (e.g. /run-my-function) and another one to set the queue to cloud task that launches the /run-my-function?

Is this the right way to do it or I have misunderstand something? In case it's the right way how to specify the url of the /run-my-function endpoint without explicitly hard-code the cloud run deployed uRL name?

The code for the endpoint that launches the endpoint with the run-my-function code would be:

from google.cloud import tasks_v2
client = tasks_v2.CloudTasksClient()
project = 'myproject'
queue = 'myqueue'
location = 'mylocation'
url = 'https://cloudrunservice-abcdefg-ca.b.run.app/run-my-function'
service_account_email = '[email protected]'

parent = client.queue_path(project, location, queue)
task = {
            "http_request": {  
                "http_method": tasks_v2.HttpMethod.POST,
                'url': url,
                "oidc_token": {"service_account_email": service_account_email},
            }
        }
response = client.create_task(parent=parent, task=task)

However, this requires to hard-code the service name https://cloudrunservice-abcdefg-ca.b.run.app and to define an auxiliary endpoint /run-my-function that can be called via http

CodePudding user response:

In your code you are able to get the Cloud Run URL without hardcoding it or setting it in an environment variable.

You can have a look to a previous article that I wrote, in the gracefull termison part. I provide a working code in Go, not so difficult to re-implement in Python.

Here the principle:

Now you have it!

Let me know if you have difficulties to achieve that. I'm not good at Python, but I will be able to write that piece of code!

  • Related