Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

feature: track job success/failed run time

goenningbc9d43c53c36
diff --git a/app/jobs/email_supression_job.go b/app/jobs/email_supression_job.go
index e58a6d7a3..5040fc21b 100644
--- a/app/jobs/email_supression_job.go
+++ b/app/jobs/email_supression_job.go
@@ -1,7 +1,6 @@
package jobs
import (
- "context"
"time"
"github.com/getfider/fider/app/models/cmd"
@@ -19,11 +18,15 @@ func (e EmailSupressionJobHandler) Schedule() string {
return "0 5 * * * *" // every hour at minute 5
}
-func (e EmailSupressionJobHandler) Run(ctx context.Context) error {
- twoDaysAgo := time.Now().AddDate(0, 0, -2)
+func (e EmailSupressionJobHandler) Run(ctx Context) error {
+ startTime := ctx.LastSuccessfulRun
+ if startTime == nil {
+ twoDaysAgo := time.Now().AddDate(0, 0, -2)
+ startTime = &twoDaysAgo
+ }
q := &query.FetchRecentSupressions{
- StartTime: twoDaysAgo,
+ StartTime: *startTime,
}
if err := bus.Dispatch(ctx, q); err != nil {
diff --git a/app/jobs/job.go b/app/jobs/job.go
index 53c50e702..cf5d3823d 100644
--- a/app/jobs/job.go
+++ b/app/jobs/job.go
@@ -13,7 +13,7 @@ import (
)
type Handler interface {
- Run(ctx context.Context) error
+ Run(ctx Context) error
Schedule() string
}
@@ -32,28 +32,23 @@ func NewJob(ctx context.Context, name string, handler Handler) (string, fiderJob
}
func (j fiderJob) Run() {
- ctx := context.Background()
- ctx = log.WithProperties(ctx, dto.Props{
- log.PropertyKeyContextID: rand.String(32),
- log.PropertyKeyTag: "JOBS",
- })
-
- trx, err := dbx.BeginTx(ctx)
+ ctx, trx, err := newJobContext()
if err != nil {
- log.Error(ctx, errors.Wrap(err, "failed to open transaction"))
+ log.Error(ctx, err)
return
}
- ctx = context.WithValue(ctx, app.TransactionCtxKey, trx)
+ start := time.Now()
+ ctx.LastSuccessfulRun = getLastSuccessfulRun(ctx, j.Name)
defer func() {
if r := recover(); r != nil {
log.Error(ctx, errors.Panicked(r))
+ setLastFailedRun(j.Name, start)
trx.MustRollback()
}
}()
- start := time.Now()
log.Debugf(ctx, "Job '@{JobName}' started", dto.Props{
"JobName": j.Name,
})
@@ -84,8 +79,27 @@ func (j fiderJob) Run() {
if err := j.Handler.Run(ctx); err != nil {
log.Error(ctx, err)
+ setLastFailedRun(j.Name, start)
trx.MustRollback()
} else {
+ setLastSuccessfulRun(j.Name, start)
trx.MustCommit()
}
}
+
+func newJobContext() (Context, *dbx.Trx, error) {
+ ctx := context.Background()
+ ctx = log.WithProperties(ctx, dto.Props{
+ log.PropertyKeyContextID: rand.String(32),
+ log.PropertyKeyTag: "JOBS",
+ })
+
+ trx, err := dbx.BeginTx(ctx)
+ if err != nil {
+ log.Error(ctx, err)
+ return Context{Context: ctx}, nil, err
+ }
+
+ ctx = context.WithValue(ctx, app.TransactionCtxKey, trx)
+ return Context{Context: ctx}, trx, nil
+}
diff --git a/app/jobs/job_settings.go b/app/jobs/job_settings.go
new file mode 100644
index 000000000..f5219b148
--- /dev/null
+++ b/app/jobs/job_settings.go
@@ -0,0 +1,63 @@
+package jobs
+
+import (
+ "context"
+ "fmt"
+ "time"
+
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/query"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/log"
+)
+
+type Context struct {
+ context.Context
+ LastSuccessfulRun *time.Time
+}
+
+func getLastSuccessfulRun(ctx context.Context, jobName string) *time.Time {
+ key := fmt.Sprintf("jobs.%s.last_successful_run", jobName)
+ get := &query.GetSystemSettings{
+ Key: key,
+ }
+ if err := bus.Dispatch(ctx, get); err != nil {
+ log.Error(ctx, err)
+ }
+
+ if get.Value != "" {
+ t, err := time.Parse(time.RFC3339, get.Value)
+ if err != nil {
+ log.Error(ctx, err)
+ return nil
+ }
+ return &t
+ }
+ return nil
+}
+
+func setLastSuccessfulRun(jobName string, t time.Time) {
+ key := fmt.Sprintf("jobs.%s.last_successful_run", jobName)
+ setLastRun(key, t)
+}
+
+func setLastFailedRun(jobName string, t time.Time) {
+ key := fmt.Sprintf("jobs.%s.last_failed_run", jobName)
+ setLastRun(key, t)
+}
+
+func setLastRun(key string, t time.Time) {
+ ctx, trx, err := newJobContext()
+ if err != nil {
+ log.Error(ctx, err)
+ return
+ }
+ defer trx.MustCommit()
+
+ if err = bus.Dispatch(ctx, &cmd.SetSystemSettings{
+ Key: key,
+ Value: t.Format(time.RFC3339),
+ }); err != nil {
+ log.Error(ctx, err)
+ }
+}
diff --git a/app/jobs/purge_notifications_job.go b/app/jobs/purge_notifications_job.go
index 009e39338..098d52669 100644
--- a/app/jobs/purge_notifications_job.go
+++ b/app/jobs/purge_notifications_job.go
@@ -1,8 +1,6 @@
package jobs
import (
- "context"
-
"github.com/getfider/fider/app/models/cmd"
"github.com/getfider/fider/app/pkg/bus"
)
@@ -14,6 +12,6 @@ func (e PurgeExpiredNotificationsJobHandler) Schedule() string {
return "0 0 * * * *" // every hour at minute 0
}
-func (e PurgeExpiredNotificationsJobHandler) Run(ctx context.Context) error {
+func (e PurgeExpiredNotificationsJobHandler) Run(ctx Context) error {
return bus.Dispatch(ctx, &cmd.PurgeExpiredNotifications{})
}
diff --git a/app/models/cmd/system.go b/app/models/cmd/system.go
new file mode 100644
index 000000000..5937df64c
--- /dev/null
+++ b/app/models/cmd/system.go
@@ -0,0 +1,6 @@
+package cmd
+
+type SetSystemSettings struct {
+ Key string
+ Value string
+}
diff --git a/app/models/query/system.go b/app/models/query/system.go
new file mode 100644
index 000000000..eca2ff4a6
--- /dev/null
+++ b/app/models/query/system.go
@@ -0,0 +1,8 @@
+package query
+
+type GetSystemSettings struct {
+ Key string
+
+ // Output
+ Value string
+}
diff --git a/app/services/sqlstore/postgres/postgres.go b/app/services/sqlstore/postgres/postgres.go
index 016856f36..84fad1157 100644
--- a/app/services/sqlstore/postgres/postgres.go
+++ b/app/services/sqlstore/postgres/postgres.go
@@ -129,6 +129,9 @@ func (s Service) Init() {
bus.AddHandler(getBillingState)
bus.AddHandler(activateBillingSubscription)
bus.AddHandler(cancelBillingSubscription)
+
+ bus.AddHandler(setSystemSettings)
+ bus.AddHandler(getSystemSettings)
}
type SqlHandler func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error
diff --git a/app/services/sqlstore/postgres/system.go b/app/services/sqlstore/postgres/system.go
new file mode 100644
index 000000000..6e5d90faf
--- /dev/null
+++ b/app/services/sqlstore/postgres/system.go
@@ -0,0 +1,40 @@
+package postgres
+
+import (
+ "context"
+
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/entity"
+ "github.com/getfider/fider/app/models/query"
+ "github.com/getfider/fider/app/pkg/dbx"
+ "github.com/getfider/fider/app/pkg/errors"
+)
+
+func getSystemSettings(ctx context.Context, q *query.GetSystemSettings) error {
+ return using(ctx, func(trx *dbx.Trx, _ *entity.Tenant, _ *entity.User) error {
+
+ var value string
+ err := trx.Scalar(&value, `SELECT value FROM system_settings WHERE key = $1`, q.Key)
+ if err != nil {
+ return errors.Wrap(err, "failed to query system_settings with key '%s'", q.Key)
+ }
+
+ q.Value = value
+ return nil
+ })
+}
+
+func setSystemSettings(ctx context.Context, c *cmd.SetSystemSettings) error {
+ return using(ctx, func(trx *dbx.Trx, _ *entity.Tenant, _ *entity.User) error {
+
+ _, err := trx.Execute(`
+ INSERT INTO system_settings (key, value) VALUES ($1, $2)
+ ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value;
+ `, c.Key, c.Value)
+ if err != nil {
+ return errors.Wrap(err, "failed to update system_settings with key '%s'", c.Key)
+ }
+
+ return nil
+ })
+}
diff --git a/migrations/202109272130_system_settings.sql b/migrations/202109272130_system_settings.sql
new file mode 100644
index 000000000..90f51f6eb
--- /dev/null
+++ b/migrations/202109272130_system_settings.sql
@@ -0,0 +1,4 @@
+create table if not exists system_settings (
+ key varchar(100) primary key,
+ value text not null
+);
\ No newline at end of file