# การปรับขนาด รุ่นการตั้งค่า และบันทึกของเอนด์พอยต์จากโค้ด

หน้านี้ทำจากสคริปต์ในสิ่งที่การ์ด **การปรับขนาดและการใช้จ่าย** การ์ด **รุ่นการตั้งค่า** และบันทึกเวิร์กเกอร์ของเอนด์พอยต์ทำในคอนโซล ตัวอย่างใช้ id เอนด์พอยต์ `ep_ID` การอ่านใช้คีย์ `read` ได้ ส่วนการเปลี่ยนแปลงต้องใช้คีย์ที่มี scope `full` คีย์ที่ผูกกับเอนด์พอยต์เดียวเรียกได้เฉพาะเอนด์พอยต์นั้น จึงทำสิ่งเหล่านี้ไม่ได้ เอนด์พอยต์แบบ Serverless อาจยังไม่ได้เปิดสำหรับบัญชีของคุณ ความหมายของแต่ละการตั้งค่าอธิบายไว้ใน [เอนด์พอยต์แบบ Serverless](/docs/endpoints)

## ใช้พรีเซ็ตการปรับขนาด

แสดงพรีเซ็ตทั้งสามพร้อมค่าใช้จ่ายต่อชั่วโมงตามราคาวันนี้ ทั้งเมื่อไม่มีทราฟฟิก (`idleHourlyMicros`) และเมื่อโหลดเต็ม (`maxHourlyMicros`) โดย `current` ระบุพรีเซ็ตที่เอนด์พอยต์ตรงอยู่แล้ว จากนั้นเลือกใช้หนึ่งพรีเซ็ต พรีเซ็ตจะถูกบันทึกเหมือนการเปลี่ยนแปลงอื่น จึงกลายเป็นรุ่นการตั้งค่าด้วย และไม่เคยเริ่มการอัปเดตแบบทยอย

```bash
curl "https://apidly.com/api/v1/endpoints/ep_ID/presets" \
  -H "Authorization: Bearer $APIDLY_API_KEY"

curl -X POST "https://apidly.com/api/v1/endpoints/ep_ID/presets" \
  -H "Authorization: Bearer $APIDLY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"preset":"scale_to_zero"}'

apidly endpoints presets ep_ID
apidly endpoints preset ep_ID scale_to_zero
```

```js
import { Apidly } from "@apidly/sdk";

const apidly = new Apidly({ apiKey: process.env.APIDLY_API_KEY });
const { presets } = await apidly.endpoints.presets("ep_ID");
for (const p of presets) {
  console.log(p.name, p.idleHourlyMicros, p.maxHourlyMicros, p.current);
}
await apidly.endpoints.applyPreset("ep_ID", "scale_to_zero");
```

```python
import os
from apidly import Client

client = Client(api_key=os.environ["APIDLY_API_KEY"])
for p in client.endpoints.presets("ep_ID")["presets"]:
    print(p["name"], p["idleHourlyMicros"], p["maxHourlyMicros"], p["current"])
client.endpoints.apply_preset("ep_ID", "scale_to_zero")
```

## ตั้งวงเงินใช้จ่ายรายเดือน

วงเงินเป็นดอลลาร์สหรัฐต่อเดือนปฏิทิน (UTC) เมื่อถึงวงเงิน เอนด์พอยต์จะหยุดชั่วคราวเอง ปล่อยเวิร์กเกอร์ และส่งอีเมลถึงเจ้าของ เมื่อใช้ `resumeAtNewMonth` จะกลับมาทำงานเองเมื่อเริ่มเดือนถัดไป ส่ง `null` (ใน CLI ใช้ `none`) เพื่อยกเลิกวงเงิน

```bash
curl -X PATCH "https://apidly.com/api/v1/endpoints/ep_ID" \
  -H "Authorization: Bearer $APIDLY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"monthlySpendCapUsd":50,"resumeAtNewMonth":true}'

apidly endpoints update ep_ID --monthly-cap 50 --resume-at-new-month on
apidly endpoints update ep_ID --monthly-cap none
```

```js
import { Apidly } from "@apidly/sdk";

const apidly = new Apidly({ apiKey: process.env.APIDLY_API_KEY });
await apidly.endpoints.update("ep_ID", {
  monthlySpendCapUsd: 50,
  resumeAtNewMonth: true,
});
const { endpoint } = await apidly.endpoints.get("ep_ID");
console.log(endpoint.spendCap);
```

