Skip to content

Commit

Permalink
add: write panic's stacktrace to a log file
Browse files Browse the repository at this point in the history
  • Loading branch information
CorentinB committed Aug 28, 2024
1 parent ef8d869 commit 672cd53
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions internal/pkg/crawl/crawl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
package crawl

import (
"fmt"
"os"
"path"
"runtime/debug"
"sync"
"time"

Expand All @@ -25,6 +27,25 @@ type PrometheusMetrics struct {

// Start fire up the crawling process
func (c *Crawl) Start() (err error) {
defer func() {
if r := recover(); r != nil {
// Write the stacktrace to a file in the job's directory
stacktrace := fmt.Sprintf("%s\n%s", r, debug.Stack())
stacktracePath := path.Join(c.JobPath, "logs", fmt.Sprintf("%s.%s.log", "panic", time.Now().Format("2006.01.02T15-04")))
f, err := os.Create(stacktracePath)
if err != nil {
c.Log.Fatal("unable to create stacktrace file", "error", err)
}
defer f.Close()

if _, err := f.WriteString(stacktrace); err != nil {
c.Log.Fatal("unable to write stacktrace to file", "error", err)
}

c.Log.Fatal("panic occurred, stacktrace written to file", "file", stacktracePath)
}
}()

c.StartTime = time.Now()
c.Paused = new(utils.TAtomBool)
c.Finished = new(utils.TAtomBool)
Expand Down

0 comments on commit 672cd53

Please sign in to comment.