Skip to content

Commit e49abe4

Browse files
add webhook fallback and fix debug messages
1 parent 15e3e69 commit e49abe4

File tree

3 files changed

+80
-5
lines changed

3 files changed

+80
-5
lines changed

src/core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ export const unlock = (refire?: boolean) => {
263263
}
264264
}
265265
check()
266+
return Promise.resolve()
266267
}
267268

268269
/**

src/index.ts

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,10 @@ export const start = (config: IConfig = {}): Promise<{}> => {
227227
pinger()
228228

229229
return listener.start(appConfig)
230-
}).then(() => {
230+
}).then((webhookServer) => {
231+
// Set up webhook listener monitoring and fallback mechanism
232+
setupWebhookMonitoring(webhookServer)
233+
231234
logger.info(MESSAGES.INDEX.SYNC_UTILITY_STARTED)
232235

233236
return resolve('')
@@ -255,3 +258,74 @@ notifications
255258
.on('unpublish', debugNotifications('unpublish'))
256259
.on('delete', debugNotifications('delete'))
257260
.on('error', debugNotifications('error'))
261+
262+
/**
263+
* Set up webhook listener monitoring and fallback polling mechanism
264+
* @param {object} webhookServer The webhook server instance
265+
*/
266+
function setupWebhookMonitoring(webhookServer) {
267+
const FALLBACK_POLL_INTERVAL = 60000 // 1 minute fallback polling
268+
let fallbackTimer: NodeJS.Timeout | null = null
269+
let webhookHealthy = true
270+
271+
debug('Webhook monitoring initialized. Server:', !!webhookServer, 'Healthy:', webhookHealthy)
272+
273+
274+
// Start fallback polling when webhook is unhealthy
275+
const startFallbackPolling = () => {
276+
if (fallbackTimer) return // Already running
277+
278+
logger.info(`Starting fallback polling every ${FALLBACK_POLL_INTERVAL}ms`)
279+
fallbackTimer = setInterval(() => {
280+
debug('Fallback polling: triggering sync check')
281+
try {
282+
poke().catch((error) => {
283+
debug('Fallback polling error:', error)
284+
})
285+
} catch (error) {
286+
debug('Fallback polling exception:', error)
287+
}
288+
}, FALLBACK_POLL_INTERVAL)
289+
}
290+
291+
// Stop fallback polling when webhook is healthy
292+
const stopFallbackPolling = () => {
293+
if (fallbackTimer) {
294+
clearInterval(fallbackTimer)
295+
fallbackTimer = null
296+
logger.info('Fallback polling stopped')
297+
}
298+
}
299+
300+
// Webhook activity is tracked via events, no need to wrap poke function
301+
302+
303+
// Handle process cleanup
304+
const cleanup = () => {
305+
if (fallbackTimer) {
306+
clearInterval(fallbackTimer)
307+
fallbackTimer = null
308+
}
309+
}
310+
311+
process.on('SIGINT', cleanup)
312+
process.on('SIGTERM', cleanup)
313+
process.on('exit', cleanup)
314+
315+
// Handle webhook server events if available
316+
if (listener.getEventEmitter) {
317+
const webhookEmitter = listener.getEventEmitter()
318+
319+
webhookEmitter.on('server-error', (error) => {
320+
logger.warn('Webhook server error detected:', error.message)
321+
webhookHealthy = false
322+
startFallbackPolling()
323+
})
324+
325+
webhookEmitter.on('reconnect-success', () => {
326+
logger.info('Webhook server reconnected successfully')
327+
webhookHealthy = true
328+
stopFallbackPolling()
329+
})
330+
}
331+
}

src/util/messages.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ export const MESSAGES = {
7979
FIRE_CALLED: (req: any) => `Fire operation triggered with: ${JSON.stringify(req)}`,
8080
FIRE_COMPLETE: (itemCount: number) => `Fire operation completed. Items received: ${itemCount}.`,
8181
API_CALL_CT: (uid: string) => `API call initiated for content type: ${uid}.`,
82-
ERROR_MAP: 'Error [map]: Failed to fetch content type schema.',
83-
ERROR_MAP_RESOLVE: 'Error [mapResolve]: Unable to resolve mapping.',
84-
ERROR_FILTER_ITEMS: 'Error [filterItems]: Unable to filter items.',
85-
ERROR_FIRE: 'Error during fire operation.',
82+
ERROR_MAP: 'Debug [map]: Content type schema fetch failed.',
83+
ERROR_MAP_RESOLVE: 'Debug [mapResolve]: Mapping resolution failed.',
84+
ERROR_FILTER_ITEMS: 'Debug [filterItems]: Item filtering failed.',
85+
ERROR_FIRE: 'Debug [fire]: Fire operation encountered error.',
8686
REFIRE_CALLED: (req: any) => `Re-fire operation triggered with: ${JSON.stringify(req)}`,
8787
CHECKPOINT_LOCKDOWN: 'Checkpoint: lockdown has been invoked',
8888
},

0 commit comments

Comments
 (0)