Creating your background job

Starting with version 4.2, Ruby on Rails includes an abstraction layer around background job processing called
ActiveJob. This abstraction allows you to write the queuing
and execution logic independently of queue and job runner implementations.

Ruby on Rails provides command-line tools for generating templated skeletons for things such as database migrations,
controllers, and even background jobs.

You will create a job named HelloJob that will accept a name argument and print "Hello #{name}" to standard output.

Deploying to App Engine flexible environment

Option A: Shared worker and web application

For this option, the App Engine service will run both the web server and a worker process via a process manager called
foreman. If you choose this method, App Engine will scale your web and worker
instances together.

Be sure to replace the [SECRET_KEY] with a secret key for Rails sessions.

Deploy to App Engine

gcloud app deploy app.yaml

Option B: Separate worker and web application

For this option, you are creating 2 App Engine services - one runs the web server and one runs worker processes. Both
services use the same application code. This configuration allows you to scale background worker instances independently
of your web instances at the cost of potentially using more resources.

Create an app.yaml for deploying the web service to Google App Engine:

Be sure to replace the [SECRET_KEY] with a secret key for Rails sessions.

Note that the health check is disabled here because the worker service is not running a web server and cannot
respond to the health check ping.

As mentioned above, you can configure scaling for the worker service independently of the default (web) service.
In the manual_scaling section, you have configured the worker service to start with 1 worker instance. For
more information on scaling options, see scaling configuration options in app.yaml.
If you choose an automatic_scaling option, be aware that scaling for the background processing is based off
of CPU utilization, not queue size.

Deploy both services to App Engine

gcloud app deploy app.yaml worker.yaml

Verify your background queuing works

In the Cloud Platform Console, go to the
App Engine Services page. Locate the service that is
running your background workers (if option A, it should be the default service, if option B, it should be
the worker service). Click Tools -> Logs for that service.

Navigate back to the Logs dashboard. In the Filter by label or text search field, add "Hello, Jeff"
and you should see a logging statement like the following if using foreman:

13:13:52.000 worker.1 | Hello, Jeff

or if using a second service:

13:13:52.000 Hello, Jeff

Congratulations, you have successfully set up background job processing on Google App Engine with Google Cloud Pub/Sub.

Cleaning up

After you've finished this tutorial, you can clean up the resources you created on Google Cloud Platform
so you won't be billed for them in the future. The following sections describe how to delete or turn off these
resources.

Deleting the project

The easiest way to eliminate billing is to delete the project you created for the tutorial.

If you used an existing project, you'll also delete any other work you've done in the project.
You can't reuse the project ID of a deleted project. If you created a custom project ID that you plan to use in
the future, you should delete the resources inside the project instead. This ensures that URLs that use the project ID,
such as an appspot.com URL, remain available.

Related Tutorials

Submit a Tutorial

Request a Tutorial

GCP Tutorials

Beta: Community Contributed Tutorials is a Beta release. This feature is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
Except as otherwise noted, the content of this page is licensed under the
Creative Commons Attribution 4.0 License, and code samples are licensed under the
Apache 2.0 License. For details, see our
Site Policies. Java is a registered trademark of Oracle and/or its affiliates.