Skip to content

Commit 67a34d7

Browse files
authored
feat: add a way to disable background sync jobs (#849)
1 parent 0fc0cd2 commit 67a34d7

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

billing/checkout/service.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ func NewService(stripeClient *client.API, cfg billing.Config, repository Reposit
141141
}
142142

143143
func (s *Service) Init(ctx context.Context) error {
144+
if s.syncDelay == time.Duration(0) {
145+
return nil
146+
}
144147
if s.syncJob != nil {
145148
<-s.syncJob.Stop().Done()
146149
}

billing/customer/service.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@ func (s *Service) ListPaymentMethods(ctx context.Context, id string) ([]PaymentM
349349
}
350350

351351
func (s *Service) Init(ctx context.Context) error {
352+
if s.syncDelay == time.Duration(0) {
353+
return nil
354+
}
352355
if s.syncJob != nil {
353356
<-s.syncJob.Stop().Done()
354357
}

billing/invoice/service.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,24 @@ func NewService(stripeClient *client.API, invoiceRepository Repository,
104104

105105
func (s *Service) Init(ctx context.Context) error {
106106
logger := grpczap.Extract(ctx)
107-
if s.syncJob != nil {
108-
s.syncJob.Stop()
109-
}
110-
s.syncJob = cron.New(cron.WithChain(
111-
cron.SkipIfStillRunning(cron.DefaultLogger),
112-
cron.Recover(cron.DefaultLogger),
113-
))
114-
115-
if _, err := s.syncJob.AddFunc(fmt.Sprintf("@every %s", s.syncDelay.String()), func() {
116-
ctx, cancel := context.WithCancel(ctx)
117-
defer cancel()
118-
s.backgroundSync(ctx)
119-
}); err != nil {
120-
return err
107+
if s.syncDelay != time.Duration(0) {
108+
if s.syncJob != nil {
109+
s.syncJob.Stop()
110+
}
111+
s.syncJob = cron.New(cron.WithChain(
112+
cron.SkipIfStillRunning(cron.DefaultLogger),
113+
cron.Recover(cron.DefaultLogger),
114+
))
115+
116+
if _, err := s.syncJob.AddFunc(fmt.Sprintf("@every %s", s.syncDelay.String()), func() {
117+
ctx, cancel := context.WithCancel(ctx)
118+
defer cancel()
119+
s.backgroundSync(ctx)
120+
}); err != nil {
121+
return err
122+
}
123+
s.syncJob.Start()
121124
}
122-
s.syncJob.Start()
123125

124126
if s.creditOverdraftProduct != "" {
125127
creditProduct, err := s.productService.GetByID(ctx, s.creditOverdraftProduct)

billing/subscription/service.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ func (s *Service) GetByProviderID(ctx context.Context, id string) (Subscription,
112112
}
113113

114114
func (s *Service) Init(ctx context.Context) error {
115+
syncDelay := s.config.RefreshInterval.Subscription
116+
if syncDelay == time.Duration(0) {
117+
return nil
118+
}
115119
if s.syncJob != nil {
116120
<-s.syncJob.Stop().Done()
117121
}
@@ -120,7 +124,7 @@ func (s *Service) Init(ctx context.Context) error {
120124
cron.SkipIfStillRunning(cron.DefaultLogger),
121125
cron.Recover(cron.DefaultLogger),
122126
))
123-
if _, err := s.syncJob.AddFunc(fmt.Sprintf("@every %s", s.config.RefreshInterval.Subscription.String()), func() {
127+
if _, err := s.syncJob.AddFunc(fmt.Sprintf("@every %s", syncDelay.String()), func() {
124128
ctx, cancel := context.WithCancel(ctx)
125129
defer cancel()
126130
s.backgroundSync(ctx)

0 commit comments

Comments
 (0)