הפעלת פונקציות באמצעות מסמכי Firestore

טריגרים של Firestore מאפשרים להפעיל פונקציות באופן אוטומטי בכל פעם שנוצרים, מתעדכנים או נמחקים מסמכים באוסף מסוים. כך אפשר לעבד שינויים בנתונים בזמן אמת, לסנכרן מערכות חיצוניות ולהפוך תהליכי עבודה לקצה העורפי לאוטומטיים.

לפני שמתחילים

לפני שמריצים את הקוד לדוגמה במדריך הזה, צריך לבצע את הפעולות הבאות:

דוגמאות

בדוגמאות הבאות מוסבר איך לכתוב פונקציות שמגיבות לטריגר של Firestore.

דוגמה 1: פונקציית Hello Firestore

הדוגמה הבאה מדפיסה את השדות של אירוע Firestore שמפעיל את הפונקציה:

Node.js

/**
 * Cloud Event Function triggered by a change to a Firestore document.
 */
const functions = require('@google-cloud/functions-framework');
const protobuf = require('protobufjs');

functions.cloudEvent('helloFirestore', async cloudEvent => {
  console.log(`Function triggered by event on: ${cloudEvent.source}`);
  console.log(`Event type: ${cloudEvent.type}`);

  console.log('Loading protos...');
  const root = await protobuf.load('data.proto');
  const DocumentEventData = root.lookupType(
    'google.events.cloud.firestore.v1.DocumentEventData'
  );

  console.log('Decoding data...');
  const firestoreReceived = DocumentEventData.decode(cloudEvent.data);

  console.log('\nOld value:');
  console.log(JSON.stringify(firestoreReceived.oldValue, null, 2));

  console.log('\nNew value:');
  console.log(JSON.stringify(firestoreReceived.value, null, 2));
});

Python

from cloudevents.http import CloudEvent
import functions_framework
from google.events.cloud import firestore


@functions_framework.cloud_event
def hello_firestore(cloud_event: CloudEvent) -> None:
    """Triggers by a change to a Firestore document.

    Args:
        cloud_event: cloud event with information on the firestore event trigger
    """
    firestore_payload = firestore.DocumentEventData()
    firestore_payload._pb.ParseFromString(cloud_event.data)

    print(f"Function triggered by change to: {cloud_event['source']}")

    print("\nOld value:")
    print(firestore_payload.old_value)

    print("\nNew value:")
    print(firestore_payload.value)

Go


// Package hellofirestore contains a Cloud Event Function triggered by a Cloud Firestore event.
package hellofirestore

import (
	"context"
	"fmt"

	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
	"github.com/cloudevents/sdk-go/v2/event"
	"github.com/googleapis/google-cloudevents-go/cloud/firestoredata"
	"google.golang.org/protobuf/proto"
)

func init() {
	functions.CloudEvent("helloFirestore", HelloFirestore)
}

// HelloFirestore is triggered by a change to a Firestore document.
func HelloFirestore(ctx context.Context, event event.Event) error {
	var data firestoredata.DocumentEventData

	// If you omit `DiscardUnknown`, protojson.Unmarshal returns an error
	// when encountering a new or unknown field.
	options := proto.UnmarshalOptions{
		DiscardUnknown: true,
	}
	err := options.Unmarshal(event.Data(), &data)

	if err != nil {
		return fmt.Errorf("proto.Unmarshal: %w", err)
	}

	fmt.Printf("Function triggered by change to: %v\n", event.Source())
	fmt.Printf("Old value: %+v\n", data.GetOldValue())
	fmt.Printf("New value: %+v\n", data.GetValue())
	return nil
}

Java

import com.google.cloud.functions.CloudEventsFunction;
import com.google.events.cloud.firestore.v1.DocumentEventData;
import com.google.protobuf.InvalidProtocolBufferException;
import io.cloudevents.CloudEvent;
import java.util.logging.Logger;

public class FirebaseFirestore implements CloudEventsFunction {
  private static final Logger logger = Logger.getLogger(FirebaseFirestore.class.getName());

  @Override
  public void accept(CloudEvent event) throws InvalidProtocolBufferException {
    DocumentEventData firestoreEventData = DocumentEventData
        .parseFrom(event.getData().toBytes());

    logger.info("Function triggered by event on: " + event.getSource());
    logger.info("Event type: " + event.getType());

    logger.info("Old value:");
    logger.info(firestoreEventData.getOldValue().toString());

    logger.info("New value:");
    logger.info(firestoreEventData.getValue().toString());
  }
}

C#‎

