Ative as novas tentativas de funções acionadas por eventos (1.ª geração)

Este documento descreve como ativar a repetição para funções orientadas por eventos. As novas tentativas automáticas não estão disponíveis para funções HTTP.

Semântica da nova tentativa

As funções do Cloud Run oferecem uma execução, pelo menos, uma vez de uma função orientada por eventos para cada evento emitido por uma origem de eventos. Por predefinição, se uma invocação de função terminar com um erro, a função não é invocada novamente e o evento é ignorado. Quando ativa as repetições numa função baseada em eventos, o Cloud Run Functions repete uma invocação de função com falha até ser concluída com êxito ou o período de repetição expirar.

Esta janela de nova tentativa expira após 7 dias. As funções do Cloud Run repetem as funções acionadas por eventos recém-criadas através de uma estratégia de recuo exponencial, com um recuo crescente entre 10 e 600 segundos. Esta política é aplicada a novas funções na primeira vez que as implementa. Não é aplicada retroativamente às funções existentes que foram implementadas pela primeira vez antes de as alterações descritas nesta nota de lançamento entrarem em vigor, mesmo que reimplemente as funções.

Quando as novas tentativas não estão ativadas para uma função, que é a predefinição, a função comunica sempre que foi executada com êxito e os códigos de resposta 200 OK podem aparecer nos respetivos registos. Isto ocorre mesmo que a função tenha encontrado um erro. Para deixar claro quando a sua função encontra um erro, certifique-se de que comunica os erros de forma adequada.

Por que motivo as funções acionadas por eventos não são concluídas

Em raras ocasiões, uma função pode terminar prematuramente devido a um erro interno e, por predefinição, a função pode ou não ser repetida automaticamente.

Normalmente, uma função acionada por eventos pode não ser concluída com êxito devido a erros gerados no próprio código da função. Os motivos pelos quais isto pode acontecer incluem:

  • A função contém um erro e o tempo de execução gera uma exceção.
  • A função não consegue alcançar um ponto final de serviço ou excede o tempo limite ao tentar fazê-lo.
  • A função gera intencionalmente uma exceção (por exemplo, quando um parâmetro falha a validação).
  • Uma função Node.js devolve uma promessa rejeitada ou passa um valor não-null para um retorno.

Em qualquer um destes casos, a função deixa de ser executada por predefinição e o evento é rejeitado. Para repetir a função quando ocorre um erro, pode alterar a política de repetição predefinida definindo a propriedade "retry on failure". Isto faz com que o evento seja repetido várias vezes até que a função seja concluída com êxito ou o tempo limite de repetição expire.

Ative ou desative as novas tentativas

Para ativar ou desativar as repetições, pode usar a gcloudferramenta de linha de comandos ou a Google Cloud consola. Por predefinição, as novas tentativas estão desativadas.

Configure as repetições a partir da ferramenta de linha de comandos gcloud

Para ativar as novas tentativas através da ferramenta de linha de comandos gcloud, inclua a flag --retry ao implementar a sua função:

gcloud functions deploy FUNCTION_NAME --retry FLAGS...

Para desativar as novas tentativas, volte a implementar a função sem a flag --retry:

gcloud functions deploy FUNCTION_NAME FLAGS...

Configure novas tentativas a partir da consola

Se estiver a criar uma nova função:

  1. No ecrã Criar função, em Acionador, escolha o tipo de evento que vai atuar como acionador para a sua função.
  2. Selecione a caixa de verificação Tentar novamente em caso de falha para ativar as novas tentativas.

Se estiver a atualizar uma função existente:

  1. Na página Vista geral das funções do Cloud Run, clique no nome da função que está a atualizar para abrir o ecrã Detalhes da função e, de seguida, escolha Editar na barra de menu para apresentar o painel Acionador.
  2. Selecione ou desmarque a caixa de verificação Repetir em caso de falha para ativar ou desativar as repetições.

Práticas recomendadas

Esta secção descreve as práticas recomendadas para usar novas tentativas.

Use a repetição para processar erros momentâneos

Uma vez que a sua função é repetida continuamente até à execução bem-sucedida, os erros permanentes, como erros de programação, devem ser eliminados do seu código através de testes antes de ativar as repetições. As novas tentativas são mais adequadas para lidar com falhas intermitentes ou transitórias que têm uma elevada probabilidade de resolução após uma nova tentativa, como um ponto final de serviço instável ou um limite de tempo.

