Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Added panic recover defers for capturing panics inside cron jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Jun 6, 2022
1 parent f27a7bb commit e89fe9b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
8 changes: 7 additions & 1 deletion model_incoming_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,15 @@ func processIncomingTransactions(ctx context.Context, maxTransactions int, opts
// processIncomingTransaction will process the incoming transaction record into a transaction, or save the failure
func processIncomingTransaction(ctx context.Context, incomingTx *IncomingTransaction) error {

// Successfully capture any panics, convert to readable string and log the error
defer func() {
if err := recover(); err != nil {
incomingTx.Client().Logger().Error(ctx, fmt.Sprintf("panic occurred: %v - stack: %v", err, string(debug.Stack())))
incomingTx.Client().Logger().Error(ctx,
fmt.Sprintf(
"panic: %v - stack trace: %v", err,
strings.ReplaceAll(string(debug.Stack()), "\n", ""),
),
)
}
}()

Expand Down
38 changes: 38 additions & 0 deletions model_sync_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"database/sql"
"errors"
"fmt"
"runtime/debug"
"strings"
"time"

"github.com/BuxOrg/bux/chainstate"
Expand Down Expand Up @@ -373,6 +375,18 @@ func processBroadcastTransactions(ctx context.Context, maxTransactions int, opts
// processBroadcastTransaction will process the sync transaction record, or save the failure
func processBroadcastTransaction(ctx context.Context, syncTx *SyncTransaction) error {

// Successfully capture any panics, convert to readable string and log the error
defer func() {
if err := recover(); err != nil {
syncTx.Client().Logger().Error(ctx,
fmt.Sprintf(
"panic: %v - stack trace: %v", err,
strings.ReplaceAll(string(debug.Stack()), "\n", ""),
),
)
}
}()

// Create the lock and set the release for after the function completes
unlock, err := newWriteLock(
ctx, fmt.Sprintf(lockKeyProcessBroadcastTx, syncTx.GetID()), syncTx.Client().Cachestore(),
Expand Down Expand Up @@ -455,6 +469,18 @@ func processBroadcastTransaction(ctx context.Context, syncTx *SyncTransaction) e
// processSyncTransaction will process the sync transaction record, or save the failure
func processSyncTransaction(ctx context.Context, syncTx *SyncTransaction, transaction *Transaction) error {

// Successfully capture any panics, convert to readable string and log the error
defer func() {
if err := recover(); err != nil {
syncTx.Client().Logger().Error(ctx,
fmt.Sprintf(
"panic: %v - stack trace: %v", err,
strings.ReplaceAll(string(debug.Stack()), "\n", ""),
),
)
}
}()

// Create the lock and set the release for after the function completes
unlock, err := newWriteLock(
ctx, fmt.Sprintf(lockKeyProcessSyncTx, syncTx.GetID()), syncTx.Client().Cachestore(),
Expand Down Expand Up @@ -557,6 +583,18 @@ func processP2PTransactions(ctx context.Context, maxTransactions int, opts ...Mo
// processP2PTransaction will process the sync transaction record, or save the failure
func processP2PTransaction(ctx context.Context, syncTx *SyncTransaction, transaction *Transaction) error {

// Successfully capture any panics, convert to readable string and log the error
defer func() {
if err := recover(); err != nil {
syncTx.Client().Logger().Error(ctx,
fmt.Sprintf(
"panic: %v - stack trace: %v", err,
strings.ReplaceAll(string(debug.Stack()), "\n", ""),
),
)
}
}()

// Create the lock and set the release for after the function completes
unlock, err := newWriteLock(
ctx, fmt.Sprintf(lockKeyProcessP2PTx, syncTx.GetID()), syncTx.Client().Cachestore(),
Expand Down

0 comments on commit e89fe9b

Please sign in to comment.