Skip to content

Commit

Permalink
change to using an interval type, hopefully
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmcorvidae committed Sep 4, 2024
1 parent 17ad691 commit 168d68e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion analyses.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ SELECT jobs.id,
LEFT join notif_statuses ON jobs.id = notif_statuses.analysis_id
WHERE jobs.status = $1
AND (notif_statuses.last_periodic_warning is null
OR notif_statuses.last_periodic_warning < now() - (coalesce(notif_statuses.periodic_warning_period, 14400)::text || ' seconds'::text)::interval)
OR notif_statuses.last_periodic_warning < now() - coalesce(notif_statuses.periodic_warning_period, '4 hours'::interval))
`

// JobPeriodicWarnings returns a list of running jobs that may need periodic notifications to be sent
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/cyverse-de/messaging/v9 v9.1.5
github.com/lib/pq v1.10.4
github.com/pkg/errors v0.9.1
github.com/sanyokbig/pqinterval v1.1.2
github.com/sirupsen/logrus v1.4.2
github.com/spf13/viper v1.4.0
github.com/streadway/amqp v1.0.1-0.20200716223359-e6b33f460591
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/sanyokbig/pqinterval v1.1.2 h1:RzHMPdRMNvSZSDE+Qr20fFWSfBkKPFrLdFhzqmF0VnY=
github.com/sanyokbig/pqinterval v1.1.2/go.mod h1:jJvMjZaZFVqNTNVCd90zcFOkmbJgjxlWWkpu9/VeUFs=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
Expand All @@ -126,6 +128,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
11 changes: 6 additions & 5 deletions vicedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"time"

pqinterval "github.com/sanyokbig/pqinterval"
log "github.com/sirupsen/logrus"
)

Expand All @@ -24,7 +25,7 @@ type NotifStatuses struct {
KillWarningSent bool
KillWarningFailureCount int
LastPeriodicWarning time.Time
PeriodicWarningPeriod int
PeriodicWarningPeriod time.Duration
}

const notifStatusQuery = `
Expand All @@ -37,7 +38,7 @@ const notifStatusQuery = `
kill_warning_sent,
kill_warning_failure_count,
coalesce(last_periodic_warning, '1970-01-01 00:00:00') as last_periodic_warning,
coalesce(periodic_warning_period, 0) as periodic_warning_period
coalesce(periodic_warning_period, '0 seconds'::interval) as periodic_warning_period
from notif_statuses
where analysis_id = $1
`
Expand Down Expand Up @@ -65,7 +66,7 @@ func (v *VICEDatabaser) NotifStatuses(ctx context.Context, job *Job) (*NotifStat
&notifStatuses.KillWarningSent,
&notifStatuses.KillWarningFailureCount,
&notifStatuses.LastPeriodicWarning,
&notifStatuses.PeriodicWarningPeriod,
(*pqinterval.Duration)(&notifStatuses.PeriodicWarningPeriod),
); err != nil {
return nil, err
}
Expand Down Expand Up @@ -100,7 +101,7 @@ func (v *VICEDatabaser) AnalysisRecordExists(ctx context.Context, analysisID str
}

const addNotifRecordQuery = `
insert into notif_statuses (analysis_id, external_id, periodic_warning_period) values ($1, $2, $3) returning id
insert into notif_statuses (analysis_id, external_id, periodic_warning_period) values ($1, $2, cast($3 as interval)) returning id
`

// AddNotifRecord adds a new record to the notif_statuses table for the provided analysis.
Expand All @@ -116,7 +117,7 @@ func (v *VICEDatabaser) AddNotifRecord(ctx context.Context, job *Job) (string, e
addNotifRecordQuery,
job.ID,
job.ExternalID,
14400, // 4 hours
"4 hours", // hardcoded for now
).Scan(&notifID); err != nil {
return "", err
}
Expand Down

0 comments on commit 168d68e

Please sign in to comment.