Open platform to collect and prioritize feedback
refactor: improve job executor
goenningc3c4c75ffc28
diff --git a/app/jobs/job.go b/app/jobs/job.goindex 3be88586d..0871c7927 100644--- a/app/jobs/job.go+++ b/app/jobs/job.go@@ -7,11 +7,13 @@ import ("github.com/getfider/fider/app/models/dto""github.com/getfider/fider/app/pkg/dbx""github.com/getfider/fider/app/pkg/env"+ "github.com/getfider/fider/app/pkg/errors""github.com/getfider/fider/app/pkg/log"+ "github.com/getfider/fider/app/pkg/rand")type Handler interface {- Run(ctx context.Context)+ Run(ctx context.Context) errorSchedule() string}@@ -34,7 +36,17 @@ func NewJob(ctx context.Context, name string, handler Handler) (string, fiderJob}func (j fiderJob) Run() {- ctx := log.WithProperty(context.Background(), log.PropertyKeyTag, "JOBS")+ ctx := context.Background()+ ctx = log.WithProperties(ctx, dto.Props{+ log.PropertyKeyContextID: rand.String(32),+ log.PropertyKeyTag: "JOBS",+ })++ defer func() {+ if r := recover(); r != nil {+ log.Error(ctx, errors.Panicked(r))+ }+ }()start := time.Now()log.Debugf(ctx, "Job '@{JobName}' started", dto.Props{@@ -49,14 +61,22 @@ func (j fiderJob) Run() {return}- defer unlock()defer func() {elapsedMs := time.Since(start).Nanoseconds() / int64(time.Millisecond)log.Debugf(ctx, "Job '@{JobName}' finished in @{ElapsedMs:magenta}ms", dto.Props{"ElapsedMs": elapsedMs,"JobName": j.Name,})++ // Jobs should take at least 1sec before unlocking to avoid double execution+ if elapsedMs <= 1000 {+ waitMs := time.Duration(1000 - elapsedMs)+ time.Sleep(waitMs * time.Millisecond)+ }+ unlock()}()- j.Handler.Run(ctx)+ if err := j.Handler.Run(ctx); err != nil {+ log.Error(ctx, err)+ }}diff --git a/app/jobs/purge_notifications_job.go b/app/jobs/purge_notifications_job.goindex b0ed108da..009e39338 100644--- a/app/jobs/purge_notifications_job.go+++ b/app/jobs/purge_notifications_job.go@@ -11,9 +11,9 @@ type PurgeExpiredNotificationsJobHandler struct {}func (e PurgeExpiredNotificationsJobHandler) Schedule() string {- return "0 0 * * * *" // every hour+ return "0 0 * * * *" // every hour at minute 0}-func (e PurgeExpiredNotificationsJobHandler) Run(ctx context.Context) {- bus.Publish(ctx, &cmd.PurgeExpiredNotifications{})+func (e PurgeExpiredNotificationsJobHandler) Run(ctx context.Context) error {+ return bus.Dispatch(ctx, &cmd.PurgeExpiredNotifications{})}diff --git a/app/services/sqlstore/postgres/postgres.go b/app/services/sqlstore/postgres/postgres.goindex 47b84d4cb..7d4095087 100644--- a/app/services/sqlstore/postgres/postgres.go+++ b/app/services/sqlstore/postgres/postgres.go@@ -31,7 +31,7 @@ func (s Service) Enabled() bool {func (s Service) Init() {bus.AddHandler(storeEvent)- bus.AddListener(purgeExpiredNotifications)+ bus.AddHandler(purgeExpiredNotifications)bus.AddHandler(markAllNotificationsAsRead)bus.AddHandler(markNotificationAsRead)