Defina uma condição de fim para evitar ciclos de repetição infinitos

É uma prática recomendada proteger a sua função contra ciclos contínuos quando usa novas tentativas. Pode fazê-lo incluindo uma condição de fim bem definida antes de a função começar o processamento. Tenha em atenção que esta técnica só funciona se a função for iniciada com êxito e conseguir avaliar a condição final.

Uma abordagem simples, mas eficaz, consiste em rejeitar eventos com data/hora anteriores a um determinado período. Isto ajuda a evitar execuções excessivas quando as falhas são persistentes ou duram mais tempo do que o esperado.

Por exemplo, este fragmento do código rejeita todos os eventos com mais de 10 segundos:

Node.js

/**
 * Background Cloud Function that only executes within
 * a certain time period after the triggering event
 *
 * @param {object} event The Cloud Functions event.
 * @param {function} callback The callback function.
 */
exports.avoidInfiniteRetries = (event, callback) => {
  const eventAge = Date.now() - Date.parse(event.timestamp);
  const eventMaxAge = 10000;

  // Ignore events that are too old
  if (eventAge > eventMaxAge) {
    console.log(`Dropping event ${event} with age ${eventAge} ms.`);
    callback();
    return;
  }

  // Do what the function is supposed to do
  console.log(`Processing event ${event} with age ${eventAge} ms.`);

  // Retry failed function executions
  const failed = false;
  if (failed) {
    callback('some error');
  } else {
    callback();
  }
};

Python

from datetime import datetime, timezone

# The 'python-dateutil' package must be included in requirements.txt.
from dateutil import parser


def avoid_infinite_retries(data, context):
    """Background Cloud Function that only executes within a certain
    time period after the triggering event.

    Args:
        data (dict): The event payload.
        context (google.cloud.functions.Context): The event metadata.
    Returns:
        None; output is written to Stackdriver Logging
    """

    timestamp = context.timestamp

    event_time = parser.parse(timestamp)
    event_age = (datetime.now(timezone.utc) - event_time).total_seconds()
    event_age_ms = event_age * 1000

    # Ignore events that are too old
    max_age_ms = 10000
    if event_age_ms > max_age_ms:
        print(f"Dropped {context.event_id} (age {event_age_ms}ms)")
        return "Timeout"

    # Do what the function is supposed to do
    print(f"Processed {context.event_id} (age {event_age_ms}ms)")
    return  # To retry the execution, raise an exception here

Go


// Package tips contains tips for writing Cloud Functions in Go.
package tips

import (
	"context"
	"fmt"
	"log"
	"time"

	"cloud.google.com/go/functions/metadata"
)

// PubSubMessage is the payload of a Pub/Sub event.
// See the documentation for more details:
// https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage
type PubSubMessage struct {
	Data []byte `json:"data"`
}

// FiniteRetryPubSub demonstrates how to avoid inifinite retries.
func FiniteRetryPubSub(ctx context.Context, m PubSubMessage) error {
	meta, err := metadata.FromContext(ctx)
	if err != nil {
		// Assume an error on the function invoker and try again.
		return fmt.Errorf("metadata.FromContext: %w", err)
	}

	// Ignore events that are too old.
	expiration := meta.Timestamp.Add(10 * time.Second)
	if time.Now().After(expiration) {
		log.Printf("event timeout: halting retries for expired event '%q'", meta.EventID)
		return nil
	}

	// Add your message processing logic.
	return processTheMessage(m)
}

Java


import com.google.cloud.functions.BackgroundFunction;
import com.google.cloud.functions.Context;
import com.google.gson.Gson;
import functions.eventpojos.PubsubMessage;
import java.time.Duration;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.logging.Logger;

public class RetryTimeout implements BackgroundFunction<PubsubMessage> {
  private static final Logger logger = Logger.getLogger(RetryTimeout.class.getName());
  private static final long MAX_EVENT_AGE = 10_000;

  // Use Gson (https://github.com/google/gson) to parse JSON content.
  private static final Gson gson = new Gson();