using CloudNative.CloudEvents;
using Google.Cloud.Functions.Framework;
using Google.Events.Protobuf.Cloud.Firestore.V1;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace FirebaseFirestore;

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

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

    public Task HandleAsync(CloudEvent cloudEvent, DocumentEventData data, CancellationToken cancellationToken)
    {
        _logger.LogInformation("Function triggered by event on {subject}", cloudEvent.Subject);
        _logger.LogInformation("Event type: {type}", cloudEvent.Type);
        MaybeLogDocument("Old value", data.OldValue);
        MaybeLogDocument("New value", data.Value);

        // In this example, we don't need to perform any asynchronous operations, so the
        // method doesn't need to be declared async.
        return Task.CompletedTask;
    }

    /// <summary>
    /// Logs the names and values of the fields in a document in a very simplistic way.
    /// </summary>
    private void MaybeLogDocument(string message, Document document)
    {
        if (document is null)
        {
            return;
        }

        // ConvertFields converts the Firestore representation into a .NET-friendly
        // representation.
        IReadOnlyDictionary<string, object> fields = document.ConvertFields();
        var fieldNamesAndTypes = fields
            .OrderBy(pair => pair.Key)
            .Select(pair => $"{pair.Key}: {pair.Value}");
        _logger.LogInformation(message + ": {fields}", string.Join(", ", fieldNamesAndTypes));
    }
}

פריסת הפונקציה Hello Firestore

אם עדיין לא עשיתם את זה, הגדירו את מסד הנתונים של Firestore.

אחרי שפורסים פונקציה, אפשר להגדיר טריגר באמצעות מסוף Google Cloud , ה-CLI של Google Cloud או Terraform.

המסוף

כשמשתמשים במסוף Google Cloud כדי ליצור פונקציה, אפשר גם להוסיף לה טריגר. כדי ליצור טריגר לפונקציה:

  1. נכנסים ל-Cloud Run במסוף Google Cloud :

    כניסה ל-Cloud Run

  2. לוחצים על כתיבת פונקציה ומזינים את פרטי הפונקציה. מידע נוסף על הגדרת פונקציות במהלך הפריסה זמין במאמר פריסת פונקציות.

  3. בקטע Trigger (טריגר), לוחצים על Add trigger (הוספת טריגר).

  4. בוחרים באפשרות Firestore trigger.

  5. בחלונית Eventarc trigger, משנים את פרטי הטריגר באופן הבא:

    1. בשדה Trigger name, מזינים שם לטריגר או משתמשים בשם ברירת המחדל.

    2. בוחרים סוג טריגר מהרשימה:

      • מקורות של Google כדי לציין טריגרים ל-Pub/Sub, ל-Cloud Storage, ל-Firestore ולספקי אירועים אחרים של Google.

      • צד שלישי כדי לשלב עם ספקים שאינם של Google שמציעים מקור Eventarc. מידע נוסף זמין במאמר אירועים של צד שלישי ב-Eventarc.

    3. בוחרים באפשרות Cloud Firestore מהרשימה Event provider, כדי לבחור מוצר שמספק את סוג האירוע להפעלת הפונקציה. רשימת ספקי האירועים מופיעה במאמר ספקים ויעדים של אירועים.

    4. בוחרים באפשרות type=google.cloud.firestore.document.v1.written מהרשימה Event type. הגדרת הטריגר משתנה בהתאם לסוג האירוע הנתמך. מידע נוסף מופיע במאמר סוגי אירועים.

    5. משאירים את השדה סוג התוכן של נתוני האירועים כמו שהוא.

    6. בקטע Filters, בוחרים מסד נתונים, פעולה וערכי מאפיינים, או משתמשים בברירות המחדל.

    7. אם השדה Region מופעל, בוחרים מיקום לטריגר של Eventarc. באופן כללי, המיקום של הטריגר של Eventarc צריך להתאים למיקום של המשאב ב-Google Cloud שאתם רוצים לעקוב אחרי האירועים שלו. ברוב המקרים, כדאי לפרוס את הפונקציה באותו אזור. למידע נוסף על מיקומי הטריגרים של Eventarc, קראו את המאמר הסבר על מיקומי Eventarc.

    8. בשדה Service account, בוחרים חשבון שירות. הטריגרים של Eventarc מקושרים לחשבונות שירות כדי להשתמש בהם כזהות כשמפעילים את הפונקציה. לחשבון השירות של הטריגר ב-Eventarc צריכה להיות הרשאה להפעיל את הפונקציה. כברירת מחדל, Cloud Run משתמש בחשבון השירות שמוגדר כברירת מחדל ב-Compute Engine.

    9. אם רוצים, מציינים את נתיב כתובת ה-URL של השירות שאליו תישלח הבקשה הנכנסת. זהו הנתיב היחסי בשירות היעד שאליו צריך לשלוח את האירועים של הטריגר. לדוגמה: /, /route, route ו-route/subroute.

    10. אם רוצים לאפשר ניסיונות חוזרים במקרה של כישלון, מסמנים את התיבה Enable retry on failure. אחרת, פעולת ברירת המחדל היא ניסיון מסירה אחד ללא ניסיונות חוזרים. מידע נוסף מופיע במאמר אירועי ניסיון חוזר.

  6. אחרי שממלאים את שדות החובה, לוחצים על שמירת הטריגר.

  7. לוחצים על יצירה.

  8. בכרטיסייה Source, עורכים את קוד המקור לפי הצורך ולוחצים על Save and redeploy.

