จัดระเบียบฟังก์ชันหลายรายการ


เมื่อคุณผสานรวม Cloud Functions เข้ากับโปรเจ็กต์ โค้ดอาจขยายเป็น มีฟังก์ชันอิสระมากมาย คุณอาจมีฟังก์ชันมากเกินไป พอดีกับไฟล์เดียว หรือหลายๆ ทีมอาจปรับใช้กลุ่มที่แตกต่างกัน ทำให้เกิดความเสี่ยงที่ทีมหนึ่งจะเขียนทับหรือเผลอลบ ของอีกทีมหนึ่งได้ Cloud Functions มีวิธีการต่างๆ ในการจัดระเบียบ เพื่อไปยังส่วนต่างๆ และดูแลฟังก์ชันได้ง่ายขึ้น

จัดระเบียบฟังก์ชันในโค้ดเบส

คุณสามารถใช้พร็อพเพอร์ตี้ codebase ของออบเจ็กต์การกำหนดค่าฟังก์ชันใน firebase.json เพื่อจัดการคอลเล็กชันฟังก์ชันขนาดใหญ่จากหลายฟังก์ชัน ที่เก็บหรือแพ็กเกจย่อยภายในการตั้งค่าการ Monorepo ของที่เก็บเดียว

# firebase.json
"functions": {
  "codebase": "my-codebase"
  # NOTE: Codebase must be less than 63 characters and can contain only
  # lowercase letters, numeric characters, underscores, and dashes.
}

ระบบรองรับพร็อพเพอร์ตี้ codebase ใน Firebase CLI v10.7.1 ขึ้นไป

การจัดการที่เก็บหลายรายการ

พร็อพเพอร์ตี้ codebase ช่วยลดความซับซ้อนในการจัดการหลายบัญชี ที่เก็บได้ ลองมาดูกรณีที่คุณมีที่เก็บที่ต่างกัน 2 รายการกัน ที่ทำให้ฟังก์ชันใช้งานได้ในโปรเจ็กต์ Firebase เดียวกัน:

$  tree .
├── repoA
│   ├── firebase.json
│   └── functions
│       ├── index.js
│       └── package.json
└── repoB
    ├── firebase.json
    └── functions
        ├── index.js
        └── package.json

หากไม่มีคำอธิบายประกอบของโค้ดเบสของ Firebase CLI จะแจ้งให้คุณดำเนินการ ลบฟังก์ชันที่กำหนดไว้ในที่เก็บอื่นขณะทำให้ใช้งานได้:

$ (cd repoA && firebase deploy --only functions)
...
i  functions: preparing functions directory for uploading...
✔  functions: functions folder uploaded successfully
The following functions are found in your project but do not exist in your local source code:
        fn1FromRepoB
        fn2FromRepoB
        ...
? Would you like to proceed with deletion? Selecting no will continue the rest of the deployments. (y/N)

คุณสามารถหลีกเลี่ยงปัญหานี้ได้โดยการเพิ่มคำอธิบายประกอบ Codebase ที่ไม่ซ้ำกันใน ส่วนการกำหนดค่าฟังก์ชันของ firebase.json ในที่เก็บโปรเจ็กต์แต่ละรายการ:

# repoA/firebase.json
"functions": {
  "codebase": "repo-a"
}

# repoB/firebase.json
"functions": {
  "codebase": "repo-b"
}

เมื่อใช้คำอธิบายประกอบของโค้ดเบสของ Firebase CLI จะไม่แจ้งให้คุณลบอีกต่อไป ฟังก์ชันที่กำหนดไว้นอกที่เก็บทันทีของคุณ:

$ (cd repoA && firebase deploy --only functions)
...
i  functions: preparing functions directory for uploading...
✔  functions: functions folder uploaded successfully
#  Gleefully ignores functions from repoB
i  functions: creating Node.js 16 function fnFromRepoA (us-central1)...
✔  Deploy Complete!

การจัดการแพ็กเกจต้นทางหลายรายการ (monorepo)

พร็อพเพอร์ตี้ codebase ช่วยลดความซับซ้อนในการจัดการแหล่งที่มาหลายแห่ง แพ็กเกจในที่เก็บเดียวได้ มาดูกรณีที่คุณมี Firebase กัน ไดเรกทอรีโครงการที่มีการกำหนดฟังก์ชันที่กระจายอยู่ในแพ็กเกจย่อยหลายรายการ:

$  tree .
├── firebase.json
├── teamA
│   ├── index.js
│   └── package.json
└── teamB
    ├── index.js
    └── package.json

การตั้งค่านี้เหมาะกับกรณีการใช้งานต่อไปนี้

  • คุณมีการตั้งค่าผูกขาดและมีทีมต่างๆ จัดการคำจำกัดความฟังก์ชันของตนเองในแพ็กเกจที่แยกต่างหาก
  • คุณมีฟังก์ชันที่มีทรัพยากร Dependency ภายนอกจำนวนมากและการเริ่มต้นใช้งานที่ใช้เวลานาน และต้องการแยกฟังก์ชันดังกล่าวออกจากฟังก์ชันอื่นๆ ที่ไวต่อเวลาในการตอบสนอง