  /**
   * Background Cloud Function that only executes within
   * a certain time period after the triggering event
   */
  @Override
  public void accept(PubsubMessage message, Context context) {
    ZonedDateTime utcNow = ZonedDateTime.now(ZoneOffset.UTC);
    ZonedDateTime timestamp = ZonedDateTime.parse(context.timestamp());

    long eventAge = Duration.between(timestamp, utcNow).toMillis();

    // Ignore events that are too old
    if (eventAge > MAX_EVENT_AGE) {
      logger.info(String.format("Dropping event with timestamp %s.", timestamp));
      return;
    }

    // Process events that are recent enough
    // To retry this invocation, throw an exception here
    logger.info(String.format("Processing event with timestamp %s.", timestamp));
  }
}

C#

using CloudNative.CloudEvents;
using Google.Cloud.Functions.Framework;
using Google.Events.Protobuf.Cloud.PubSub.V1;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace TimeBoundedRetries;

public class Function : ICloudEventFunction<MessagePublishedData>
{
    private static readonly TimeSpan MaxEventAge = TimeSpan.FromSeconds(10);
    private readonly ILogger _logger;

    // Note: for additional testability, use an injectable clock abstraction.
    public Function(ILogger<Function> logger) =>
        _logger = logger;

    public Task HandleAsync(CloudEvent cloudEvent, MessagePublishedData data, CancellationToken cancellationToken)
    {
        string textData = data.Message.TextData;

        DateTimeOffset utcNow = DateTimeOffset.UtcNow;

        // Every PubSub CloudEvent will contain a timestamp.
        DateTimeOffset timestamp = cloudEvent.Time.Value;
        DateTimeOffset expiry = timestamp + MaxEventAge;

        // Ignore events that are too old.
        if (utcNow > expiry)
        {
            _logger.LogInformation("Dropping PubSub message '{text}'", textData);
            return Task.CompletedTask;
        }

        // Process events that are recent enough.
        // If this processing throws an exception, the message will be retried until either
        // processing succeeds or the event becomes too old and is dropped by the code above.
        _logger.LogInformation("Processing PubSub message '{text}'", textData);
        return Task.CompletedTask;
    }
}

Ruby

require "functions_framework"

FunctionsFramework.cloud_event "avoid_infinite_retries" do |event|
  # Use the event timestamp to determine the event age.
  event_age_secs = Time.now - event.time.to_time
  event_age_ms = (event_age_secs * 1000).to_i

  max_age_ms = 10_000
  if event_age_ms > max_age_ms
    # Ignore events that are too old.
    logger.info "Dropped #{event.id} (age #{event_age_ms}ms)"

  else
    # Do what the function is supposed to do.
    logger.info "Handling #{event.id} (age #{event_age_ms}ms)..."
    failed = true

    # Raise an exception to signal failure and trigger a retry.
    raise "I failed!" if failed
  end
end

PHP

/**
 * This function shows an example method for avoiding infinite retries in
 * Google Cloud Functions. By default, functions configured to automatically
 * retry execution on failure will be retried indefinitely - causing an
 * infinite loop. To avoid this, we stop retrying executions (by not throwing
 * exceptions) for any events that are older than a predefined threshold.
 */

use Google\CloudFunctions\CloudEvent;

function avoidInfiniteRetries(CloudEvent $event): void
{
    $log = fopen(getenv('LOGGER_OUTPUT') ?: 'php://stderr', 'wb');

    $eventId = $event->getId();

    // The maximum age of events to process.
    $maxAge = 10; // 10 seconds

    // The age of the event being processed.
    $eventAge = time() - strtotime($event->getTime());

    // Ignore events that are too old
    if ($eventAge > $maxAge) {
        fwrite($log, 'Dropping event ' . $eventId . ' with age ' . $eventAge . ' seconds' . PHP_EOL);
        return;
    }

    // Do what the function is supposed to do
    fwrite($log, 'Processing event: ' . $eventId . ' with age ' . $eventAge . ' seconds' . PHP_EOL);

    // infinite_retries failed function executions
    $failed = true;
    if ($failed) {
        throw new Exception('Event ' . $eventId . ' failed; retrying...');
    }
}

Distinguir entre funções que podem ser repetidas e erros fatais

Se a sua função tiver repetições ativadas, qualquer erro não processado aciona uma repetição. Certifique-se de que o seu código capta todos os erros que não devem resultar numa nova tentativa.

Node.js

/**
 * Background Cloud Function that demonstrates
 * how to toggle retries using a promise
 *
 * @param {object} event The Cloud Functions event.
 * @param {object} event.data Data included with the event.
 * @param {object} event.data.retry User-supplied parameter that tells the function whether to retry.
 */
