Task queue functions take advantage of Google Cloud Tasks to help your app run time-consuming, resource-intensive, or bandwidth-limited tasks asynchronously, outside your main application flow.
For example, imagine that you want to create backups of a large set of image files that are currently hosted on an API with a rate limit. In order to be a responsible consumer of that API, you need to respect their rate limits. Plus, this kind of long-running job could be vulnerable to failure due to timeouts and memory limits.
To mitigate this complexity, you can write a task queue function that sets basic
task options like scheduleTime
, and dispatchDeadline
, and then hands the
function off to a queue in Cloud Tasks
. The Cloud Tasks
environment is designed specifically to ensure effective congestion control and
retry policies for these kinds of operations.
The Firebase SDK for Cloud Functions for Firebase v3.20.1 and higher interoperates with Firebase Admin SDK v10.2.0 and higher to support task queue functions.
Using task queue functions with Firebase can result in charges for Cloud Tasks processing. See Cloud Tasks pricing for more information.
Create task queue functions
To use task queue functions, follow this workflow:
- Write a task queue function using the Firebase SDK for Cloud Functions .
- Test your function by triggering it with an HTTP request.
- Deploy your function with the Firebase CLI. When deploying your task queue function for the first time, the CLI will create a task queue in Cloud Tasks with options (rate limiting and retry) specified in your source code.
- Add tasks to the newly created task queue, passing along parameters to set up an execution schedule if needed. You can achieve this by writing the code using the Admin SDK and deploying it to Cloud Functions for Firebase .
Write task queue functions
Code samples in this section are based on an app that sets up a service that backs up all images from NASA's Astronomy Picture of the Day . To get started, import the required modules:
Node.js
// Dependencies for task queue functions.
const
{
onTaskDispatched
}
=
require
(
"firebase-functions/tasks"
);
const
{
onRequest
,
HttpsError
}
=
require
(
"firebase-functions/https"
);
const
{
getFunctions
}
=
require
(
"firebase-admin/functions"
);
const
{
logger
}
=
require
(
"firebase-functions"
);
// Dependencies for image backup.
const
path
=
require
(
"path"
);
const
{
initializeApp
}
=
require
(
"firebase-admin/app"
);
const
{
getStorage
}
=
require
(
"firebase-admin/storage"
);
const
{
GoogleAuth
}
=
require
(
"google-auth-library"
);
Python
# Dependencies for task queue functions.
from
google.cloud
import
tasks_v2
import
requests
from
firebase_functions.options
import
RetryConfig
,
RateLimits
,
SupportedRegion
# Dependencies for image backup.
from
datetime
import
datetime
,
timedelta
import
json
import
pathlib
from
urllib.parse
import
urlparse
from
firebase_admin
import
initialize_app
,
storage
,
functions
from
firebase_functions
import
https_fn
,
tasks_fn
,
params
import
google.auth
from
google.auth.transport.requests
import
AuthorizedSession
.
py

