Skip to content

Commit 3672912

Browse files
authored
Added application restart if webpage load does not return 2xx code (#14)
1 parent 81442a3 commit 3672912

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

cmd/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"syscall"
1515
"time"
1616

17+
"github.com/chromedp/cdproto/network"
1718
"github.com/chromedp/chromedp"
1819
"github.com/nicklaw5/helix/v2"
1920
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -377,13 +378,32 @@ func streamWebpage(ctx context.Context, config *Config) error {
377378
// Start Chrome and navigate to webpage
378379
logger.Info("Starting Chrome browser", zap.String("url", config.WebpageURL))
379380

381+
// Capture the status code when the page loads
382+
var statusCode int64
383+
chromedp.ListenTarget(chromeCtx, func(ev interface{}) {
384+
switch ev := ev.(type) {
385+
case *network.EventResponseReceived:
386+
if ev.Response.URL == config.WebpageURL {
387+
statusCode = ev.Response.Status
388+
}
389+
}
390+
})
391+
392+
// Load the page
380393
if err := chromedp.Run(chromeCtx,
381394
chromedp.Navigate(config.WebpageURL),
382395
chromedp.WaitVisible("body", chromedp.ByQuery),
383396
); err != nil {
384397
return fmt.Errorf("failed to navigate to webpage: %v", err)
385398
}
386399

400+
// Log the page load result based on status code
401+
if statusCode >= 200 && statusCode < 300 {
402+
logger.Info("Page load completed successfully", zap.String("url", config.WebpageURL), zap.Int64("status_code", statusCode))
403+
} else {
404+
logger.Fatal("Page load failed with error status, terminating program", zap.String("url", config.WebpageURL), zap.Int64("status_code", statusCode))
405+
}
406+
387407
// Wait a moment for the page to fully load
388408
time.Sleep(3 * time.Second)
389409

0 commit comments

Comments
 (0)