exports.retryPromise = event => {
  const tryAgain = !!event.data.retry;

  if (tryAgain) {
    throw new Error('Retrying...');
  } else {
    console.error('Not retrying...');
    return Promise.resolve();
  }
};

/**
 * Background Cloud Function that demonstrates
 * how to toggle retries using a callback
 *
 * @param {object} event The Cloud Functions event.
 * @param {object} event.data Data included with the event.
 * @param {object} event.data.retry User-supplied parameter that tells the function whether to retry.
 * @param {function} callback The callback function.
 */
exports.retryCallback = (event, callback) => {
  const tryAgain = !!event.data.retry;
  const err = new Error('Error!');

  if (tryAgain) {
    console.error('Retrying:', err);
    callback(err);
  } else {
    console.error('Not retrying:', err);
    callback();
  }
};

Python

from google.cloud import error_reporting


error_client = error_reporting.Client()


def retry_or_not(data, context):
    """Background Cloud Function that demonstrates how to toggle retries.

    Args:
        data (dict): The event payload.
        context (google.cloud.functions.Context): The event metadata.
    Returns:
        None; output is written to Stackdriver Logging
    """

    # Retry based on a user-defined parameter
    try_again = data.data.get("retry") is not None

    try:
        raise RuntimeError("I failed you")
    except RuntimeError:
        error_client.report_exception()
        if try_again:
            raise  # Raise the exception and try again
        else:
            pass  # Swallow the exception and don't retry

Go


// Package tips contains tips for writing Cloud Functions in Go.
package tips

import (
	"context"
	"errors"
	"log"
)

// PubSubMessage is the payload of a Pub/Sub event.
// See the documentation for more details:
// https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage
type PubSubMessage struct {
	Data []byte `json:"data"`
}

// RetryPubSub demonstrates how to toggle using retries.
func RetryPubSub(ctx context.Context, m PubSubMessage) error {
	name := string(m.Data)
	if name == "" {
		name = "World"
	}

	// A misconfigured client will stay broken until the function is redeployed.
	client, err := MisconfiguredDataClient()
	if err != nil {
		log.Printf("MisconfiguredDataClient (retry denied):  %v", err)
		// A nil return indicates that the function does not need a retry.
		return nil
	}

	// Runtime error might be resolved with a new attempt.
	if err = FailedWriteOperation(client, name); err != nil {
		log.Printf("FailedWriteOperation (retry expected): %v", err)
		// A non-nil return indicates that a retry is needed.
		return err
	}

	return nil
}

Java


import com.google.cloud.functions.BackgroundFunction;
import com.google.cloud.functions.Context;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import functions.eventpojos.PubsubMessage;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.logging.Logger;

public class RetryPubSub implements BackgroundFunction<PubsubMessage> {
  private static final Logger logger = Logger.getLogger(RetryPubSub.class.getName());

  // Use Gson (https://github.com/google/gson) to parse JSON content.
  private static final Gson gson = new Gson();

  @Override
  public void accept(PubsubMessage message, Context context) {
    String bodyJson = new String(
        Base64.getDecoder().decode(message.getData()), StandardCharsets.UTF_8);
    JsonElement bodyElement = gson.fromJson(bodyJson, JsonElement.class);

    // Get the value of the "retry" JSON parameter, if one exists
    boolean retry = false;
    if (bodyElement != null && bodyElement.isJsonObject()) {
      JsonObject body = bodyElement.getAsJsonObject();

      if (body.has("retry") && body.get("retry").getAsBoolean()) {
        retry = true;
      }
    }

    // Retry if appropriate
    if (retry) {
      // Throwing an exception causes the execution to be retried
      throw new RuntimeException("Retrying...");
    } else {
      logger.info("Not retrying...");
    }
  }
}

C#

using CloudNative.CloudEvents;
using Google.Cloud.Functions.Framework;
using Google.Events.Protobuf.Cloud.PubSub.V1;
using Microsoft.Extensions.Logging;
using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

namespace Retry;

public class Function : ICloudEventFunction<MessagePublishedData>
{
    private readonly ILogger _logger;

    public Function(ILogger<Function> logger) =>
        _logger = logger;