gcloud

כשיוצרים פונקציה באמצעות ה-CLI של gcloud, קודם צריך לפרוס את הפונקציה ואז ליצור טריגר. כדי ליצור טריגר לפונקציה:

  1. מריצים את הפקודה הבאה בספרייה שמכילה את הקוד לדוגמה כדי לפרוס את הפונקציה:

    gcloud run deploy FUNCTION \
        --source . \
        --function FUNCTION_ENTRYPOINT \
        --base-image BASE_IMAGE_ID \
        --region REGION
    

    מחליפים את מה שכתוב בשדות הבאים:

    • ‫FUNCTION: שם הפונקציה שאתם פורסים. אפשר להשמיט את הפרמטר הזה לגמרי, אבל אם תשמיטו אותו תתבקשו להזין את השם.

    • ‫FUNCTION_ENTRYPOINT: נקודת הכניסה לפונקציה בקוד המקור. זה הקוד ש-Cloud Run מריץ כשהפונקציה פועלת. הערך של הדגל הזה חייב להיות שם של פונקציה או שם מלא של מחלקה שקיימים בקוד המקור.

    • ‫BASE_IMAGE_ID: סביבת קובץ האימג' הבסיסי של הפונקציה. לפרטים נוספים על קובצי אימג' בסיסיים ועל החבילות שכלולות בכל קובץ אימג', קראו את המאמר קובצי אימג' בסיסיים של סביבות זמן ריצה.

    • ‫REGION: האזור Google Cloudשבו רוצים לפרוס את הפונקציה. לדוגמה, europe-west1.

  2. מריצים את הפקודה הבאה כדי ליצור טריגר שמסנן אירועים ומנתב אותם:

    gcloud eventarc triggers create TRIGGER_NAME  \
        --location=LOCATION \
        --destination-run-service=FUNCTION  \
        --destination-run-region=DESTINATION_RUN_REGION \
        --event-filters="type=EVENT_FILTER_TYPE" \
        --event-filters=database='(default)' \
        --event-data-content-type=application/protobuf \
        --event-filters-path-pattern=document='users/{username}' \
        --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.
    

    מחליפים את מה שכתוב בשדות הבאים:

    • ‫TRIGGER_NAME: המזהה של הטריגר או מזהה מלא.
    • ‫LOCATION: המיקום של הטריגר של Eventarc. אפשר גם להגדיר את המאפיין eventarc/location, לדוגמה gcloud config set eventarc/location us-central1.

      כדי למנוע בעיות בביצועים ובמיקום אחסון הנתונים, המיקום צריך להתאים למיקום של שירות Google Cloud שיוצר אירועים. מידע נוסף זמין במאמר מיקומי Eventarc.

    • ‫FUNCTION: השם של פונקציית Cloud Run שנפרסה ומקבלת את האירועים של הטריגר.
    • ‫DESTINATION_RUN_REGION: (אופציונלי) המיקום ב-Cloud Run שבו נמצאת פונקציית היעד של Cloud Run. אם לא מציינים אזור, ההנחה היא שהפונקציה נמצאת באותו אזור של הטריגר.
    • ‫EVENT_FILTER_TYPE: המזהה של האירוע. אירוע נוצר כשקריאה ל-API של ה-method מצליחה. בפעולות שנמשכות זמן רב, האירוע נוצר רק בסוף הפעולה, ורק אם הפעולה בוצעה בהצלחה. רשימה של סוגי האירועים הנתמכים מופיעה במאמר סוגי האירועים של Google שנתמכים ב-Eventarc.
    • ‫SERVICE_ACCOUNT_NAME: השם של חשבון השירות שמנוהל על ידי המשתמש.
    • ‫PROJECT_ID: מזהה הפרויקט ב- Google Cloud .

    הערות:

    • אחרי שיוצרים טריגר, אי אפשר לשנות את סוג מסנן האירועים. כדי להשתמש בסוג אירוע אחר, צריך ליצור טריגר חדש.
    • --event-filters=type=google.cloud.firestore.document.v1.written מציין שהפונקציה מופעלת כשמסמך נוצר, מתעדכן או נמחק, בהתאם לסוג האירוע.
    • ‫--event-filters=database='(default)' מציין את מסד הנתונים של Firebase. שם ברירת המחדל של מסד הנתונים הוא (default).
    • --event-filters-path-pattern=document='users/{username}' מציין את תבנית הנתיב של המסמכים שצריך לעקוב אחריהם כדי לזהות שינויים רלוונטיים. תבנית הנתיב הזו מציינת שצריך לעקוב אחרי כל המסמכים באוסף users. מידע נוסף זמין במאמר הסבר על תבניות נתיב.
    • אם רוצים לציין ניסיון אחד לשליחת אירוע בלי ניסיונות חוזרים, משתמשים בדגל --max-retry-attempts. הערך התקין היחיד הוא 1. אם לא מציינים את הדגל, חלה התנהגות ברירת המחדל של ניסיון חוזר. מידע נוסף זמין במאמר ניסיון חוזר של אירועים.
    • יש עוד דגלים שאפשר להשתמש בהם. מידע נוסף זמין במאמר gcloud eventarc triggers create.

