Skip to content

Commit

Permalink
Merge pull request #14 from ianmcorvidae/periodic-notifications
Browse files Browse the repository at this point in the history
Periodic notification preferences
  • Loading branch information
ianmcorvidae authored Sep 26, 2024
2 parents d10cf30 + b5cdab7 commit eb4fe78
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
24 changes: 21 additions & 3 deletions analyses.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type Job struct {
Type string `json:"type"`
User string `json:"user"`
ExternalID string `json:"external_id"`
NotifyPeriodic bool `json:"notify_periodic"`
PeriodicPeriod int `json:"periodic_period"`
}

// getJobDuration takes a job and returns a duration string and the start time of the job
Expand Down Expand Up @@ -67,7 +69,9 @@ select jobs.id,
jobs.planned_end_date,
jobs.start_date,
job_types.system_id,
users.username
users.username,
COALESCE((jobs.submission->>'notify_periodic')::bool, TRUE) AS notify_periodic,
COALESCE((jobs.submission->>'periodic_period')::int, 0) AS periodic_period
from jobs
join job_types on jobs.job_type_id = job_types.id
join users on jobs.user_id = users.id
Expand Down Expand Up @@ -140,6 +144,8 @@ func JobsToKill(ctx context.Context, dedb *sql.DB) ([]Job, error) {
&startDate,
&job.Type,
&job.User,
&job.NotifyPeriodic,
&job.PeriodicPeriod,
); err != nil {
return nil, err
}
Expand Down Expand Up @@ -178,7 +184,9 @@ SELECT jobs.id,
jobs.planned_end_date,
jobs.start_date,
job_types.system_id,
users.username
users.username,
COALESCE((jobs.submission->>'notify_periodic')::bool, TRUE) AS notify_periodic,
COALESCE((jobs.submission->>'periodic_period')::int, 0) AS periodic_period
FROM jobs
JOIN job_types on jobs.job_type_id = job_types.id
JOIN users on jobs.user_id = users.id
Expand Down Expand Up @@ -228,6 +236,8 @@ func JobPeriodicWarnings(ctx context.Context, dedb *sql.DB) ([]Job, error) {
&startDate,
&job.Type,
&job.User,
&job.NotifyPeriodic,
&job.PeriodicPeriod,
); err != nil {
return nil, err
}
Expand Down Expand Up @@ -266,7 +276,9 @@ select jobs.id,
jobs.planned_end_date,
jobs.start_date,
job_types.system_id,
users.username
users.username,
COALESCE((jobs.submission->>'notify_periodic')::bool, TRUE) AS notify_periodic,
COALESCE((jobs.submission->>'periodic_period')::int, 0) AS periodic_period
from jobs
join job_types on jobs.job_type_id = job_types.id
join users on jobs.user_id = users.id
Expand Down Expand Up @@ -323,6 +335,8 @@ func JobKillWarnings(ctx context.Context, dedb *sql.DB, minutes int64) ([]Job, e
&startDate,
&job.Type,
&job.User,
&job.NotifyPeriodic,
&job.PeriodicPeriod,
); err != nil {
return nil, err
}
Expand Down Expand Up @@ -473,6 +487,8 @@ select jobs.id,
jobs.start_date,
job_types.system_id,
users.username,
COALESCE((jobs.submission->>'notify_periodic')::bool, TRUE) AS notify_periodic,
COALESCE((jobs.submission->>'periodic_period')::int, 0) AS periodic_period,
job_steps.external_id
from jobs
join job_types on jobs.job_type_id = job_types.id
Expand Down Expand Up @@ -504,6 +520,8 @@ func lookupByExternalID(ctx context.Context, dedb *sql.DB, externalID string) (*
&startDate,
&job.Type,
&job.User,
&job.NotifyPeriodic,
&job.PeriodicPeriod,
&job.ExternalID,
); err != nil {
return nil, err
Expand Down
14 changes: 8 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ iplant_groups:
const warningSentKey = "warningsent"
const oneDayWarningKey = "onedaywarning"

func sendNotif(ctx context.Context, j *Job, status, subject, msg, email_template string) error {
func sendNotif(ctx context.Context, j *Job, status, subject, msg string, email bool, email_template string) error {
var err error

// Don't send notification if things aren't configured correctly. It's
Expand Down Expand Up @@ -80,10 +80,12 @@ func sendNotif(ctx context.Context, j *Job, status, subject, msg, email_template
p.AnalysisStartDate = strconv.FormatInt(sdmillis, 10)
p.AnalysisResultsFolder = j.ResultFolder
p.RunDuration = durString
p.Email = user.Email
if email {
p.Email = user.Email
}
p.User = u

notif := NewNotification(u, subject, msg, email_template, p)
notif := NewNotification(u, subject, msg, email, email_template, p)

resp, err := notif.Send(ctx)
if err != nil {
Expand Down Expand Up @@ -143,7 +145,7 @@ func SendKillNotification(ctx context.Context, j *Job, killNotifKey string) erro
endtime.UTC().Format(time.UnixDate),
j.ResultFolder,
)
err = sendNotif(ctx, j, "Canceled", subject, msg, "analysis_status_change")
err = sendNotif(ctx, j, "Canceled", subject, msg, true, "analysis_status_change")
return err
}

Expand All @@ -167,7 +169,7 @@ func SendWarningNotification(ctx context.Context, j *Job) error {
j.ResultFolder,
)

return sendNotif(ctx, j, j.Status, subject, msg, "analysis_status_change")
return sendNotif(ctx, j, j.Status, subject, msg, true, "analysis_status_change")
}

func SendPeriodicNotification(ctx context.Context, j *Job) error {
Expand All @@ -186,7 +188,7 @@ func SendPeriodicNotification(ctx context.Context, j *Job) error {
durString,
)

return sendNotif(ctx, j, j.Status, subject, msg, "analysis_periodic_notification")
return sendNotif(ctx, j, j.Status, subject, msg, j.NotifyPeriodic, "analysis_periodic_notification")
}

func ensureNotifRecord(ctx context.Context, vicedb *VICEDatabaser, job Job) error {
Expand Down
4 changes: 2 additions & 2 deletions notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ func NewPayload() *Payload {
}

// NewNotification returns a newly initialized *Notification.
func NewNotification(user, subject, msg, emailTemplate string, payload *Payload) *Notification {
func NewNotification(user, subject, msg string, email bool, emailTemplate string, payload *Payload) *Notification {
return &Notification{
URI: NotifsURI,
Type: "analysis",
User: user,
Subject: subject,
Message: msg,
Email: true,
Email: email,
EmailTemplate: emailTemplate,
Payload: payload,
}
Expand Down
4 changes: 2 additions & 2 deletions notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestNewNotification(t *testing.T) {
expectedSubject := "subject"
expectedTemplate := "analysis_status_change"
NotifsInit(expectedURI)
n := NewNotification(expectedUser, expectedSubject, "", expectedTemplate, nil)
n := NewNotification(expectedUser, expectedSubject, "", true, expectedTemplate, nil)
if n.URI != expectedURI {
t.Errorf("URI was %s, not %s", n.URI, expectedURI)
}
Expand All @@ -42,7 +42,7 @@ func TestNewNotification(t *testing.T) {
func TestSend(t *testing.T) {
expectedUser := "test-user"
expectedSubject := "test-subject"
n := NewNotification(expectedUser, expectedSubject, "", "analysis_status_change", nil)
n := NewNotification(expectedUser, expectedSubject, "", true, "analysis_status_change", nil)

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
Expand Down
10 changes: 9 additions & 1 deletion vicedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"database/sql"
"fmt"
"time"

pqinterval "github.com/sanyokbig/pqinterval"
Expand Down Expand Up @@ -110,14 +111,21 @@ func (v *VICEDatabaser) AddNotifRecord(ctx context.Context, job *Job) (string, e
var (
err error
notifID string
period string
)

if job.PeriodicPeriod > 0 {
period = fmt.Sprintf("%d seconds", job.PeriodicPeriod)
} else {
period = "4 hours"
}

if err = v.db.QueryRowContext(
ctx,
addNotifRecordQuery,
job.ID,
job.ExternalID,
"4 hours", // hardcoded for now
period,
).Scan(&notifID); err != nil {
return "", err
}
Expand Down

0 comments on commit eb4fe78

Please sign in to comment.