Using Google Cloud Tasks in Laravel PHP

Deploying laravel application on Google App Engine is a fairly easy task provided the documentation available on the internet but setting up the asynchronous task processing (Laravel Eventing) on Google Cloud effectively is not that simple.

One of the ways is using the supervisord.conf file to set up the laravel queue:listen command but this is only available for Google App Engine Flexible Environment. So if you need a solution for Google App Engine Standard Environment then you need to use Google Cloud Tasks to process async jobs.

Google Cloud Tasks is a fully managed service that allows you to manage the execution, dispatch, and delivery of a large number of distributed tasks. Using Cloud Tasks, you can perform work asynchronously outside of a user or service-to-service request.

Step 1:

Create a task queue to handle the tasks. Over here we use the queue.yaml file to specify the configuration of the queue to be created in Google Cloud. The target parameter contains the name of the application deployed on app engine.

Step 2:

Deploy the queue.yaml file in Google Cloud using the command gcloud app deploy queue.yaml The below image is of a Google Task queue created in Google Cloud.

Step 3:

We need to pass the route of the api and the payload object for the task. In the below example we will pass the required data to an initTask() function

Step 4:

Create the initTask() function

We pass the userid of the currrent user in the payload for authentication reasons. Now that the payload is ready we pass all the required details to the task builder function

Step 5:

Create the create_task() method. This method will use the Google API to build a Cloud Task and pass it to the task queue.The gAppCreds.json file is the Google Service Account created for defining the roles of the application

Step 6:

Now we create the api routes using the RouteServiceProvider in api.php

Step 7:

Now we create a TaskController which will route the different api to the specific function. So in the below example we create an associateApp() function

In the above function, we json_decode the payload that we sent in Step 4 and pass it to the respective event for processing. The userid that we passed in Step 4 is used to validate the authenticity of the incoming api request.

Final Verdict:

Normally in laravel, we call the event from the respective controller directly but for usage of Google cloud tasks we create a Task API which in turn calls the route that in turn calls the event to process our data, so in short we create multiple api for our laravel events and jobs which are then called (by Google Task API) based on the route that you pass in Step 3 and Step 6. Since I am using Google Cloud Tasks, I dont need to worry about the supervisord or managing the jobs in the queue table since everything is taken care by the Google Task Queue and all I have to do is only monitor the Task Queue if there is any failed task. Using Google Cloud API, I can create multiple queue for different target applications I deploy on Google App Engine irrespective whether the environment is Standard or Flexible.