Many apps need to do background processing outside of the context of a web request. This tutorial creates a web app that lets users input text to translate, and then displays a list of previous translations. The translation is done in a background process to avoid blocking the user's request.
The following diagram illustrates the translation request process.
Here is the sequence of events for how the tutorial app works:
- Visit the web page to see a list of previous translations, stored in Firestore.
- Request a translation of text by entering an HTML form.
- The translation request is published to Pub/Sub.
- A Cloud Run function subscribed to that Pub/Sub topic is triggered.
- The Cloud Run function uses Cloud Translation to translate the text.
- The Cloud Run function stores the result in Firestore.
This tutorial is intended for anyone who is interested in learning about background processing with Google Cloud. No prior experience is required with Pub/Sub, Firestore, App Engine, or Cloud Run functions. However, to understand all of the code, some experience with Go, JavaScript, and HTML is helpful.
Objectives
- Understand and deploy a Cloud Run function.
- Understand and deploy an App Engine app.
- Try the app.
Costs
In this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage,
use the pricing calculator
.
When you finish the tasks that are described in this document, you can avoid continued billing by deleting the resources that you created. For more information, see Clean up .
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project .
-
Enable the Firestore, Cloud Run functions, Pub/Sub, and Cloud Translation APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project .
-
Enable the Firestore, Cloud Run functions, Pub/Sub, and Cloud Translation APIs.
-
In the Google Cloud console, open the app in Cloud Shell .
Cloud Shell provides command-line access to your cloud resources directly from the browser. Open Cloud Shell in your browser and click Proceedto download the sample code and change into the app directory.
- In Cloud Shell, configure the
gcloud
tool to use your Google Cloud project:# Configure gcloud for your project gcloud config set project YOUR_PROJECT_ID
Understanding the Cloud Run function
- The function starts by importing several dependencies, like Firestore and Translation. It also has some global variables and types.
- The global Firestore and Translation clients are initialized so they can be reused between function invocations. That way, you don't have to initialize new clients for every function invocation, which would slow down execution.
- The Translation API translates the string to the language you selected.
-
The Cloud Run function starts by initializing the Firestore and Pub/Sub clients. Then, it parses the Pub/Sub message to get the text to translate and the desired target language.
Then, the app comes up with a unique name for the translation request to make sure it doesn't store any duplicate translations. Then, it translates in a Firestore transaction to make sure concurrent executions don't accidentally run the same translation twice.
Deploying the Cloud Run function
-
In Cloud Shell, in the same directory as the
translate.go
file, deploy the Cloud Run function with a Pub/Sub trigger:gcloud functions deploy Translate --runtime go111 \ --trigger-topic = translate --set-env-vars GOOGLE_CLOUD_PROJECT = YOUR_GOOGLE_CLOUD_PROJECT
where
YOUR_GOOGLE_CLOUD_PROJECT
is your Google Cloud project ID.
Understanding the app
There are two main components for the web app:
- A Go HTTP server to handle web requests. The server has the following two
endpoints:
-
/
: Lists all of the existing translations and shows a form users can submit to request new translations. -
/request-translation
: Form submissions are sent to this endpoint, which publishes the request to Pub/Sub to be translated asynchronously.
-
- An HTML template that is filled in with the existing translations by the Go server.
The HTTP server
-
In the
index
directory,main.go
starts by setting up the app and registering HTTP handlers: -
The index handler (
/
) gets all existing translations from Firestore and fills an HTML template with the list: -
New translations are requested by submitting an HTML form. The request translation handler, registered at
/request-translation
, parses the form submission, validates the request, and publishes a message to Pub/Sub:
The HTML template
The HTML template is the basis for the HTML page shown to the user so they can see previous translations and request new ones. The template is filled in by the HTTP server with the list of existing translations.
- The
<head>
element of the HTML template includes metadata, style sheets, and JavaScript for the page:The page pulls in Material Design Lite (MDL) CSS and JavaScript assets. MDL lets you add a Material Design look and feel to your websites.
The page uses JQuery to wait for the document to finish loading and set a form submission handler. Whenever the request translation form is submitted, the page does minimal form validation to check that the value isn't empty, and then sends an asynchronous request to the
/request-translation
endpoint.Finally, an MDL snackbar appears to indicate if the request was successful or if it encountered an error.
- The HTML body of the page uses an MDL layout and several MDL components to display a list of translations and a form to request additional translations:
Building the app
- Before trying to deploy the web app, build the app to make sure it compiles
and all of your dependencies are working.
go build - o start . / index
The build was successful if nothing was printed and a
start
file was created.
Deploying the web app
You can use the App Engine standard environment to build and deploy an app that runs reliably under heavy load and with large amounts of data.
This tutorial uses the App Engine standard environment to deploy the HTTP frontend.
The app.yaml
configures the App Engine app:
- From the same directory as the
app.yaml
file, deploy your app to the App Engine standard environment:gcloud app deploy
Testing the app
After you've deployed the Cloud Run function and App Engine app, try requesting a translation.
-
To view the app in your browser,enter the following URL:
https:// PROJECT_ID . REGION_ID .r.appspot.com
Replace the following:
-
PROJECT_ID
: Your Google Cloud project ID -
REGION_ID
: A code that App Engine assigns to your app
There is a page with an empty list of translations and a form to request new translations.
-
- In the Text to translate
field, enter some text to translate, for
example,
Hello, World
. - Select a language from the drop-down list that you want to translate the text to.
- Click Submit .
- To refresh the page, click Refresh refresh . There is a new row in the translation list. If you don't see a translation, wait a few more seconds and try again. If you still don't see a translation, see the next section about debugging the app.
Debugging the app
If you cannot connect to your App Engine app or don't see new translations, check the following:
- Check that the
gcloud
deploy commands successfully completed and didn't output any errors. If there were errors, fix them, and try deploying the Cloud Run function and the App Engine app again. -
In the Google Cloud console, go to the Logs Viewer page.
Go to Logs Viewer page- In the Recently selected resources drop-down list, click GAE Application , and then click All module_id . You see a list of requests from when you visited your app. If you don't see a list of requests, confirm you selected All module_id from the drop-down list. If you see error messages printed to the Google Cloud console, check that your app's code matches the code in the section about understanding the app .
- In the Recently selected resources
drop-down list, click Cloud Function
, and then click All function name
. You see a
function listed for each requested translation. If not, check that the
Cloud Run function and App Engine app are using the same
Pub/Sub topic:
- In the
background/index/main.go
file, check that thetopicName
constant is"translate"
. - When you deploy the Cloud Run function
,
be sure to include the
--trigger-topic=translate
flag.
- In the
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
Delete the Google Cloud project
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete .
- In the dialog, type the project ID, and then click Shut down to delete the project.
Delete the App Engine instance
- In the Google Cloud console, go to the Versions page for App Engine.
- Select the checkbox for the non-default app version that you want to delete.
- To delete the app version, click Delete .
Delete the Cloud Run function
- Delete the Cloud Run function you created in this tutorial:
gcloud functions delete Translate
What's next
- Try additional Cloud Run functions tutorials .
- Learn more about App Engine .
- Try Cloud Run , which lets you run stateless containers on a fully managed environment or in your own Google Kubernetes Engine cluster.