Skip to content

Commit

Permalink
feat(sdk): run a goroutine with panic recovery (#2959)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsamin authored and yesnault committed Jun 26, 2018
1 parent b20afc5 commit 59dccdd
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions sdk/common.go
Expand Up @@ -12,8 +12,11 @@ import (
"os"
"reflect"
"regexp"
"runtime"

"github.com/go-gorp/gorp"

"github.com/ovh/cds/sdk/log"
)

// EncryptFunc is a common type
Expand Down Expand Up @@ -95,3 +98,19 @@ func FileSHA512sum(filePath string) (string, error) {
sum := hex.EncodeToString(hashInBytes)
return sum, nil
}

// GoRoutine runs the function within a goroutine with a panic recovery
func GoRoutine(name string, fn func()) {
go func() {
defer func() {
if r := recover(); r != nil {
buf := make([]byte, 1<<16)
runtime.Stack(buf, true)
log.Error("[PANIC] %s Failed", name)
log.Error("[PANIC] %s> %s", name, string(buf))
}
}()
fn()
}()

}

0 comments on commit 59dccdd

Please sign in to comment.