Skip to content

Commit

Permalink
feat: add panic recover for probe
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed Apr 17, 2021
1 parent 06cf10b commit af4293e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
14 changes: 7 additions & 7 deletions exporter/templates_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package exporter

import (
"reflect"
"fmt"
"reflect"

"testing"
)
Expand Down Expand Up @@ -45,9 +45,9 @@ func TestInvertIncidents(t *testing.T) {
func TestBreakText(t *testing.T) {
f := templateFuncs["break_text"].(func(string, int) []string)

tests := []struct{
Input string
Width int
tests := []struct {
Input string
Width int
Output []string
}{
{"hello_world", 20, []string{"hello_world"}},
Expand All @@ -69,9 +69,9 @@ func TestBreakText(t *testing.T) {
func TestAlignCenter(t *testing.T) {
f := templateFuncs["align_center"].(func(string, int) string)

tests := []struct{
Input string
Width int
tests := []struct {
Input string
Width int
Output string
}{
{"foobar", 10, " foobar"},
Expand Down
15 changes: 13 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"sync"
"sync/atomic"
"time"

"github.com/macrat/ayd/exporter"
"github.com/macrat/ayd/probe"
Expand Down Expand Up @@ -198,9 +199,19 @@ func RunServer(s *store.Store, tasks []Task) {

s.AddTarget(t.Probe.Target())

f := t.Probe.Check
p := t.Probe
job := func() {
s.Append(f()...)
defer func() {
if err := recover(); err != nil {
s.Append(store.Record{
CheckedAt: time.Now(),
Target: p.Target(),
Status: store.STATUS_UNKNOWN,
Message: fmt.Sprintf("panic: %s", err),
})
}
}()
s.Append(p.Check()...)
}

if t.Schedule.NeedKickWhenStart() {
Expand Down

0 comments on commit af4293e

Please sign in to comment.