Terraform

במאמר יצירת טריגר באמצעות Terraform מוסבר איך יוצרים טריגר של Eventarc לפונקציית Cloud Run.

בדיקת הפונקציה Hello Firestore

כדי לבדוק את הפונקציה Hello Firestore, מגדירים אוסף בשם users במסד הנתונים של Firestore:

  1. במסוף Google Cloud , נכנסים לדף מסדי הנתונים של Firestore:

    כניסה ל-Firestore

  2. לוחצים על התחלת אוסף.

  3. מציינים את הערך users כמזהה האוסף.

  4. כדי להתחיל להוסיף את המסמך הראשון לאוסף, בקטע Add its first document מאשרים את הערך שנוצר אוטומטית בשדה Document ID.

  5. מוסיפים לפחות שדה אחד למסמך, ומציינים שם וערך. לדוגמה, בשדה Field name מזינים username, ובשדה Field value מזינים rowan.

  6. כשתסיים, לחץ על שמור.

    הפעולה הזו יוצרת מסמך חדש, וכך מפעילה את הפונקציה.

  7. כדי לוודא שהפונקציה הופעלה, לוחצים על השם המקושר של הפונקציה בדף הסקירה הכללית של Cloud Run במסוף Google Cloud כדי לפתוח את הדף Service details.

  8. בכרטיסייה Observability, בוחרים בכרטיסייה Logs ומחפשים את המחרוזת הבאה:

    Function triggered by change to: //firestore.googleapis.com/projects/your-project-id/databases/(default)'

דוגמה 2: הפונקציה "המרת הטקסט לאותיות רישיות (אותיות גדולות)"

בדוגמה הבאה, המערכת מאחזרת את הערך שהמשתמש הוסיף, ממירה את המחרוזת במיקום הזה לאותיות רישיות ומחליפה את הערך במחרוזת עם האותיות הרישיות:

Node.js

משתמשים ב-protobufjs כדי לפענח את נתוני האירוע. מוסיפים את google.events.cloud.firestore.v1 data.proto למקור.

const functions = require('@google-cloud/functions-framework');
const Firestore = require('@google-cloud/firestore');
const protobuf = require('protobufjs');

const firestore = new Firestore({
  projectId: process.env.GOOGLE_CLOUD_PROJECT,
});

// Converts strings added to /messages/{pushId}/original to uppercase
functions.cloudEvent('makeUpperCase', async cloudEvent => {
  console.log('Loading protos...');
  const root = await protobuf.load('data.proto');
  const DocumentEventData = root.lookupType(
    'google.events.cloud.firestore.v1.DocumentEventData'
  );

  console.log('Decoding data...');
  const firestoreReceived = DocumentEventData.decode(cloudEvent.data);

  const resource = firestoreReceived.value.name;
  const affectedDoc = firestore.doc(resource.split('/documents/')[1]);

  const curValue = firestoreReceived.value.fields.original.stringValue;
  const newValue = curValue.toUpperCase();

  if (curValue === newValue) {
    // Value is already upper-case
    // Don't perform a(nother) write to avoid infinite loops
    console.log('Value is already upper-case.');
    return;
  }

  console.log(`Replacing value: ${curValue} --> ${newValue}`);
  affectedDoc.set({
    original: newValue,
  });
});

Python

from cloudevents.http import CloudEvent
import functions_framework
from google.cloud import firestore
from google.events.cloud import firestore as firestoredata

client = firestore.Client()


# Converts strings added to /messages/{pushId}/original to uppercase
@functions_framework.cloud_event
def make_upper_case(cloud_event: CloudEvent) -> None:
    firestore_payload = firestoredata.DocumentEventData()
    firestore_payload._pb.ParseFromString(cloud_event.data)

    path_parts = firestore_payload.value.name.split("/")
    separator_idx = path_parts.index("documents")
    collection_path = path_parts[separator_idx + 1]
    document_path = "/".join(path_parts[(separator_idx + 2) :])

    print(f"Collection path: {collection_path}")
    print(f"Document path: {document_path}")

    affected_doc = client.collection(collection_path).document(document_path)

    cur_value = firestore_payload.value.fields["original"].string_value
    new_value = cur_value.upper()

    if cur_value != new_value:
        print(f"Replacing value: {cur_value} --> {new_value}")
        affected_doc.set({"original": new_value})
    else:
        # Value is already upper-case
        # Don't perform a second write (which can trigger an infinite loop)
        print("Value is already upper-case.")

Go


// Package upper contains a Firestore Cloud Function.
package upper

