Background function retries and termination

Background functions support optional retries as a way to increase the likelihood of events being successfully processed when functions involve operations that may fail intermittently. Retries can be enabled when deploying via command line by providing the --retry flag, or in the Cloud Console when configuring triggers under Advanced options Retry on failure.

Note that retries will be performed for up to seven days when retires are enabled. For this reason, Google recommends that a retry termination condition be included in the function definition, such as comparing the event's creation timestamp to the current time of execution.

As a convenience, background functions can be terminated in a number of ways. The primary way to terminate background functions is by simply invoking the provided callback function. Another option is to return anything or throw an exception. Lastly, a function may return a promise to allow potentially unfinished asynchronous operations to complete before termination. This can be convenient when performing multiple asynchronous operations or when leveraging third-party libraries that return promises.

Functions should always explicitly terminate in some manner. Failure to do so will result in the function executing until the timeout is reached. Timing out will cause the function to incur higher costs, and functions that time out will be cold started in a subsequent execution, leading to poor performance. Throwing an exception will also lead to subsequent cold starts. This means it is often better to handle the exception via a try/catch block before invoking the function callback.