@@ -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+ }
0 commit comments