Something went wrong. Try again.
APNs relay for Prefect for iOS: pushes a flow run's pause to your phone
Something went wrong. Try again.
1.8 kB · 46 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import { ApnsPusher, NoPusher } from "./apns.ts";import { loadConfig } from "./config.ts";import { Store } from "./db.ts";import { EventStream } from "./events.ts";import { handler } from "./http.ts";import { PrefectClient } from "./prefect.ts";import { Relay } from "./relay.ts";
const RECONCILE_MS = 60_000;
const log = (message: string) => console.log(`${new Date().toISOString()} ${message}`);const config = loadConfig();const store = new Store(config.dbPath);const prefect = new PrefectClient(config.apiUrl, config.auth);const pusher = config.apns ? new ApnsPusher(config.apns, config.topic) : new NoPusher();const relay = new Relay(store, prefect, pusher, log);
const reconcile = () => void relay.reconcile().catch((error: unknown) => log(`reconcile failed: ${error instanceof Error ? error.message : String(error)}`));
const events = new EventStream({ apiUrl: config.apiUrl, auth: config.auth, log, onSubscribed: reconcile, onEvent: (event) => void relay.onEvent(event).catch((error: unknown) => log(`event ${event.id} failed: ${error instanceof Error ? error.message : String(error)}`)),});
const server = Bun.serve({ port: config.port, fetch: handler({ store, prefect, relay, basePath: config.basePath, status: () => ({ subscribed: events.subscribed, apns: pusher.configured }) }),});
events.start();const timer = setInterval(reconcile, RECONCILE_MS);log(`listening on :${server.port}${config.basePath}, watching ${config.apiUrl}, APNs ${pusher.configured ? "configured" : "NOT configured: nothing will be sent"}`);
for (const signal of ["SIGINT", "SIGTERM"] as const) { process.on(signal, () => { clearInterval(timer); events.stop(); void server.stop().then(() => { store.close(); process.exit(0); }); });}