import (
	"context"
	"errors"
	"fmt"
	"log"
	"os"
	"strings"

	"cloud.google.com/go/firestore"
	firebase "firebase.google.com/go/v4"
	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
	"github.com/cloudevents/sdk-go/v2/event"
	"github.com/googleapis/google-cloudevents-go/cloud/firestoredata"
	"google.golang.org/protobuf/proto"
)

// set the GOOGLE_CLOUD_PROJECT environment variable when deploying.
var projectID = os.Getenv("GOOGLE_CLOUD_PROJECT")

// client is a Firestore client, reused between function invocations.
var client *firestore.Client

func init() {
	// Use the application default credentials.
	conf := &firebase.Config{ProjectID: projectID}

	// Use context.Background() because the app/client should persist across
	// invocations.
	ctx := context.Background()

	app, err := firebase.NewApp(ctx, conf)
	if err != nil {
		log.Fatalf("firebase.NewApp: %v", err)
	}

	client, err = app.Firestore(ctx)
	if err != nil {
		log.Fatalf("app.Firestore: %v", err)
	}

	// Register cloud event function
	functions.CloudEvent("MakeUpperCase", MakeUpperCase)
}

// MakeUpperCase is triggered by a change to a Firestore document. It updates
// the `original` value of the document to upper case.
func MakeUpperCase(ctx context.Context, e event.Event) error {
	var data firestoredata.DocumentEventData

	// If you omit `DiscardUnknown`, protojson.Unmarshal returns an error
	// when encountering a new or unknown field.
	options := proto.UnmarshalOptions{
		DiscardUnknown: true,
	}
	err := options.Unmarshal(e.Data(), &data)

	if err != nil {
		return fmt.Errorf("proto.Unmarshal: %w", err)
	}

	if data.GetValue() == nil {
		return errors.New("Invalid message: 'Value' not present")
	}

	fullPath := strings.Split(data.GetValue().GetName(), "/documents/")[1]
	pathParts := strings.Split(fullPath, "/")
	collection := pathParts[0]
	doc := strings.Join(pathParts[1:], "/")

	var originalStringValue string
	if v, ok := data.GetValue().GetFields()["original"]; ok {
		originalStringValue = v.GetStringValue()
	} else {
		return errors.New("Document did not contain field \"original\"")
	}

	newValue := strings.ToUpper(originalStringValue)
	if originalStringValue == newValue {
		log.Printf("%q is already upper case: skipping", originalStringValue)
		return nil
	}
	log.Printf("Replacing value: %q -> %q", originalStringValue, newValue)

	newDocumentEntry := map[string]string{"original": newValue}
	_, err = client.Collection(collection).Doc(doc).Set(ctx, newDocumentEntry)
	if err != nil {
		return fmt.Errorf("Set: %w", err)
	}
	return nil
}

Java

import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;
import com.google.cloud.firestore.SetOptions;
import com.google.cloud.functions.CloudEventsFunction;
import com.google.events.cloud.firestore.v1.DocumentEventData;
import com.google.events.cloud.firestore.v1.Value;
import com.google.protobuf.InvalidProtocolBufferException;
import io.cloudevents.CloudEvent;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;

public class FirebaseFirestoreReactive implements CloudEventsFunction {
  private static final Logger logger = Logger.getLogger(FirebaseFirestoreReactive.class.getName());
  private final Firestore firestore;

  private static final String FIELD_KEY = "original";
  private static final String APPLICATION_PROTOBUF = "application/protobuf";

  public FirebaseFirestoreReactive() {
    this(FirestoreOptions.getDefaultInstance().getService());
  }

  public FirebaseFirestoreReactive(Firestore firestore) {
    this.firestore = firestore;
  }

  @Override
  public void accept(CloudEvent event)
      throws InvalidProtocolBufferException, InterruptedException, ExecutionException {
    if (event.getData() == null) {
      logger.warning("No data found in event!");
      return;
    }

    if (!event.getDataContentType().equals(APPLICATION_PROTOBUF)) {
      logger.warning(String.format("Found unexpected content type %s, expected %s",
          event.getDataContentType(),
          APPLICATION_PROTOBUF));
      return;
    }

    DocumentEventData firestoreEventData = DocumentEventData
        .parseFrom(event.getData().toBytes());

    // Get the fields from the post-operation document snapshot
    // https://firebase.google.com/docs/firestore/reference/rest/v1/projects.databases.documents#Document
    Map<String, Value> fields = firestoreEventData.getValue().getFieldsMap();
    if (!fields.containsKey(FIELD_KEY)) {
      logger.warning("Document does not contain original field");
      return;
    }
    String currValue = fields.get(FIELD_KEY).getStringValue();
    String newValue = currValue.toUpperCase();

    if (currValue.equals(newValue)) {
      logger.info("Value is already upper-case");
      return;
    }

    // Retrieve the document name from the resource path:
    // projects/{project_id}/databases/{database_id}/documents/{document_path}
    String affectedDoc = firestoreEventData.getValue()
        .getName()
        .split("/documents/")[1]
        .replace("\"", "");

    logger.info(String.format("Replacing values: %s --> %s", currValue, newValue));

    // Wait for the async call to complete
    this.firestore
        .document(affectedDoc)
        .set(Map.of(FIELD_KEY, newValue), SetOptions.merge())
        .get();
  }
}

