From 59dccdd606f723c7e600f606208c0537c9cc0005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Samin?= Date: Tue, 26 Jun 2018 14:00:27 +0200 Subject: [PATCH] feat(sdk): run a goroutine with panic recovery (#2959) --- sdk/common.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sdk/common.go b/sdk/common.go index e5854c18da..ce26ba5254 100644 --- a/sdk/common.go +++ b/sdk/common.go @@ -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 @@ -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() + }() + +}