krz/moonrise

Night-sky companion for iPhone, Apple Watch and Vision Pro: what's up tonight, the Moon, planets, launches and missions, and a Library of the solar system. SwiftUI app with a Cloudflare Workers backend. astronomy cloudflare-workers ios night-sky space swiftui visionos watchos

Commit bd24190a6c

bd24190a6cfcad48470d823ce4d89e3059a0d080

parent: c21bb2b23e

Unsigned

cmc <hello@cleberg.net> · 2026-10-21 17:14 UTC

Push slices sized for Workers Paid (!83)

PUSH_SLICE 20 -> 200 now that the account is on Workers Paid (10,000 subrequests per invocation). PUSH_CONCURRENCY stays 6, the open-connection limit on both plans. Worker tests use PUSH_SLICE instead of a fixed 20. Server tests: 55 pass. PLAN.org: Workers Paid item removed from the before-publishing list.

Layout: unified · split

PLAN.org −3
@@ -470,9 +470,6 @@ Sky, System, Sun, Launches, Library core (above).
470470 device and add one-object alignment with a gyroscope heading.
471471
472472* before publishing
473- Workers Paid ($5/month) before real users register, then raise
474 =PUSH_SLICE= in =server/src/index.ts= (sized for Free's 50 subrequests per
475 invocation). The */5 cron already goes over Free's 10 ms CPU now and then.
476473- Decide on server-side caching: a custom domain with the Cache API, D1 read
477474 replication or Smart Placement. =workers.dev= does not cache Worker
478475 responses, and every D1 round trip goes to San Jose (about 85 ms from the
server/src/index.ts +3 −3
@@ -37,10 +37,10 @@ const RETENTION_MS = 14 * 86400_000;
3737/** Devices are dropped after this long without registering; the app registers whenever iOS hands it a token. */
3838const DEVICE_RETENTION_MS = 180 * 86400_000;
3939/**
40 * Alert pushes per run; the rest go on later runs. Sized for the Free plan's 50 subrequests per invocation, shared
41 * with the rest of the run; raise it on Paid. Workers keep at most 6 connections open at once.
40 * Alert pushes per run; the rest go on later runs. The Paid plan allows 10,000 subrequests per invocation; at 6
41 * connections open at once, 200 sends take a few seconds of the five-minute run.
4242 */
43const PUSH_SLICE = 20;
43export const PUSH_SLICE = 200;
4444const PUSH_CONCURRENCY = 6;
4545
4646const json = (body: unknown, status = 200, maxAge?: number) =>
server/test/worker.test.ts +14 −14
@@ -15,7 +15,7 @@ registerHooks({
1515 }
1616 },
1717});
18const { default: worker } = await import("../src/index.ts");
18const { default: worker, PUSH_SLICE } = await import("../src/index.ts");
1919
2020const FIVE = "*/5 * * * *";
2121const TWO = "*/2 * * * *";
@@ -172,21 +172,21 @@ test("a failed CelesTrak poll is tried once more 33 minutes later, from the 5-mi
172172
173173test("aurora alerts go out in bounded slices and are recorded per device once answered", async () => {
174174 const { db, env, calls } = await setup();
175 for (let i = 0; i < 30; i++) device(db, i);
176 device(db, 30, { offset: 720 }); // 10:05 local: not night
175 for (let i = 0; i < PUSH_SLICE + 10; i++) device(db, i);
176 device(db, PUSH_SLICE + 10, { offset: 720 }); // 10:05 local: not night
177177 const push = apns({ [token(3)]: 503, [token(4)]: 410, [token(5)]: 400 });
178178 const hosts = network({ ...swpc(6), ...push.handlers });
179179
180180 await run(env, FIVE, "2026-10-20T22:05:00Z");
181 assert.equal(push.state.sent.length, 20);
181 assert.equal(push.state.sent.length, PUSH_SLICE);
182182 assert.ok(push.state.maxInFlight <= 6, `max in flight ${push.state.maxInFlight}`);
183 assert.ok(hosts.length + calls.length <= 50, `${hosts.length} fetches + ${calls.length} D1 calls`);
184 assert.equal(count(db, "SELECT count(*) AS n FROM alerts_sent WHERE kind = 'aurora'"), 18);
183 assert.ok(hosts.length + calls.length <= PUSH_SLICE + 30, `${hosts.length} fetches + ${calls.length} D1 calls`);
184 assert.equal(count(db, "SELECT count(*) AS n FROM alerts_sent WHERE kind = 'aurora'"), PUSH_SLICE - 2);
185185 assert.equal(count(db, `SELECT count(*) AS n FROM devices WHERE token = '${token(4)}'`), 0);
186186
187187 push.state.sent = [];
188188 await run(env, FIVE, "2026-10-20T22:10:00Z");
189 assert.deepEqual(push.state.sent.sort(), [3, ...Array.from({ length: 10 }, (_, i) => 20 + i)].map(token).sort());
189 assert.deepEqual(push.state.sent.sort(), [3, ...Array.from({ length: 10 }, (_, i) => PUSH_SLICE + i)].map(token).sort());
190190
191191 push.state.sent = [];
192192 await run(env, FIVE, "2026-10-20T22:15:00Z");
@@ -213,22 +213,22 @@ test("an aurora run cut short leaves the remaining devices for the next run", as
213213
214214test("news digests go out in slices during the local 09:00 hour, once per local day", async () => {
215215 const { db, env, calls } = await setup();
216 for (let i = 0; i < 25; i++) device(db, i, { offset: -300, aurora: 0, news: 1 });
217 device(db, 25, { offset: 330, aurora: 0, news: 1 });
216 for (let i = 0; i < PUSH_SLICE + 5; i++) device(db, i, { offset: -300, aurora: 0, news: 1 });
217 device(db, PUSH_SLICE + 5, { offset: 330, aurora: 0, news: 1 });
218218 const push = apns();
219219 const articles = [{ id: 1, title: "t1", news_site: "ESA", published_at: "2026-10-20T13:00:00Z" }];
220220 const hosts = network({ ...push.handlers, "api.spaceflightnewsapi.net": () => Response.json({ results: articles }) });
221221 const snapi = () => hosts.filter((h) => h === "api.spaceflightnewsapi.net").length;
222222
223223 await run(env, NEWS, "2026-10-20T14:00:00Z");
224 assert.equal(push.state.sent.length, 20);
224 assert.equal(push.state.sent.length, PUSH_SLICE);
225225 await run(env, NEWS, "2026-10-20T14:10:00Z");
226 assert.equal(push.state.sent.length, 25);
227 assert.equal(new Set(push.state.sent).size, 25);
226 assert.equal(push.state.sent.length, PUSH_SLICE + 5);
227 assert.equal(new Set(push.state.sent).size, PUSH_SLICE + 5);
228228 await run(env, NEWS, "2026-10-20T14:20:00Z");
229 assert.equal(push.state.sent.length, 25);
229 assert.equal(push.state.sent.length, PUSH_SLICE + 5);
230230 assert.equal(snapi(), 2);
231 assert.equal(count(db, "SELECT count(*) AS n FROM alerts_sent WHERE kind = 'news' AND event LIKE '%:2026-10-20'"), 25);
231 assert.equal(count(db, "SELECT count(*) AS n FROM alerts_sent WHERE kind = 'news' AND event LIKE '%:2026-10-20'"), PUSH_SLICE + 5);
232232
233233 const q = calls.find((c) => c.sql.includes("news = 1"))!;
234234 const plan = db.prepare(`EXPLAIN QUERY PLAN ${q.sql}`).all(...q.params).map((r) => String(r.detail)).join("\n");