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).
470 device and add one-object alignment with a gyroscope heading. 470 device and add one-object alignment with a gyroscope heading.
471 471
472* before publishing 472* 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.
476- Decide on server-side caching: a custom domain with the Cache API, D1 read 473- Decide on server-side caching: a custom domain with the Cache API, D1 read
477 replication or Smart Placement. =workers.dev= does not cache Worker 474 replication or Smart Placement. =workers.dev= does not cache Worker
478 responses, and every D1 round trip goes to San Jose (about 85 ms from the 475 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;
37/** Devices are dropped after this long without registering; the app registers whenever iOS hands it a token. */ 37/** Devices are dropped after this long without registering; the app registers whenever iOS hands it a token. */
38const DEVICE_RETENTION_MS = 180 * 86400_000; 38const DEVICE_RETENTION_MS = 180 * 86400_000;
39/** 39/**
40 * Alert pushes per run; the rest go on later runs. Sized for the Free plan's 50 subrequests per invocation, shared 40 * Alert pushes per run; the rest go on later runs. The Paid plan allows 10,000 subrequests per invocation; at 6
41 * with the rest of the run; raise it on Paid. Workers keep at most 6 connections open at once. 41 * connections open at once, 200 sends take a few seconds of the five-minute run.
42 */ 42 */
43const PUSH_SLICE = 20; 43export const PUSH_SLICE = 200;
44const PUSH_CONCURRENCY = 6; 44const PUSH_CONCURRENCY = 6;
45 45
46const json = (body: unknown, status = 200, maxAge?: number) => 46const json = (body: unknown, status = 200, maxAge?: number) =>
server/test/worker.test.ts +14 −14
@@ -15,7 +15,7 @@ registerHooks({
15 } 15 }
16 }, 16 },
17}); 17});
18const { default: worker } = await import("../src/index.ts"); 18const { default: worker, PUSH_SLICE } = await import("../src/index.ts");
19 19
20const FIVE = "*/5 * * * *"; 20const FIVE = "*/5 * * * *";
21const TWO = "*/2 * * * *"; 21const TWO = "*/2 * * * *";
@@ -172,21 +172,21 @@ test("a failed CelesTrak poll is tried once more 33 minutes later, from the 5-mi
172 172
173test("aurora alerts go out in bounded slices and are recorded per device once answered", async () => { 173test("aurora alerts go out in bounded slices and are recorded per device once answered", async () => {
174 const { db, env, calls } = await setup(); 174 const { db, env, calls } = await setup();
175 for (let i = 0; i < 30; i++) device(db, i); 175 for (let i = 0; i < PUSH_SLICE + 10; i++) device(db, i);
176 device(db, 30, { offset: 720 }); // 10:05 local: not night 176 device(db, PUSH_SLICE + 10, { offset: 720 }); // 10:05 local: not night
177 const push = apns({ [token(3)]: 503, [token(4)]: 410, [token(5)]: 400 }); 177 const push = apns({ [token(3)]: 503, [token(4)]: 410, [token(5)]: 400 });
178 const hosts = network({ ...swpc(6), ...push.handlers }); 178 const hosts = network({ ...swpc(6), ...push.handlers });
179 179
180 await run(env, FIVE, "2026-10-20T22:05:00Z"); 180 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);
182 assert.ok(push.state.maxInFlight <= 6, `max in flight ${push.state.maxInFlight}`); 182 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`); 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'"), 18); 184 assert.equal(count(db, "SELECT count(*) AS n FROM alerts_sent WHERE kind = 'aurora'"), PUSH_SLICE - 2);
185 assert.equal(count(db, `SELECT count(*) AS n FROM devices WHERE token = '${token(4)}'`), 0); 185 assert.equal(count(db, `SELECT count(*) AS n FROM devices WHERE token = '${token(4)}'`), 0);
186 186
187 push.state.sent = []; 187 push.state.sent = [];
188 await run(env, FIVE, "2026-10-20T22:10:00Z"); 188 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());
190 190
191 push.state.sent = []; 191 push.state.sent = [];
192 await run(env, FIVE, "2026-10-20T22:15:00Z"); 192 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
213 213
214test("news digests go out in slices during the local 09:00 hour, once per local day", async () => { 214test("news digests go out in slices during the local 09:00 hour, once per local day", async () => {
215 const { db, env, calls } = await setup(); 215 const { db, env, calls } = await setup();
216 for (let i = 0; i < 25; i++) device(db, i, { offset: -300, 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, 25, { offset: 330, aurora: 0, news: 1 }); 217 device(db, PUSH_SLICE + 5, { offset: 330, aurora: 0, news: 1 });
218 const push = apns(); 218 const push = apns();
219 const articles = [{ id: 1, title: "t1", news_site: "ESA", published_at: "2026-10-20T13:00:00Z" }]; 219 const articles = [{ id: 1, title: "t1", news_site: "ESA", published_at: "2026-10-20T13:00:00Z" }];
220 const hosts = network({ ...push.handlers, "api.spaceflightnewsapi.net": () => Response.json({ results: articles }) }); 220 const hosts = network({ ...push.handlers, "api.spaceflightnewsapi.net": () => Response.json({ results: articles }) });
221 const snapi = () => hosts.filter((h) => h === "api.spaceflightnewsapi.net").length; 221 const snapi = () => hosts.filter((h) => h === "api.spaceflightnewsapi.net").length;
222 222
223 await run(env, NEWS, "2026-10-20T14:00:00Z"); 223 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);
225 await run(env, NEWS, "2026-10-20T14:10:00Z"); 225 await run(env, NEWS, "2026-10-20T14:10:00Z");
226 assert.equal(push.state.sent.length, 25); 226 assert.equal(push.state.sent.length, PUSH_SLICE + 5);
227 assert.equal(new Set(push.state.sent).size, 25); 227 assert.equal(new Set(push.state.sent).size, PUSH_SLICE + 5);
228 await run(env, NEWS, "2026-10-20T14:20:00Z"); 228 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);
230 assert.equal(snapi(), 2); 230 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);
232 232
233 const q = calls.find((c) => c.sql.includes("news = 1"))!; 233 const q = calls.find((c) => c.sql.includes("news = 1"))!;
234 const plan = db.prepare(`EXPLAIN QUERY PLAN ${q.sql}`).all(...q.params).map((r) => String(r.detail)).join("\n"); 234 const plan = db.prepare(`EXPLAIN QUERY PLAN ${q.sql}`).all(...q.params).map((r) => String(r.detail)).join("\n");