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


เมื่อผสานรวม 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 จะทำให้ฟังก์ชันใช้งานได้จากแพ็กเกจทั้งหมดในคำสั่งปรับใช้เดียว ดังนี้

$ 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 อย่างแรกในไฟล์เดียวได้ดังนี้

ดัชนี.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

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

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