th functions

Deploy Function ภาษา Python, Node.js (TypeScript) และ Go จากโฟลเดอร์ ดึงโค้ดที่แก้ใน Console ลงเครื่อง และเรียกใช้งาน

เกี่ยวกับ

Function คือ handler หนึ่งตัว คุณเขียนเฉพาะ handler ระบบเพิ่ม runtime ขนาดเล็กที่รับ HTTP ให้ Build และรันแบบ Container ที่หยุดเองเมื่อไม่มีงาน โค้ดชุดเดียวกันแสดงในตัวแก้ไขของ Console จึงแก้ได้ทั้งใน Console และในเครื่อง: th functions pull ดึงโค้ดที่ Console บันทึกไว้ และ th functions deploy ส่งโฟลเดอร์ของคุณขึ้นไป

Runtimeไฟล์หลักHandler
python314, python313 (แนะนำ), python312, python311main.py (+ requirements.txt)def main(request) คืนค่าเป็น str, dict (JSON) หรือ (body, status, headers)
nodejs24 (แนะนำ), nodejs22index.ts หรือ index.js (+ package.json)export async function handler(req: Request) คืนค่าเป็น Response, string หรือ object
go126 (แนะนำ), go125*.go + go.modfunc Handle(w http.ResponseWriter, r *http.Request)

Function หนึ่งมีไฟล์ข้อความได้ไม่เกิน 100 ไฟล์ ไฟล์ละไม่เกิน 512 KiB รวมไม่เกิน 2 MiB ไฟล์ใน .thignore (หรือ .dockerignore / .gitignore), .git, node_modules, .venv, __pycache__ และ dist จะไม่ถูกส่ง และไม่ใช้ Dockerfile ค่า Build คิดเหมือน Container ถ้าต้องการให้ทำงานตามเวลาหรือทุกข้อความใน Queue ให้เพิ่ม Trigger ด้วย th triggers

คู่มือการใช้งาน

Deploy Function จากโฟลเดอร์

Shell
mkdir hello && cd hello
printf 'def main(request):\n    return {"message": "Hello, " + request.args.get("name", "World")}\n' > main.py
th functions deploy hello --env GREETING=hi
th functions call hello --path "/?name=Thai"

Deploy ครั้งแรกจะสร้าง Function ให้ Runtime ดูจากไฟล์ (หรือกำหนดด้วย --runtime) th รอจน Build เสร็จแล้วแสดง URL ใช้ --async ถ้าไม่ต้องการรอ

ย้อนกลับไปใช้เวอร์ชันก่อนหน้า

Shell
th functions versions list hello
th functions versions activate hello 12
th functions pull hello --version 12 --dir hello-v12

ทุกครั้งที่ Deploy จะเป็นเวอร์ชันใหม่ เก็บไว้ล่าสุด 30 เวอร์ชัน การใช้เวอร์ชันเก่าจะใช้การตั้งค่าและตัวแปรปัจจุบัน ถ้า image ของเวอร์ชันนั้นถูกล้างไปแล้ว ระบบจะ Build โค้ดชุดนั้นใหม่เป็นเวอร์ชันล่าสุด ใส่ --runtime ตอน Deploy เพื่อเปลี่ยนเวอร์ชันของภาษา เช่น --runtime python314

ให้ Function ทำงานเฉพาะเมื่อ Trigger เรียก

Shell
th functions deploy nightly-report --ingress triggers
th triggers create nightly --service nightly-report --schedule "0 3 * * *"

เมื่อใช้ --ingress triggers Function จะไม่มี Endpoint เลย ไม่มี URL สาธารณะ ไม่มี Custom domain และไม่มีที่อยู่ภายใน VPC ของ Project มีเพียง Trigger แบบ Cron และ Queue ของมันที่เรียกได้ และสั่งรันทันทีได้ด้วย th triggers run ใช้ --ingress all เพื่อเปิดกลับ

แก้ใน Console แล้วทำต่อในเครื่อง

Shell
th functions pull hello --dir hello
# แก้ไฟล์
th functions deploy hello --source hello

pull จะไม่เขียนทับไฟล์ในเครื่องที่ต่างจากใน Console เว้นแต่ใส่ --force

คำสั่ง

