@@ -7,6 +7,7 @@ import fs from 'fs';
77import { globalAgent } from 'http' ;
88import { promisify } from 'util'
99import { build } from 'tsup' ;
10+ const util = require ( 'util' ) ; // Import the util module
1011
1112var lambdaTunnel = require ( '@lambdatest/node-tunnel' ) ;
1213const sleep = promisify ( setTimeout ) ;
@@ -248,9 +249,9 @@ export async function startPolling(ctx: Context, build_id: string, baseline: boo
248249 try {
249250 let resp ;
250251 if ( build_id ) {
251- resp = await ctx . client . getScreenshotData ( build_id , baseline , ctx . log , projectToken ) ;
252+ resp = await ctx . client . getScreenshotData ( build_id , baseline , ctx . log , projectToken , '' ) ;
252253 } else if ( ctx . build && ctx . build . id ) {
253- resp = await ctx . client . getScreenshotData ( ctx . build . id , ctx . build . baseline , ctx . log , '' ) ;
254+ resp = await ctx . client . getScreenshotData ( ctx . build . id , ctx . build . baseline , ctx . log , '' , '' ) ;
254255 } else {
255256 return ;
256257 }
@@ -324,7 +325,7 @@ export async function startPolling(ctx: Context, build_id: string, baseline: boo
324325
325326export let pingIntervalId : NodeJS . Timeout | null = null ;
326327
327- export async function startPingPolling ( ctx : Context , event : string ) : Promise < void > {
328+ export async function startPingPolling ( ctx : Context ) : Promise < void > {
328329 try {
329330 ctx . log . debug ( 'Sending initial ping to server...' ) ;
330331 await ctx . client . ping ( ctx . build . id , ctx . log ) ;
@@ -333,12 +334,13 @@ export async function startPingPolling(ctx: Context, event: string): Promise<voi
333334 ctx . log . error ( `Error during initial ping: ${ error . message } ` ) ;
334335 }
335336
337+ let sourceCommand = ctx . sourceCommand ? ctx . sourceCommand : '' ;
336338 // Start the polling interval
337339 pingIntervalId = setInterval ( async ( ) => {
338340 try {
339- ctx . log . debug ( 'Sending ping to server...' + event ) ;
341+ ctx . log . debug ( 'Sending ping to server... ' + sourceCommand ) ;
340342 await ctx . client . ping ( ctx . build . id , ctx . log ) ;
341- ctx . log . debug ( 'Ping sent successfully.' + event ) ;
343+ ctx . log . debug ( 'Ping sent successfully. ' + sourceCommand ) ;
342344 } catch ( error : any ) {
343345 ctx . log . error ( `Error during ping polling: ${ error . message } ` ) ;
344346 }
@@ -406,54 +408,56 @@ export async function startTunnelBinary(ctx: Context) {
406408 }
407409}
408410
409- export async function startPollingForTunnel ( ctx : Context , build_id : string , baseline : boolean , projectToken : string ) : Promise < void > {
411+ export let isTunnelPolling : NodeJS . Timeout | null = null ;
412+
413+ export async function startPollingForTunnel ( ctx : Context , build_id : string , baseline : boolean , projectToken : string , buildName : string ) : Promise < void > {
414+ if ( isTunnelPolling ) {
415+ ctx . log . debug ( 'Tunnel polling is already active. Skipping for build_id: ' + build_id ) ;
416+ return ;
417+ }
410418 const intervalId = setInterval ( async ( ) => {
411419 try {
412420 let resp ;
413421 if ( build_id ) {
414- resp = await ctx . client . getScreenshotData ( build_id , baseline , ctx . log , projectToken ) ;
422+ resp = await ctx . client . getScreenshotData ( build_id , baseline , ctx . log , projectToken , buildName ) ;
415423 } else if ( ctx . build && ctx . build . id ) {
416- resp = await ctx . client . getScreenshotData ( ctx . build . id , ctx . build . baseline , ctx . log , '' ) ;
424+ resp = await ctx . client . getScreenshotData ( ctx . build . id , ctx . build . baseline , ctx . log , '' , '' ) ;
417425 } else {
426+ ctx . log . debug ( 'No build information available for polling tunnel status.' ) ;
427+ clearInterval ( intervalId ) ;
428+ await stopTunnelHelper ( ctx ) ;
418429 return ;
419430 }
420-
431+ ctx . log . debug ( ' resp from polling for tunnel status: ' + JSON . stringify ( resp ) ) ;
421432 if ( ! resp . build ) {
422433 ctx . log . info ( "Error: Build data is null." ) ;
423434 clearInterval ( intervalId ) ;
424-
425- const tunnelRunningStatus = await tunnelInstance . isRunning ( ) ;
426- ctx . log . debug ( 'Running status of tunnel before stopping ? ' + tunnelRunningStatus ) ;
427-
428- const status = await tunnelInstance . stop ( ) ;
429- ctx . log . debug ( 'Tunnel is Stopped ? ' + status ) ;
430-
435+ await stopTunnelHelper ( ctx ) ;
431436 return ;
432437 }
433438
434439 if ( resp . build . build_status_ind === constants . BUILD_COMPLETE || resp . build . build_status_ind === constants . BUILD_ERROR ) {
435440 clearInterval ( intervalId ) ;
436-
437- const tunnelRunningStatus = await tunnelInstance . isRunning ( ) ;
438- ctx . log . debug ( 'Running status of tunnel before stopping ? ' + tunnelRunningStatus ) ;
439-
440- const status = await tunnelInstance . stop ( ) ;
441- ctx . log . debug ( 'Tunnel is Stopped ? ' + status ) ;
441+ await stopTunnelHelper ( ctx ) ;
442442 return ;
443443 }
444444 } catch ( error : any ) {
445- if ( error . message . includes ( 'ENOTFOUND' ) ) {
445+ if ( error ? .message . includes ( 'ENOTFOUND' ) ) {
446446 ctx . log . error ( 'Error: Network error occurred while fetching build status while polling. Please check your connection and try again.' ) ;
447447 clearInterval ( intervalId ) ;
448448 } else {
449- ctx . log . error ( `Error fetching build status while polling: ${ error . message } ` ) ;
449+ // Log the error in a human-readable format
450+ ctx . log . debug ( util . inspect ( error , { showHidden : false , depth : null } ) ) ;
451+ ctx . log . error ( `Error fetching build status while polling: ${ JSON . stringify ( error ) } ` ) ;
450452 }
451453 clearInterval ( intervalId ) ;
452454 }
453455 } , 5000 ) ;
456+ isTunnelPolling = intervalId ;
454457}
455458
456459export async function stopTunnelHelper ( ctx : Context ) {
460+ ctx . log . debug ( 'stop-tunnel:: Stopping the tunnel now' ) ;
457461 const tunnelRunningStatus = await tunnelInstance . isRunning ( ) ;
458462 ctx . log . debug ( 'stop-tunnel:: Running status of tunnel before stopping ? ' + tunnelRunningStatus ) ;
459463
0 commit comments