```python
import os
from apidly import Client

client = Client(api_key=os.environ["APIDLY_API_KEY"])
client.endpoints.update("ep_ID", {"monthlySpendCapUsd": 50, "resumeAtNewMonth": True})
print(client.endpoints.get("ep_ID")["endpoint"]["spendCap"])
```

## ดูและกู้คืนรุ่นการตั้งค่า

การเปลี่ยนแปลงที่บันทึกแต่ละครั้งเป็นรุ่นการตั้งค่าที่มีหมายเลข เรียงจากใหม่สุด โดยแสดงตัวแปรสภาพแวดล้อมเพียงชื่อ การกู้คืนจะนำการตั้งค่าของรุ่นนั้นกลับมาเป็นรุ่นใหม่ หากเปลี่ยนการตั้งค่าเวิร์กเกอร์ การอัปเดตแบบทยอยจะเริ่มขึ้น CLI จะถามก่อนกู้คืน เว้นแต่คุณใส่ `--yes`

```bash
curl "https://apidly.com/api/v1/endpoints/ep_ID/releases?limit=20" \
  -H "Authorization: Bearer $APIDLY_API_KEY"

curl -X POST "https://apidly.com/api/v1/endpoints/ep_ID/releases/VERSION/restore" \
  -H "Authorization: Bearer $APIDLY_API_KEY"

apidly endpoints releases ep_ID --limit 20
apidly endpoints restore ep_ID 3
```

```js
import { Apidly } from "@apidly/sdk";

const apidly = new Apidly({ apiKey: process.env.APIDLY_API_KEY });
const { releases } = await apidly.endpoints.releases("ep_ID", { limit: 20 });
console.log(releases.map((r) => r.version));
const { changed } = await apidly.endpoints.restoreRelease("ep_ID", 3);
console.log(changed);
```

```python
import os
from apidly import Client

client = Client(api_key=os.environ["APIDLY_API_KEY"])
for r in client.endpoints.releases("ep_ID", limit=20)["releases"]:
    print(r["version"], r["changes"])
print(client.endpoints.restore_release("ep_ID", 3)["changed"])
```

## อ่านบันทึกเวิร์กเกอร์

บันทึกของเอนด์พอยต์คือบรรทัดล่าสุดของเวิร์กเกอร์แต่ละตัว สูงสุด 12 ตัวต่อการเรียก หรือของเวิร์กเกอร์ตัวเดียวเมื่อระบุ `worker` บันทึกของงานคือบันทึกของเวิร์กเกอร์ที่รันงานนั้น `available: false` พร้อม `reason` เป็น `not_dispatched` หรือ `worker_gone` หมายความว่าไม่มีบันทึกให้อ่าน `tail` กำหนดจำนวนบรรทัด สูงสุด 500 ค่าลับจะถูกซ่อนในทุกบันทึก

```bash
curl "https://apidly.com/api/v1/endpoints/ep_ID/logs?tail=100" \
  -H "Authorization: Bearer $APIDLY_API_KEY"

curl "https://apidly.com/api/v1/endpoints/ep_ID/logs?worker=ins_ID&tail=200" \
  -H "Authorization: Bearer $APIDLY_API_KEY"

curl "https://apidly.com/api/v1/endpoints/ep_ID/jobs/job_ID/logs?tail=200" \
  -H "Authorization: Bearer $APIDLY_API_KEY"

apidly endpoints logs ep_ID --tail 100
apidly endpoints logs ep_ID --worker ins_ID
apidly endpoints job-logs ep_ID job_ID
```

```js
import { Apidly } from "@apidly/sdk";

const apidly = new Apidly({ apiKey: process.env.APIDLY_API_KEY });
const { workers } = await apidly.endpoints.logs("ep_ID", { tail: 100 });
for (const w of workers) {
  console.log(`== ${w.serverId} (${w.status})\n${w.logs}`);
}
const { job } = await apidly.endpoints.jobLogs("ep_ID", "job_ID", { tail: 200 });
console.log(job.available ? job.logs : job.reason);
```

```python
import os
from apidly import Client

client = Client(api_key=os.environ["APIDLY_API_KEY"])
for w in client.endpoints.logs("ep_ID", tail=100, worker="ins_ID")["workers"]:
    print(w["serverId"], w["logs"])
job = client.endpoints.job_logs("ep_ID", "job_ID", tail=200)["job"]
print(job["logs"] if job["available"] else job["reason"])
```
