Skip to content

Commit b2314f4

Browse files
fix(cron): wait for scheduler to stop and add trace
1 parent 4200545 commit b2314f4

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

cmd/smtpbridge/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func run(flags *flag.FlagSet) lieut.Executor {
134134
}
135135

136136
if cfg.HealthcheckURL != "" {
137-
job := cron.NewHealthcheck(cfg.HealthcheckURL)
137+
job := cron.NewHealthcheck(app, cfg.HealthcheckURL)
138138
trigger := quartz.NewSimpleTrigger(cfg.HealthcheckInterval)
139139
if err := scheduler.ScheduleJob(ctx, job, trigger); err != nil {
140140
return err

cron/cron.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func (RetentionPolicy) Description() string {
2828
func (r RetentionPolicy) Execute(ctx context.Context) {
2929
err := r.app.RetentionPolicyRun(ctx, r.app.Tracer(trace.SourceCron))
3030
if err != nil {
31+
r.app.Tracer(trace.SourceCron).Trace(ctx, "cron.RetentionPolicy", trace.WithError(err))
3132
log.Err(err).Msg("Failed to run app.RetentionPolicyRun")
3233
}
3334
}
@@ -64,12 +65,14 @@ func (r AttachmentOrphan) Key() int {
6465

6566
// Healthcheck
6667
type Healthcheck struct {
67-
URL string
68+
app core.App
69+
url string
6870
}
6971

70-
func NewHealthcheck(url string) Healthcheck {
72+
func NewHealthcheck(app core.App, url string) Healthcheck {
7173
return Healthcheck{
72-
URL: url,
74+
app: app,
75+
url: url,
7376
}
7477
}
7578

@@ -78,14 +81,16 @@ func (Healthcheck) Description() string {
7881
}
7982

8083
func (r Healthcheck) Execute(ctx context.Context) {
81-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.URL, nil)
84+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.url, nil)
8285
if err != nil {
86+
r.app.Tracer(trace.SourceCron).Trace(ctx, "cron.Healthcheck", trace.WithError(err))
8387
log.Err(err).Msg("Failed create HTTP request")
8488
return
8589
}
8690

8791
res, err := http.DefaultClient.Do(req)
8892
if err != nil {
93+
r.app.Tracer(trace.SourceCron).Trace(ctx, "cron.Healthcheck", trace.WithError(err))
8994
log.Err(err).Msg("Failed send HTTP request")
9095
return
9196
}
@@ -114,6 +119,7 @@ func (DatabaseVacuum) Description() string {
114119
func (r DatabaseVacuum) Execute(ctx context.Context) {
115120
err := r.app.DatabaseVacuum(ctx)
116121
if err != nil {
122+
r.app.Tracer(trace.SourceCron).Trace(ctx, "cron.DatabaseVacuum", trace.WithError(err))
117123
log.Err(err).Msg("Failed to vacuum database")
118124
return
119125
}

cron/scheduler.go

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ func (s Scheduler) Serve(ctx context.Context) error {
2222

2323
<-ctx.Done()
2424

25+
s.StdScheduler.Wait(context.Background())
26+
2527
return nil
2628
}
2729

0 commit comments

Comments
 (0)