How to use Google Cloud Tasks in Laravel PHP

Errol Fernandes
Feb 22, 2019 · 4 min read
Image for post
Image for post
content.nanobox.io

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

One of the ways we can do this is by 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. 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.

Image for post
Image for post

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.

Image for post
Image for post

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:

Image for post
Image for post

Step 4:

Create the initTask() function:

Image for post
Image for post

We pass the userID of the current 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.

Image for post
Image for post

Step 6:

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

Image for post
Image for post

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:

Image for post
Image for post

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.

Normally in Laravel, we call the event from the respective controller directly. But for using 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 APIs 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 don’t need to worry about the supervisor or managing the jobs in the queue table, as everything is taken care by the Google Task Queue. All I have to do is monitor the Task Queue if there is any failed task.

Using Google Cloud API, I can create multiple queues for different target applications I deploy on Google App Engine irrespective of whether the environment is Standard or Flexible.

freeCodeCamp.org

This is no longer updated.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store