C#‎

using CloudNative.CloudEvents;
using Google.Cloud.Firestore;
using Google.Cloud.Functions.Framework;
using Google.Cloud.Functions.Hosting;
using Google.Events.Protobuf.Cloud.Firestore.V1;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace FirestoreReactive;

public class Startup : FunctionsStartup
{
    public override void ConfigureServices(WebHostBuilderContext context, IServiceCollection services) =>
        services.AddSingleton(FirestoreDb.Create());
}

// Register the startup class to provide the Firestore dependency.
[FunctionsStartup(typeof(Startup))]
public class Function : ICloudEventFunction<DocumentEventData>
{
    private readonly ILogger _logger;
    private readonly FirestoreDb _firestoreDb;

    public Function(ILogger<Function> logger, FirestoreDb firestoreDb) =>
        (_logger, _firestoreDb) = (logger, firestoreDb);

    public async Task HandleAsync(CloudEvent cloudEvent, DocumentEventData data, CancellationToken cancellationToken)
    {
        // Get the recently-written value. This expression will result in a null value
        // if any of the following is true:
        // - The event doesn't contain a "new" document
        // - The value doesn't contain a field called "original"
        // - The "original" field isn't a string
        string currentValue = data.Value?.ConvertFields().GetValueOrDefault("original") as string;
        if (currentValue is null)
        {
            _logger.LogWarning($"Event did not contain a suitable document");
            return;
        }

        string newValue = currentValue.ToUpperInvariant();
        if (newValue == currentValue)
        {
            _logger.LogInformation("Value is already upper-cased; no replacement necessary");
            return;
        }

        // The CloudEvent subject is "documents/x/y/...".
        // The Firestore SDK FirestoreDb.Document method expects a reference relative to
        // "documents" (so just the "x/y/..." part). This may be simplified over time.
        if (cloudEvent.Subject is null || !cloudEvent.Subject.StartsWith("documents/"))
        {
            _logger.LogWarning("CloudEvent subject is not a document reference.");
            return;
        }
        string documentPath = cloudEvent.Subject.Substring("documents/".Length);

        _logger.LogInformation("Replacing '{current}' with '{new}' in '{path}'", currentValue, newValue, documentPath);
        await _firestoreDb.Document(documentPath).UpdateAsync("original", newValue, cancellationToken: cancellationToken);
    }
}

פריסת הפונקציה Convert to Uppercase

אם עדיין לא עשיתם את זה, הגדירו את מסד הנתונים של Firestore.

אחרי שפורסים פונקציה, אפשר להגדיר טריגר באמצעות מסוף Google Cloud , ה-CLI של Google Cloud או Terraform.

המסוף

כשמשתמשים במסוף Google Cloud כדי ליצור פונקציה, אפשר גם להוסיף לה טריגר. כדי ליצור טריגר לפונקציה:

  1. נכנסים ל-Cloud Run במסוף Google Cloud :

    כניסה ל-Cloud Run

  2. לוחצים על כתיבת פונקציה ומזינים את פרטי הפונקציה. מידע נוסף על הגדרת פונקציות במהלך הפריסה זמין במאמר פריסת פונקציות.

  3. בקטע Trigger (טריגר), לוחצים על Add trigger (הוספת טריגר).

  4. בוחרים באפשרות Firestore trigger.

  5. בחלונית Eventarc trigger, משנים את פרטי הטריגר באופן הבא:

    1. בשדה Trigger name, מזינים שם לטריגר או משתמשים בשם ברירת המחדל.

    2. בוחרים סוג טריגר מהרשימה:

      • מקורות של Google כדי לציין טריגרים ל-Pub/Sub, ל-Cloud Storage, ל-Firestore ולספקי אירועים אחרים של Google.

      • צד שלישי כדי לשלב עם ספקים שאינם של Google שמציעים מקור Eventarc. מידע נוסף זמין במאמר אירועים של צד שלישי ב-Eventarc.

    3. בוחרים באפשרות Firestore מהרשימה Event provider כדי לבחור מוצר שמספק את סוג האירוע להפעלת הפונקציה. רשימת ספקי האירועים מופיעה במאמר ספקים ויעדים של אירועים.

    4. בוחרים באפשרות type=google.cloud.firestore.document.v1.written מהרשימה Event type. הגדרת הטריגר משתנה בהתאם לסוג האירוע הנתמך. מידע נוסף מופיע במאמר סוגי אירועים.

    5. משאירים את השדה סוג התוכן של נתוני האירועים כמו שהוא.

    6. בקטע Filters, בוחרים מסד נתונים, פעולה וערכי מאפיינים, או משתמשים בברירות המחדל. אם נתתם שם למסד הנתונים, הזינו את השם בשדה ערך מאפיין 1.

    7. אם השדה Region מופעל, בוחרים מיקום לטריגר של Eventarc. באופן כללי, המיקום של הטריגר של Eventarc צריך להתאים למיקום של המשאב ב-Google Cloud שאתם רוצים לעקוב אחרי האירועים שלו. ברוב המקרים, כדאי לפרוס את הפונקציה באותו אזור. למידע נוסף על מיקומי הטריגרים של Eventarc, קראו את המאמר הסבר על מיקומי Eventarc.

    8. בשדה Service account, בוחרים חשבון שירות. הטריגרים של Eventarc מקושרים לחשבונות שירות כדי להשתמש בהם כזהות כשמפעילים את הפונקציה. לחשבון השירות של הטריגר ב-Eventarc צריכה להיות הרשאה להפעיל את הפונקציה. כברירת מחדל, Cloud Run משתמש בחשבון השירות שמוגדר כברירת מחדל ב-Compute Engine.

    9. אם רוצים, מציינים את נתיב כתובת ה-URL של השירות שאליו תישלח הבקשה הנכנסת. זהו הנתיב היחסי בשירות היעד שאליו צריך לשלוח את האירועים של הטריגר. לדוגמה: /, /route, route ו-route/subroute.

    10. אם רוצים לאפשר ניסיונות חוזרים במקרה של כישלון, מסמנים את התיבה Enable retry on failure. אחרת, פעולת ברירת המחדל היא ניסיון מסירה אחד ללא ניסיונות חוזרים. מידע נוסף מופיע במאמר אירועי ניסיון חוזר.

  6. אחרי שממלאים את שדות החובה, לוחצים על שמירת הטריגר.

  7. לוחצים על יצירה.

  8. בכרטיסייה Source, עורכים את קוד המקור לפי הצורך ולוחצים על Save and redeploy.