คำสั่งคำอธิบาย
th functions deployUploads the files of DIR (default .) as the function's code, builds them and serves the result.
th functions listLists the functions of the active Project.
th functions describeShows a function's runtime, deploy state, settings and files.
th functions pullWrites the function's saved code (what the Console editor shows), or the code of one version, into DIR (default ./NAME).
th functions callSends one request to the function and prints the answer.
th functions logsPrints the latest log lines of a function (what the handler printed).
th functions versions listLists a function's versions, newest first.
th functions versions activateServes an earlier version again; today's settings and variables stay.
th functions runtimesLists the runtimes a function can use and the handler each expects.
th functions deleteDeletes a function with its code, triggers and revisions.

th functions deploy

Uploads the files of DIR (default .) as the function's code, builds them and serves the result.

ไวยากรณ์

Shell
th functions deploy NAME [--source DIR] [flags]

A new function is created first; its runtime comes from --runtime or from the files (go.mod: Go, package.json / index.ts: Node.js, main.py / requirements.txt: Python).

Files listed in .thignore (or .dockerignore / .gitignore), .git, node_modules, .venv, __pycache__ and dist are left out. A Dockerfile is ignored: functions always build with the platform's runtime.

--runtime takes a runtime (python313, nodejs24, go126) or a language (python, nodejs, go: its recommended version). An existing function may move to another version of its language.

Every deploy is kept as a version (th functions versions list); --message describes it. --ingress sets who reaches the function: all (the internet, the default), internal (other services in its project) or triggers (no endpoint at all; only its Cron and Queue triggers run it). --env, --set-env-vars and --secret-env replace every variable of the function; without them the current variables stay. Settings given for an existing function (--cpu, --memory, --min-instances, ...) are applied before its code is built.

ตัวอย่าง

Shell
th functions deploy hello
Shell
th functions deploy hello --runtime python314 --message "Python 3.14"
Shell
th functions deploy orders --source ./orders --memory 512Mi --max-instances 10
Shell
th functions deploy nightly-report --ingress triggers

อาร์กิวเมนต์ตามตำแหน่ง

ชื่อคำอธิบาย
NAMEจำเป็นชื่อ Function: ตัวพิมพ์เล็ก ตัวเลข และ - ขึ้นต้นด้วยตัวอักษร ใช้เป็น URL slug ด้วย

พารามิเตอร์

ชื่อชนิดค่าเริ่มต้นคำอธิบาย
--sourceข้อความ.Directory with the function's files
--runtimeข้อความRuntime (e.g. python313, nodejs24, go126) or language (python, nodejs, go)
--entry-pointข้อความthe runtime's, e.g. main, handler, HandleHandler name
--cpuข้อความvCPU, e.g. 0.25, 0.5, 1 (a size of the Container menu)
--memoryข้อความMemory, e.g. 256Mi, 512Mi, 1Gi
--compute-classข้อความCompute class of a new function
--envรายการEnvironment variable KEY=VALUE (repeatable)
--set-env-varsรายการEnvironment variables KEY=VALUE,KEY2=VALUE2
--secret-envรายการSecret environment variable KEY=VALUE (repeatable)
--min-instancesตัวเลขMinimum instances (0 stops it when idle)
--max-instancesตัวเลขMaximum instances
--timeoutข้อความRequest timeout, e.g. 60s or 5m
--concurrencyตัวเลขRequests at once per instance (1-1000)
--cpu-always-allocatedแฟล็กKeep CPU between requests
--allow-unauthenticatedแฟล็กfor a new function; --no-allow-unauthenticated needs an invoker tokenAllow public calls
--ingressข้อความWho reaches the function: all, internal or triggers (no endpoint, triggers only)
--messageข้อความDescribe this version
--asyncแฟล็กReturn once the build is queued

พารามิเตอร์ส่วนกลาง

คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet

กลับขึ้นด้านบน ↑

th functions list

Lists the functions of the active Project.

ไวยากรณ์

Shell
th functions list

พารามิเตอร์ส่วนกลาง

คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet

กลับขึ้นด้านบน ↑

th functions describe

Shows a function's runtime, deploy state, settings and files.

ไวยากรณ์

Shell
th functions describe NAME

อาร์กิวเมนต์ตามตำแหน่ง

ชื่อคำอธิบาย
NAMEจำเป็นชื่อ Function: ตัวพิมพ์เล็ก ตัวเลข และ - ขึ้นต้นด้วยตัวอักษร ใช้เป็น URL slug ด้วย

พารามิเตอร์ส่วนกลาง

คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet

กลับขึ้นด้านบน ↑

th functions pull

Writes the function's saved code (what the Console editor shows), or the code of one version, into DIR (default ./NAME).

ไวยากรณ์

Shell
th functions pull NAME [--dir DIR] [--version N] [--force]

