Scheduling Tasks With Cron for PHP

The App Engine Cron Service allows you to configure regularly scheduled tasks
that operate at defined times or regular intervals. These tasks are commonly
known as cron jobs. These cron jobs are automatically triggered by the App
Engine Cron Service. For instance, you might use a cron job to send out an email
report on a daily basis, or to update some cached data every 10 minutes, or
refresh summary information once an hour.

A cron job makes an HTTP GET request to a URL as scheduled. The handler for
that URL executes the logic when it is called.
A cron job request is subject to the same limits as those for
push task queues.

Creating a cron job

Create the cron.yaml file in the root directory of your
application (alongside
app.yaml).

Add one or more <cron> entries to your file and define the
necessary elements for your job, including the required <url> and
<schedule> elements.

The target specification is optional and is the name of a service/version.
If present, the target is prepended to your app's hostname, causing the job
to be routed to that service/version.
If no target is specified, the job will run in the versions of the default
service that are configured for traffic.

Create a handler for the cron job URL. The handler should execute any tasks
that you want scheduled. The handler should respond with an HTTP status code
between 200 and 299 (inclusive) to indicate success. Other status codes can
be returned and can be used to
retry the cron job.

Testing cron jobs in the development server

The local development server doesn't automatically run your cron jobs. You can
make requests directly to your cron job's URL to test your functionality. You
can use your local cron or scheduled tasks interface to trigger the URLs of your
jobs with curl or a similar tool.

You can use the admin interface of the local development server to
view your cron jobs at http://localhost:8000/cron.

Retrying cron jobs that fail

If a cron job's request handler returns a status code that is not in the range
200–299 (inclusive) App Engine considers the job to have failed. By default,
failed jobs are not retried unless a 503 status code is returned, in which case
it is retried every minute until it succeeds or returns a 200-299 status code.

gcloud

appcfg

Deleting all cron jobs

To delete all cron jobs:

Edit the contents of the
cron.yaml file to:

cron:

Deploy the cron.yaml file
to App Engine.

Securing URLs for cron

A cron handler is just a normal handler defined in app.yaml. You can prevent
users from accessing URLs used by scheduled tasks by restricting access to
administrator accounts. Scheduled tasks can access admin-only URLs. You can
restrict a URL by adding login: admin to the handler configuration in
app.yaml.

Note: While cron jobs can use URL paths restricted with login: admin, they
cannot use URL paths restricted with login: required because cron scheduled
tasks are not run as any user. The admin restriction is satisfied by the
inclusion of the X-Appengine-Cron header described below.
For more information see how to require login or admin status in the app.yaml reference.

To test a cron job, sign in as an administrator and visit the URL of the handler
in your browser.

Requests from the Cron Service will also contain a HTTP header:

X-Appengine-Cron: true

The X-Appengine-Cron header is set internally by Google App Engine. If your
request handler finds this header it can trust that the request is a cron
request. If the header is present in an external user request to your app, it
is stripped, except for requests from logged in administrators of the
application, who are allowed to set the header for testing purposes.

Google App Engine issues Cron requests from the IP address 0.1.0.1.

Calling Google Cloud Endpoints

You cannot specify a Google Cloud Endpoint in
the url field of a cron job.
If you want your cron job to call a Google Cloud Endpoint,
issue a request to a target that is served by a handler in
your app, and call the endpoint class and method from the handler code.

Viewing cron jobs in the GCP Console

The GCP Console
Task queues page
has a tab that shows the tasks that are running cron jobs.