diff --git a/pkg/jobs/jobs.go b/pkg/jobs/jobs.go index ac96ae9e..b8f1319c 100644 --- a/pkg/jobs/jobs.go +++ b/pkg/jobs/jobs.go @@ -5,6 +5,7 @@ import ( "math" "time" + "github.com/getsentry/sentry-go" "github.com/monetr/rest-api/pkg/internal/platypus" "github.com/go-pg/pg/v10" @@ -234,3 +235,13 @@ func (j *jobManagerBase) Close() error { j.work.Stop() return nil } + +func (j *jobManagerBase) recover(ctx context.Context) { + if hub := sentry.GetHubFromContext(ctx); hub != nil { + if err := recover(); err != nil { + hub.RecoverWithContext(ctx, err) + } + } else { + sentry.RecoverWithContext(ctx) + } +} \ No newline at end of file diff --git a/pkg/jobs/pull_account_balances.go b/pkg/jobs/pull_account_balances.go index 684123ee..3ffff363 100644 --- a/pkg/jobs/pull_account_balances.go +++ b/pkg/jobs/pull_account_balances.go @@ -81,16 +81,19 @@ func (j *jobManagerBase) enqueuePullAccountBalances(job *work.Job) error { } func (j *jobManagerBase) pullAccountBalances(job *work.Job) (err error) { + hub := sentry.CurrentHub().Clone() + ctx := sentry.SetHubOnContext(context.Background(), hub) + span := sentry.StartSpan(ctx, "Job", sentry.TransactionName("Pull Account Balances")) + defer span.Finish() + + defer j.recover(span.Context()) + defer func() { if err := recover(); err != nil { sentry.CaptureException(errors.Errorf("pull account balances failure: %+v", err)) } }() - hub := sentry.CurrentHub().Clone() - ctx := sentry.SetHubOnContext(context.Background(), hub) - span := sentry.StartSpan(ctx, "Job", sentry.TransactionName("Pull Account Balances")) - defer span.Finish() start := time.Now() log := j.getLogForJob(job) diff --git a/pkg/jobs/pull_latest_transactions.go b/pkg/jobs/pull_latest_transactions.go index 74c7dfc6..4c6069c7 100644 --- a/pkg/jobs/pull_latest_transactions.go +++ b/pkg/jobs/pull_latest_transactions.go @@ -88,20 +88,17 @@ func (j *jobManagerBase) enqueuePullLatestTransactions(job *work.Job) error { } func (j *jobManagerBase) pullLatestTransactions(job *work.Job) (err error) { - defer func() { - if err := recover(); err != nil { - sentry.CaptureException(errors.Errorf("pull latest transactions failure: %+v", err)) - } - }() - - log := j.getLogForJob(job) - log.Infof("pulling account balances") - hub := sentry.CurrentHub().Clone() ctx := sentry.SetHubOnContext(context.Background(), hub) span := sentry.StartSpan(ctx, "Job", sentry.TransactionName("Pull Latest Transactions")) defer span.Finish() + defer j.recover(span.Context()) + + log := j.getLogForJob(job) + log.Infof("pulling account balances") + + defer func() { if err != nil { hub.CaptureException(err) diff --git a/pkg/jobs/transactions_removed.go b/pkg/jobs/transactions_removed.go index 2dcf7708..720e49ac 100644 --- a/pkg/jobs/transactions_removed.go +++ b/pkg/jobs/transactions_removed.go @@ -32,17 +32,13 @@ func (j *jobManagerBase) TriggerRemoveTransactions(accountId, linkId uint64, rem } func (j *jobManagerBase) removeTransactions(job *work.Job) error { - defer func() { - if err := recover(); err != nil { - sentry.CaptureException(errors.Errorf("remove transactions failure: %+v", err)) - } - }() - hub := sentry.CurrentHub().Clone() ctx := sentry.SetHubOnContext(context.Background(), hub) span := sentry.StartSpan(ctx, "Job", sentry.TransactionName("Remove Transactions")) defer span.Finish() + defer j.recover(span.Context()) + log := j.getLogForJob(job) transactionIds := strings.Split(job.ArgString("removedTransactions"), ",")