2023-01-02 07:53:51 -08:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/certusone/wormhole/node/pkg/supervisor"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-01-04 11:58:04 -08:00
|
|
|
ScissorsErrors = promauto.NewCounterVec(
|
2023-01-02 07:53:51 -08:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "scissor_errors_caught",
|
|
|
|
Help: "Total number of unhandled errors caught",
|
2023-01-04 11:58:04 -08:00
|
|
|
}, []string{"scissors", "name"})
|
2023-01-02 07:53:51 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Start a go routine with recovering from any panic by sending an error to a error channel
|
|
|
|
func RunWithScissors(ctx context.Context, errC chan error, name string, runnable supervisor.Runnable) {
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
switch x := r.(type) {
|
|
|
|
case error:
|
|
|
|
errC <- fmt.Errorf("%s: %w", name, x)
|
|
|
|
default:
|
|
|
|
errC <- fmt.Errorf("%s: %v", name, x)
|
|
|
|
}
|
2023-01-04 12:21:40 -08:00
|
|
|
ScissorsErrors.WithLabelValues("scissors", name).Inc()
|
2023-01-04 11:58:04 -08:00
|
|
|
|
2023-01-02 07:53:51 -08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
err := runnable(ctx)
|
|
|
|
if err != nil {
|
|
|
|
errC <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-01-04 11:58:04 -08:00
|
|
|
func WrapWithScissors(runnable supervisor.Runnable, name string) supervisor.Runnable {
|
2023-01-04 10:39:52 -08:00
|
|
|
return func(ctx context.Context) (result error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
switch x := r.(type) {
|
|
|
|
case error:
|
2023-01-04 11:58:04 -08:00
|
|
|
result = fmt.Errorf("%s: %w", name, x)
|
2023-01-04 10:39:52 -08:00
|
|
|
default:
|
2023-01-04 11:58:04 -08:00
|
|
|
result = fmt.Errorf("%s: %v", name, x)
|
2023-01-04 10:39:52 -08:00
|
|
|
}
|
2023-01-04 12:21:40 -08:00
|
|
|
ScissorsErrors.WithLabelValues("scissors", name).Inc()
|
2023-01-02 07:53:51 -08:00
|
|
|
}
|
2023-01-04 10:39:52 -08:00
|
|
|
}()
|
2023-01-02 07:53:51 -08:00
|
|
|
|
2023-01-04 10:39:52 -08:00
|
|
|
return runnable(ctx)
|
|
|
|
}
|
2023-01-02 07:53:51 -08:00
|
|
|
}
|