Open platform to collect and prioritize feedback
feature: scheduled jobs
goenning9d2b26f9a06d
diff --git a/app/cmd/server.go b/app/cmd/server.goindex 67918a376..d55c94639 100644--- a/app/cmd/server.go+++ b/app/cmd/server.go@@ -7,7 +7,7 @@ import ("path""syscall"- "github.com/getfider/fider/app/models/cmd"+ "github.com/getfider/fider/app/jobs""github.com/getfider/fider/app/models/dto""github.com/getfider/fider/app/models/query""github.com/getfider/fider/app/pkg/bus"@@ -15,6 +15,7 @@ import ("github.com/getfider/fider/app/pkg/errors""github.com/getfider/fider/app/pkg/log""github.com/getfider/fider/app/pkg/web"+ "github.com/robfig/cron"_ "github.com/getfider/fider/app/services/billing/paddle"_ "github.com/getfider/fider/app/services/blob/fs"@@ -43,15 +44,21 @@ func RunServer() int {})}- bus.Publish(ctx, &cmd.PurgeExpiredNotifications{})copyEtcFiles(ctx)+ startJobs(ctx)e := routes(web.New())-go e.Start(":" + env.Config.Port)return listenSignals(e)}+// Starts all scheduled jobs+func startJobs(ctx context.Context) {+ c := cron.New()+ _ = c.AddJob(jobs.NewJob(ctx, "PurgeExpiredNotificationsJob", jobs.PurgeExpiredNotificationsJobHandler{}))+ c.Start()+}+// on startup, copy all etc/ files from configured blob storage into local etc/ folder// this can be used to avoid having to mount volumes on ephemeral environmentsfunc copyEtcFiles(ctx context.Context) {diff --git a/app/jobs/job.go b/app/jobs/job.gonew file mode 100644index 000000000..3be88586d--- /dev/null+++ b/app/jobs/job.go@@ -0,0 +1,62 @@+package jobs++import (+ "context"+ "time"++ "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/log"+)++type Handler interface {+ Run(ctx context.Context)+ Schedule() string+}++type fiderJob struct {+ Name string+ Handler Handler+}++func NewJob(ctx context.Context, name string, handler Handler) (string, fiderJob) {+ schedule := handler.Schedule()+ if env.IsDevelopment() {+ // job runs every 1 minute during development mode+ schedule = "0 * * * * *"+ }+ log.Debugf(ctx, "Job '@{JobName}' scheduled to run '@{Schedule}'", dto.Props{+ "JobName": name,+ "Schedule": schedule,+ })+ return schedule, fiderJob{Name: name, Handler: handler}+}++func (j fiderJob) Run() {+ ctx := log.WithProperty(context.Background(), log.PropertyKeyTag, "JOBS")++ start := time.Now()+ log.Debugf(ctx, "Job '@{JobName}' started", dto.Props{+ "JobName": j.Name,+ })++ locked, unlock := dbx.TryLock(ctx, j.Name)+ if !locked {+ log.Debugf(ctx, "Job '@{JobName}' skipped, could not acquire lock", dto.Props{+ "JobName": j.Name,+ })+ 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,+ })+ }()++ j.Handler.Run(ctx)+}diff --git a/app/jobs/purge_notifications_job.go b/app/jobs/purge_notifications_job.gonew file mode 100644index 000000000..b0ed108da--- /dev/null+++ b/app/jobs/purge_notifications_job.go@@ -0,0 +1,19 @@+package jobs++import (+ "context"++ "github.com/getfider/fider/app/models/cmd"+ "github.com/getfider/fider/app/pkg/bus"+)++type PurgeExpiredNotificationsJobHandler struct {+}++func (e PurgeExpiredNotificationsJobHandler) Schedule() string {+ return "0 0 * * * *" // every hour+}++func (e PurgeExpiredNotificationsJobHandler) Run(ctx context.Context) {+ bus.Publish(ctx, &cmd.PurgeExpiredNotifications{})+}diff --git a/app/pkg/dbx/lock.go b/app/pkg/dbx/lock.gonew file mode 100644index 000000000..5fca86606--- /dev/null+++ b/app/pkg/dbx/lock.go@@ -0,0 +1,37 @@+package dbx++import (+ "context"+ "hash/fnv"++ "github.com/getfider/fider/app/pkg/errors"+ "github.com/getfider/fider/app/pkg/log"+)++func hash(s string) uint32 {+ h := fnv.New32a()+ h.Write([]byte(s))+ return h.Sum32()+}++// Try to obtain an advisory lock+// returns true and an unlock function if lock was aquired+func TryLock(ctx context.Context, key string) (bool, func()) {+ trx, err := BeginTx(ctx)+ if err != nil {+ log.Error(ctx, errors.Wrap(err, "failed to open transaction while acquiring advisory lock"))+ return false, nil+ }++ var locked bool+ if err := trx.Scalar(&locked, "SELECT pg_try_advisory_xact_lock($1)", hash(key)); err != nil {+ log.Error(ctx, errors.Wrap(err, "failed to acquire advisory lock"))+ return false, nil+ }++ unlock := func() {+ trx.MustCommit()+ }++ return locked, unlock+}diff --git a/app/pkg/dbx/lock_test.go b/app/pkg/dbx/lock_test.gonew file mode 100644index 000000000..7f371270e--- /dev/null+++ b/app/pkg/dbx/lock_test.go@@ -0,0 +1,46 @@+package dbx_test++import (+ "context"+ "testing"++ . "github.com/getfider/fider/app/pkg/assert"+ "github.com/getfider/fider/app/pkg/dbx"+)++func TestTryLock_MultipleProcesses_SameKey(t *testing.T) {+ RegisterT(t)+ ctx := context.Background()++ locked1, unlock1 := dbx.TryLock(ctx, "KEY_1")+ locked2, unlock2 := dbx.TryLock(ctx, "KEY_1")++ Expect(locked1).IsTrue()+ Expect(unlock1).IsNotNil()+ Expect(locked2).IsFalse()+ Expect(unlock2).IsNotNil()++ unlock1()++ locked2, unlock2 = dbx.TryLock(ctx, "KEY_1")+ Expect(locked2).IsTrue()+ Expect(unlock2).IsNotNil()++ unlock2()+}++func TestTryLock_MultipleProcesses_DifferentKey(t *testing.T) {+ RegisterT(t)+ ctx := context.Background()++ locked1, unlock1 := dbx.TryLock(ctx, "KEY_1")+ locked2, unlock2 := dbx.TryLock(ctx, "KEY_2")++ Expect(locked1).IsTrue()+ Expect(unlock1).IsNotNil()+ Expect(locked2).IsTrue()+ Expect(unlock2).IsNotNil()++ unlock1()+ unlock2()+}diff --git a/app/services/sqlstore/postgres/notification.go b/app/services/sqlstore/postgres/notification.goindex c489f9912..30d178175 100644--- a/app/services/sqlstore/postgres/notification.go+++ b/app/services/sqlstore/postgres/notification.go@@ -29,7 +29,7 @@ func purgeExpiredNotifications(ctx context.Context, c *cmd.PurgeExpiredNotificatreturn errors.Wrap(err, "failed to delete expired notifications")}- log.Debugf(ctx, "a total of @{RowsDeleted} notifications were deleted", dto.Props{+ log.Debugf(ctx, "@{RowsDeleted} notifications were deleted", dto.Props{"RowsDeleted": count,})diff --git a/go.mod b/go.modindex b7c38b233..518988144 100644--- a/go.mod+++ b/go.mod@@ -21,6 +21,7 @@ require (github.com/patrickmn/go-cache v2.1.0+incompatiblegithub.com/prometheus/client_golang v1.7.1github.com/prometheus/client_model v0.2.0+ github.com/robfig/cron v1.2.0github.com/russross/blackfriday v1.6.0golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985diff --git a/go.sum b/go.sumindex 26fb9daed..234c993c3 100644--- a/go.sum+++ b/go.sum@@ -585,6 +585,8 @@ github.com/quasilyte/go-ruleguard/rules v0.0.0-20201231183845-9e62ed36efe1/go.mogithub.com/quasilyte/go-ruleguard/rules v0.0.0-20210203162857-b223e0831f88/go.mod h1:4cgAphtvu7Ftv7vOT2ZOYhC6CvBxZixcasr8qIOTA50=github.com/quasilyte/regex/syntax v0.0.0-20200407221936-30656e2c4a95 h1:L8QM9bvf68pVdQ3bCFZMDmnt9yqcMBro1pC7F+IPYMY=github.com/quasilyte/regex/syntax v0.0.0-20200407221936-30656e2c4a95/go.mod h1:rlzQ04UMyJXu/aOvhd8qT+hvDrFpiwqp8MRXDY9szc0=+github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=+github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=