fix(workers): skip background jobs when required tables are missing
This commit is contained in:
@@ -2,6 +2,7 @@ import { and, eq, lte } from 'drizzle-orm';
|
||||
|
||||
import { db } from '../db/client';
|
||||
import { devices, pushNotifications } from '../db/schema';
|
||||
import { hasRequiredTables } from '../utils/db-schema';
|
||||
|
||||
const MAX_ATTEMPTS = Number(process.env.PUSH_MAX_ATTEMPTS ?? 5);
|
||||
|
||||
@@ -83,10 +84,23 @@ export const dispatchPushQueueOnce = async (): Promise<number> => {
|
||||
|
||||
export const startPushWorker = (): void => {
|
||||
const intervalMs = Number(process.env.PUSH_WORKER_INTERVAL_MS ?? 10_000);
|
||||
const requiredTables = ['push_notifications', 'devices'];
|
||||
|
||||
setInterval(() => {
|
||||
dispatchPushQueueOnce().catch((error) => {
|
||||
console.error('push worker failed', error);
|
||||
});
|
||||
}, intervalMs);
|
||||
void (async () => {
|
||||
const ready = await hasRequiredTables(requiredTables);
|
||||
if (!ready) {
|
||||
console.warn(
|
||||
`[push worker] skipped startup because required tables are missing (${requiredTables.join(', ')}). Run migrations and restart.`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
setInterval(() => {
|
||||
dispatchPushQueueOnce().catch((error) => {
|
||||
console.error('push worker failed', error);
|
||||
});
|
||||
}, intervalMs);
|
||||
})().catch((error) => {
|
||||
console.error('push worker failed to initialize', error);
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user