gcloud

כשיוצרים פונקציה באמצעות ה-CLI של gcloud, קודם צריך לפרוס את הפונקציה ואז ליצור טריגר. כדי ליצור טריגר לפונקציה:

  1. מריצים את הפקודה הבאה בספרייה שמכילה את הקוד לדוגמה כדי לפרוס את הפונקציה:

    gcloud run deploy FUNCTION \
        --source . \
        --function FUNCTION_ENTRYPOINT \
        --base-image BASE_IMAGE_ID \
        --region REGION
    

    מחליפים את מה שכתוב בשדות הבאים:

    • ‫FUNCTION: שם הפונקציה שאתם פורסים. אפשר להשמיט את הפרמטר הזה לגמרי, אבל אם תשמיטו אותו תתבקשו להזין את השם.

    • ‫FUNCTION_ENTRYPOINT: נקודת הכניסה לפונקציה בקוד המקור. זה הקוד ש-Cloud Run מריץ כשהפונקציה פועלת. הערך של הדגל הזה חייב להיות שם של פונקציה או שם מלא של מחלקה שקיימים בקוד המקור.

    • ‫BASE_IMAGE_ID: סביבת קובץ האימג' הבסיסי של הפונקציה. לפרטים נוספים על קובצי אימג' בסיסיים ועל החבילות שכלולות בכל קובץ אימג', קראו את המאמר קובצי אימג' בסיסיים של סביבות זמן ריצה.

    • ‫REGION: האזור Google Cloudשבו רוצים לפרוס את הפונקציה. לדוגמה, europe-west1.

  2. מריצים את הפקודה הבאה כדי ליצור טריגר שמסנן אירועים ומנתב אותם:

    gcloud eventarc triggers create TRIGGER_NAME  \
        --location=LOCATION \
        --destination-run-service=FUNCTION  \
        --destination-run-region=DESTINATION_RUN_REGION \
        --event-filters=type=google.cloud.firestore.document.v1.written \
        --event-filters=database='(default)' \
        --event-data-content-type=application/protobuf \
        --event-filters-path-pattern=document='messages/{pushId}' \
        --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.
    

    מחליפים את מה שכתוב בשדות הבאים:

    • ‫TRIGGER_NAME: המזהה של הטריגר או מזהה מלא.
    • ‫LOCATION: המיקום של הטריגר של Eventarc. אפשר גם להגדיר את המאפיין eventarc/location, לדוגמה gcloud config set eventarc/location us-central1.

      כדי למנוע בעיות בביצועים ובמיקום אחסון הנתונים, המיקום צריך להתאים למיקום של שירות Google Cloud שיוצר אירועים. מידע נוסף זמין במאמר מיקומי Eventarc.

    • ‫FUNCTION: השם של פונקציית Cloud Run שנפרסה ומקבלת את האירועים של הטריגר.
    • ‫DESTINATION_RUN_REGION: (אופציונלי) המיקום ב-Cloud Run שבו נמצאת פונקציית היעד של Cloud Run. אם לא מציינים אזור, ההנחה היא שהפונקציה נמצאת באותו אזור של הטריגר.
    • ‫EVENT_FILTER_TYPE: המזהה של האירוע. אירוע נוצר כשקריאה ל-API של ה-method מצליחה. בפעולות שנמשכות זמן רב, האירוע נוצר רק בסוף הפעולה, ורק אם הפעולה בוצעה בהצלחה. רשימה של סוגי האירועים הנתמכים מופיעה במאמר סוגי האירועים של Google שנתמכים ב-Eventarc.
    • ‫SERVICE_ACCOUNT_NAME: השם של חשבון השירות שמנוהל על ידי המשתמש.
    • ‫PROJECT_ID: מזהה הפרויקט ב- Google Cloud .

    הערות:

    • אחרי שיוצרים טריגר, אי אפשר לשנות את סוג מסנן האירועים. כדי להשתמש בסוג אירוע אחר, צריך ליצור טריגר חדש.
    • --event-filters=type=google.cloud.firestore.document.v1.written מציין שהפונקציה מופעלת כשמסמך נוצר, מתעדכן או נמחק, בהתאם לסוג האירוע.
    • ‫--event-filters=database='(default)' מציין את מסד הנתונים של Firebase. שם ברירת המחדל של מסד הנתונים הוא (default).
    • --event-filters-path-pattern=document='users/{username}' מציין את תבנית הנתיב של המסמכים שצריך לעקוב אחריהם כדי לזהות שינויים רלוונטיים. תבנית הנתיב הזו מציינת שצריך לעקוב אחרי כל המסמכים באוסף users. מידע נוסף זמין במאמר הסבר על תבניות נתיב.
    • אם רוצים לציין ניסיון אחד לשליחת אירוע בלי ניסיונות חוזרים, משתמשים בדגל --max-retry-attempts. הערך התקין היחיד הוא 1. אם לא מציינים את הדגל, חלה התנהגות ברירת המחדל של ניסיון חוזר. מידע נוסף זמין במאמר ניסיון חוזר של אירועים.
    • יש עוד דגלים שאפשר להשתמש בהם. מידע נוסף זמין במאמר gcloud eventarc triggers create.