An existing file with other content is kept unless --force.

อาร์กิวเมนต์ตามตำแหน่ง

ชื่อคำอธิบาย
NAMEจำเป็นชื่อ Function: ตัวพิมพ์เล็ก ตัวเลข และ - ขึ้นต้นด้วยตัวอักษร ใช้เป็น URL slug ด้วย

พารามิเตอร์

ชื่อชนิดค่าเริ่มต้นคำอธิบาย
--dirข้อความ./NAMEDirectory to write into
--versionข้อความthe saved codeA version from th functions versions list
--forceแฟล็กOverwrite files that differ

พารามิเตอร์ส่วนกลาง

คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet

กลับขึ้นด้านบน ↑

th functions call

Sends one request to the function and prints the answer.

ไวยากรณ์

Shell
th functions call NAME [--data BODY|@FILE] [--method POST] [--path /x] [--token TOKEN]

With --data the method defaults to POST, otherwise GET. --token is the invoker token of a function that does not allow unauthenticated calls.

อาร์กิวเมนต์ตามตำแหน่ง

ชื่อคำอธิบาย
NAMEจำเป็นชื่อ Function: ตัวพิมพ์เล็ก ตัวเลข และ - ขึ้นต้นด้วยตัวอักษร ใช้เป็น URL slug ด้วย

พารามิเตอร์

ชื่อชนิดค่าเริ่มต้นคำอธิบาย
--dataข้อความRequest body, or @FILE to read it from a file
--methodข้อความHTTP method
--pathข้อความ/Path to call
--tokenข้อความInvoker token

พารามิเตอร์ส่วนกลาง

คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet

กลับขึ้นด้านบน ↑

th functions logs

Prints the latest log lines of a function (what the handler printed).

ไวยากรณ์

Shell
th functions logs NAME [--lines N]

อาร์กิวเมนต์ตามตำแหน่ง

ชื่อคำอธิบาย
NAMEจำเป็นชื่อ Function: ตัวพิมพ์เล็ก ตัวเลข และ - ขึ้นต้นด้วยตัวอักษร ใช้เป็น URL slug ด้วย

พารามิเตอร์

ชื่อชนิดค่าเริ่มต้นคำอธิบาย
--linesตัวเลข100Number of lines (default 100)

พารามิเตอร์ส่วนกลาง

คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet

กลับขึ้นด้านบน ↑

th functions versions list

Lists a function's versions, newest first.

ไวยากรณ์

Shell
th functions versions list NAME

Every deploy is a version; the serving one is marked. The latest 30 are kept.

อาร์กิวเมนต์ตามตำแหน่ง

ชื่อคำอธิบาย
NAMEจำเป็นชื่อ Function: ตัวพิมพ์เล็ก ตัวเลข และ - ขึ้นต้นด้วยตัวอักษร ใช้เป็น URL slug ด้วย

พารามิเตอร์ส่วนกลาง

คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet

กลับขึ้นด้านบน ↑

th functions versions activate

Serves an earlier version again; today's settings and variables stay.

ไวยากรณ์

Shell
th functions versions activate NAME VERSION

When the registry no longer holds its image, its code is built again as a new version and served once the build succeeds.

ตัวอย่าง

Shell
th functions versions activate hello 12
Shell
th functions versions activate hello v12

อาร์กิวเมนต์ตามตำแหน่ง

ชื่อคำอธิบาย
NAMEจำเป็นชื่อ Function: ตัวพิมพ์เล็ก ตัวเลข และ - ขึ้นต้นด้วยตัวอักษร ใช้เป็น URL slug ด้วย
VERSIONจำเป็น

พารามิเตอร์ส่วนกลาง

คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet

กลับขึ้นด้านบน ↑

th functions runtimes

Lists the runtimes a function can use and the handler each expects.

ไวยากรณ์

Shell
th functions runtimes

พารามิเตอร์ส่วนกลาง

คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet

กลับขึ้นด้านบน ↑

th functions delete

Deletes a function with its code, triggers and revisions.

ไวยากรณ์

Shell
th functions delete NAME [--quiet]

--quiet skips the prompt.

อาร์กิวเมนต์ตามตำแหน่ง

ชื่อคำอธิบาย
NAMEจำเป็นชื่อ Function: ตัวพิมพ์เล็ก ตัวเลข และ - ขึ้นต้นด้วยตัวอักษร ใช้เป็น URL slug ด้วย

พารามิเตอร์ส่วนกลาง

คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet

กลับขึ้นด้านบน ↑