Build the agent-native compute CRM platform
CI / verify (push) Successful in 3m6s

This commit is contained in:
2026-08-13 01:39:01 -07:00
parent bfd2f8d95a
commit 853bde2265
160 changed files with 61812 additions and 483 deletions
+45 -2
View File
@@ -16,10 +16,19 @@ import { loadConfig } from './lib/config';
import { startPrimeSync } from './services/sync';
import { reconcileInviteCode } from './services/bootstrap';
import { CapacityService } from './services/capacity';
import { effectivePrimeSyncConfig } from './routes/admin-settings';
import { NotificationOutbox, NotificationWorker } from './services/notification-outbox';
import { SlackNotifier } from './services/slack';
import { BuzzNotifier } from './services/buzz';
const config = loadConfig();
const db = createDatabase({ url: config.DATABASE_URL });
const app = createApp(config, db);
let stopSync = () => {};
async function reloadPrimeSync(): Promise<void> {
stopSync();
stopSync = startPrimeSync(await effectivePrimeSyncConfig(config, db), db);
}
const app = createApp(config, db, undefined, { onPlatformSettingsChanged: reloadPrimeSync });
// Serve the built SPA when it exists. Absent in development, where Vite serves
// it on its own port with hot reload.
@@ -67,9 +76,40 @@ const server = serve({ fetch: app.fetch, port: config.PIG_PORT }, (info) => {
// Background work. Both are optional and the application is fully usable with
// neither running.
const stopSync = startPrimeSync(config, db);
await reloadPrimeSync();
const capacity = new CapacityService(db);
const notificationOutbox = new NotificationOutbox(db);
const slackNotificationsEnabled = Boolean(config.SLACK_BOT_TOKEN);
const buzzNotificationsEnabled = Boolean(config.BUZZ_RELAY_URL && config.BUZZ_PRIVATE_KEY);
const stopSlackNotifications = config.SLACK_BOT_TOKEN
? new NotificationWorker(db, new SlackNotifier({ botToken: config.SLACK_BOT_TOKEN })).start()
: () => {};
const stopBuzzNotifications = config.BUZZ_RELAY_URL && config.BUZZ_PRIVATE_KEY
? new NotificationWorker(
db,
new BuzzNotifier({
relayUrl: config.BUZZ_RELAY_URL,
privateKey: config.BUZZ_PRIVATE_KEY,
authTag: config.BUZZ_AUTH_TAG,
}),
).start()
: () => {};
let checkingIdleCapacity = false;
const checkIdleCapacity = () => {
if (checkingIdleCapacity || (!slackNotificationsEnabled && !buzzNotificationsEnabled)) return;
checkingIdleCapacity = true;
void capacity
.idleCapacity({ thresholdPct: 0.25, withinDays: 30 })
.then((rows) => notificationOutbox.enqueueIdleCapacity(rows))
.catch((error) => console.error('[pig] idle-capacity notification queue failed', error))
.finally(() => {
checkingIdleCapacity = false;
});
};
const idleNotificationTimer = setInterval(checkIdleCapacity, 6 * 60 * 60 * 1_000);
idleNotificationTimer.unref();
checkIdleCapacity();
const holdSweeper = setInterval(
() => {
void capacity
@@ -87,6 +127,9 @@ const holdSweeper = setInterval(
function shutdown(signal: string) {
console.log(`[pig] ${signal} received, shutting down`);
clearInterval(holdSweeper);
clearInterval(idleNotificationTimer);
stopSlackNotifications();
stopBuzzNotifications();
stopSync();
server.close(() => process.exit(0));
// Do not hang forever if a connection refuses to drain.