Terraform

במאמר יצירת טריגר באמצעות Terraform מוסבר איך יוצרים טריגר של Eventarc לפונקציית Cloud Run.

משתמשים בשדות האחרים כמו שהם:

  • --event-filters=type=google.cloud.firestore.document.v1.written מציין שהפונקציה מופעלת כשמסמך נוצר, מתעדכן או נמחק, בהתאם לgoogle.cloud.firestore.document.v1.written סוג האירוע.
  • ‫--event-filters=database='(default)' מציין את מסד הנתונים של Firestore. שם ברירת המחדל של מסד הנתונים הוא (default).
  • --event-filters-path-pattern=document='messages/{pushId}' מציין את תבנית הנתיב של המסמכים שצריך לעקוב אחריהם כדי לזהות שינויים רלוונטיים. תבנית הנתיב הזו מציינת שצריך לעקוב אחרי כל המסמכים באוסף messages. מידע נוסף זמין במאמר הסבר על תבניות נתיב.

בדיקת הפונקציה "המרת הטקסט לאותיות רישיות"

כדי לבדוק את הפונקציה Convert to Uppercase שפרסתם, מגדירים אוסף בשם messages במסד הנתונים של Firestore:

  1. במסוף Google Cloud , נכנסים לדף מסדי הנתונים של Firestore:

    כניסה ל-Firestore

  2. בוחרים את מזהה מסד הנתונים של Firestore.

  3. לוחצים על התחלת אוסף.

  4. מציינים את הערך messages כמזהה האוסף.

  5. כדי להתחיל להוסיף את המסמך הראשון לאוסף, בקטע הוספת המסמך הראשון מאשרים את מזהה המסמך שנוצר אוטומטית.

  6. כדי להפעיל את הפונקציה שנפרסה, מוסיפים מסמך שבו Field name הוא original ו-Field value הוא minka.

  7. כששומרים את המסמך, המילה באותיות קטנות בשדה הערך מומרת לאותיות רישיות.

    אם תערכו את הערך בשדה כך שיכיל אותיות קטנות, הפונקציה תופעל שוב ותמיר את כל האותיות הקטנות לאותיות רישיות.

מגבלות על פונקציות

  • לא מובטח שההזמנה תתבצע. שינויים מהירים יכולים להפעיל פונקציות בסדר לא צפוי.
  • אירועים נשלחים לפחות פעם אחת, אבל אירוע יחיד יכול לגרום להפעלה של כמה פונקציות. אל תסתמכו על מנגנונים של "בדיוק פעם אחת", וכתבו פונקציות אידמפוטנטיות.
  • טריגר משויך למסד נתונים אחד. אי אפשר ליצור טריגר שתואם לכמה מסדי נתונים.
  • מחיקת מסד נתונים לא מוחקת באופן אוטומטי את הטריגרים של אותו מסד נתונים. הטריגר מפסיק להעביר אירועים, אבל ממשיך להתקיים עד שמוחקים אותו.