# 코드로 엔드포인트 스케일링, 릴리스, 로그 다루기

이 페이지는 콘솔의 **스케일링과 지출** 카드, **릴리스** 카드, 엔드포인트 워커 로그가 하는 일을 스크립트로 합니다. 예시는 엔드포인트 ID `ep_ID`를 사용합니다. 읽기는 `read` 키로 되지만 변경하려면 `full` 범위의 키가 필요합니다. 엔드포인트 하나에 묶인 키는 그 엔드포인트를 호출하는 것만 할 수 있으므로 여기 있는 일은 할 수 없습니다. 서버리스 엔드포인트가 아직 계정에서 켜져 있지 않을 수 있습니다. 각 설정의 의미는 [서버리스 엔드포인트](/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"])
```
