# Endpunkt-Skalierung, Versionen und Logs per Code

Diese Seite erledigt per Skript, was in der Konsole die Karte **Skalierung und Ausgaben**, die Karte **Versionen** und die Worker-Logs eines Endpunkts tun. Die Beispiele verwenden die Endpunkt-ID `ep_ID`. Lesen geht mit einem `read`-Schlüssel; Änderungen brauchen einen Schlüssel mit dem Scope `full`. Ein Schlüssel, der an einen Endpunkt gebunden ist, kann nur diesen Endpunkt aufrufen und hier also nichts davon. Serverlose Endpunkte sind für dein Konto möglicherweise noch nicht freigeschaltet. Was jede Einstellung bedeutet, steht unter [Serverlose Endpunkte](/docs/endpoints).

## Skalierungsvorlage anwenden

Liste die drei Vorlagen mit ihren Kosten pro Stunde zum heutigen Preis auf, ohne Verkehr (`idleHourlyMicros`) und unter Volllast (`maxHourlyMicros`); `current` markiert die, der der Endpunkt schon entspricht. Wende dann eine an. Eine Vorlage wird wie jede andere Änderung gespeichert, wird also auch eine Version, und startet nie ein Rolling Update.

```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")
```

## Monatliches Ausgabenlimit setzen

Das Limit gilt in US-Dollar pro Kalendermonat (UTC). Am Limit pausiert sich der Endpunkt, gibt seine Worker frei und schickt dem Inhaber eine E-Mail. Mit `resumeAtNewMonth` läuft er zu Beginn des nächsten Monats von selbst weiter. Sende `null` (in der CLI `none`), um das Limit zu entfernen.

```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"])
```

## Versionen auflisten und wiederherstellen

Jede gespeicherte Änderung ist eine nummerierte Version, die neueste zuerst, mit Umgebungsvariablen nur als Namen. Wiederherstellen setzt die Einstellungen einer Version als neue Version zurück; ändert das die Worker-Einrichtung, startet ein Rolling Update. Die CLI fragt vor dem Wiederherstellen, außer du übergibst `--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"])
```

## Worker-Logs lesen

Die Logs des Endpunkts sind die neuesten Zeilen jedes Workers, bis zu 12 Worker pro Aufruf, oder eines Workers mit `worker`. Das Log eines Jobs ist das Log des Workers, der ihn ausgeführt hat; `available: false` mit `reason` `not_dispatched` oder `worker_gone` heißt, dass es keines zu lesen gibt. `tail` legt die Zahl der Zeilen fest, höchstens 500. Geheime Werte sind in jedem Log ausgeblendet.

```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"])
```