หากต้องการรองรับการตั้งค่า Monrepo ในลักษณะนี้ ให้กำหนดค่าฟังก์ชันหลายรายการ ใน firebase.json:

"functions": [
  {
    "source": "teamA",
    "codebase": "team-a"
  },
  {
    "source": "teamB",
    "codebase": "team-b"
  },
]

ด้วยการกำหนดค่านี้ Firebase CLI จะทำให้ฟังก์ชันจากแพ็กเกจทั้งหมดใช้งานได้ ในคำสั่ง Deploy เดียว:

$ firebase deploy --only functions
i  deploying functions
i  functions: preparing codebase team-a for deployment
i  functions: preparing codebase team-b for deployment
i  functions: creating Node.js 16 function team-a:helloATeam(us-central1)...
i  functions: creating Node.js 16 function team-b:helloBTeam(us-central1)...
...

นอกจากนี้คุณยังทำให้ Codebase ที่ต้องการใช้งานได้ด้วย ดังนี้

$ firebase deploy --only functions:team-b
i  deploying functions
i  functions: preparing codebase team-b for deployment
i  functions: updating Node.js 16 function team-b:helloBTeam(us-central1)...
...

เขียนฟังก์ชันในไฟล์หลายไฟล์

เมื่อเริ่มต้นใช้งาน Cloud Functions คุณอาจป้อน 2-3 รายการแรก ไว้ในไฟล์เดียว ได้แก่

index.js

const functions = require('firebase-functions');
exports.foo = functions.https.onRequest((request, response) => {
  // ...
});
exports.bar = functions.https.onRequest((request, response) => {
  // ...
});

Main.py

from firebase_functions import https_fn

@https_fn.on_request()
def foo(req: https_fn.Request) -> https_fn.Response:
    return https_fn.Response("Hello foo!")

@https_fn.on_request()
def bar(req: https_fn.Request) -> https_fn.Response:
    return https_fn.Response("Hello bar!")

ซึ่งอาจทำให้จัดการได้ยากหากมีฟังก์ชันมากกว่า 2-3 อย่าง แต่คุณ สามารถใส่ตรรกะของคุณทั้งหมดสำหรับแต่ละฟังก์ชันในไฟล์ของตัวเองและใช้ ไฟล์ต้นฉบับเป็นรายการการส่งออก

Node.js

foo.js

const functions = require('firebase-functions');
exports.foo = functions.https.onRequest((request, response) => {
  // ...
});

bar.js

const functions = require('firebase-functions');
exports.bar = functions.https.onRequest((request, response) => {
  // ...
});

index.js

const foo = require('./foo');
const bar = require('./bar');
exports.foo = foo.foo;
exports.bar = bar.bar;

Python

foo.py

from firebase_functions import https_fn

@https_fn.on_request()
def foo(req: https_fn.Request) -> https_fn.Response:
    return https_fn.Response("Hello foo!")

bar.py

from firebase_functions import https_fn

@https_fn.on_request()
def bar(req: https_fn.Request) -> https_fn.Response:
    return https_fn.Response("Hello foo!")

main.py

from fn_impl.foo import *
from fn_impl.bar import *

การตั้งค่านี้จะมีโครงสร้างไดเรกทอรีโปรเจ็กต์ดังต่อไปนี้

my-project
├── firebase.json
└── functions
    ├── fn_impl
    │   ├── __init__.py
    │   ├── foo.py
    │   └── bar.py
    ├── main.py
    └── requirements.txt

fn_impl: สามารถใช้ชื่อใดก็ได้

__init__.py: ต้องระบุ แต่อาจว่างเปล่าได้

ฟังก์ชันกลุ่ม

ในหลายโปรเจ็กต์ สามารถแยกฟังก์ชันออกเป็นกลุ่มเชิงตรรกะที่ควร สามารถนำไปปรับใช้งานและบำรุงรักษาไปพร้อมกันได้ ตัวอย่างเช่น คุณอาจมีกลุ่ม ฟังก์ชันที่ใช้สำหรับเมตริกการรายงาน ได้แก่

metrics.js


const functions = require('firebase-functions');
exports.usageStats = functions.https.onRequest((request, response) => {
  // ...
});
exports.nightlyReport = functions.https.onRequest((request, response) => {
  // ...
});

คุณสามารถนำฟังก์ชันเหล่านี้ไปไว้ในกลุ่มได้เมื่อส่งออกฟังก์ชันดังกล่าวใน index.js ไฟล์:

index.js


// Export both functions from metrics.js in the "metrics" group:
//  - metrics-usageStats
//  - metrics-nightlyReport
exports.metrics = require('./metrics');

เมื่อทำให้ใช้งานได้ ฟังก์ชันจะมีชื่อของกลุ่มนำหน้า ดังนั้น ในตัวอย่างนี้ ฟังก์ชันจะมีชื่อว่า metrics-usageStats และ metrics-nightlyReport

เมื่อทำให้ฟังก์ชันใช้งานได้ คุณจะจำกัดการดำเนินการไว้ที่กลุ่มเดียวได้ดังนี้


firebase deploy --only functions:metrics

ขั้นตอนถัดไป

ดูข้อมูลเพิ่มเติมเกี่ยวกับฟังก์ชันระบบคลาวด์ได้ที่