    public Task HandleAsync(CloudEvent cloudEvent, MessagePublishedData data, CancellationToken cancellationToken)
    {
        bool retry = false;
        string text = data.Message?.TextData;

        // Get the value of the "retry" JSON parameter, if one exists.
        if (!string.IsNullOrEmpty(text))
        {
            JsonElement element = JsonSerializer.Deserialize<JsonElement>(data.Message.TextData);

            retry = element.TryGetProperty("retry", out var property) &&
                property.ValueKind == JsonValueKind.True;
        }

        // Throwing an exception causes the execution to be retried.
        if (retry)
        {
            throw new InvalidOperationException("Retrying...");
        }
        else
        {
            _logger.LogInformation("Not retrying...");
        }
        return Task.CompletedTask;
    }
}

Ruby

require "functions_framework"

FunctionsFramework.cloud_event "retry_or_not" do |event|
  try_again = event.data["retry"]

  begin
    # Simulate a failure
    raise "I failed!"
  rescue RuntimeError => e
    logger.warn "Caught an error: #{e}"
    if try_again
      # Raise an exception to return a 500 and trigger a retry.
      logger.info "Trying again..."
      raise ex
    else
      # Return normally to end processing of this event.
      logger.info "Giving up."
    end
  end
end

PHP

use Google\CloudFunctions\CloudEvent;

function tipsRetry(CloudEvent $event): void
{
    $cloudEventData = $event->getData();
    $pubSubData = $cloudEventData['message']['data'];

    $json = json_decode(base64_decode($pubSubData), true);

    // Determine whether to retry the invocation based on a parameter
    $tryAgain = $json['some_parameter'];

    if ($tryAgain) {
        /**
         * Functions with automatic retries enabled should throw exceptions to
         * indicate intermittent failures that a retry might fix. In this
         * case, a thrown exception will cause the original function
         * invocation to be re-sent.
         */
        throw new Exception('Intermittent failure occurred; retrying...');
    }

    /**
     * If a function with retries enabled encounters a non-retriable
     * failure, it should return *without* throwing an exception.
     */
    $log = fopen(getenv('LOGGER_OUTPUT') ?: 'php://stderr', 'wb');
    fwrite($log, 'Not retrying' . PHP_EOL);
}

Torne as funções acionadas por eventos repetíveis idempotentes

As funções acionadas por eventos que podem ser repetidas têm de ser idempotentes. Seguem-se algumas diretrizes gerais para tornar uma função deste tipo idempotente:

  • Muitas APIs externas (como a Stripe) permitem fornecer uma chave de idempotência como um parâmetro. Se estiver a usar uma API deste tipo, deve usar o ID do evento como chave de idempotência.
  • A idempotência funciona bem com o fornecimento pelo menos uma vez, porque torna a repetição segura. Assim, uma prática recomendada geral para escrever código fiável é combinar a idempotência com as novas tentativas.
  • Certifique-se de que o seu código é idempotente internamente. Por exemplo:
    • Certifique-se de que as mutações podem ocorrer mais do que uma vez sem alterar o resultado.
    • Consultar o estado da base de dados numa transação antes de alterar o estado.
    • Certifique-se de que todos os efeitos secundários são idempotentes.
  • Impor uma verificação transacional fora da função, independentemente do código. Por exemplo, manter o estado num local que registe que um determinado ID do evento já foi processado.
  • Lidar com chamadas de funções duplicadas fora da banda. Por exemplo, ter um processo de limpeza separado que limpe após chamadas de funções duplicadas.

Configure a política de repetição

Consoante as necessidades da sua função, pode querer configurar a política de repetição diretamente. Isto permite-lhe configurar qualquer combinação do seguinte:

  • Reduzir o período de nova tentativa de 7 dias para apenas 10 minutos.
  • Altere o tempo de recuo mínimo e máximo para a estratégia de repetição de recuo exponencial.
  • Altere a estratégia de repetição para repetir imediatamente.
  • Configure um tópico de mensagens não entregues.
  • Defina um número máximo e mínimo de tentativas de entrega.

Para configurar a política de repetição:

  1. Escrever uma função HTTP.
  2. Use a API Pub/Sub para criar uma subscrição do Pub/Sub, especificando o URL da função como o destino.

Consulte a documentação do Pub/Sub sobre como processar falhas para obter mais informações sobre a configuração direta do Pub/Sub.

Passos seguintes