diff --git a/analyses.go b/analyses.go index fc415bc..ddddfab 100644 --- a/analyses.go +++ b/analyses.go @@ -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 @@ -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 @@ -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 } @@ -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 @@ -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 } @@ -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 @@ -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 } @@ -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 @@ -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 diff --git a/main.go b/main.go index 30dea5a..d095bcf 100644 --- a/main.go +++ b/main.go @@ -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 @@ -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 { @@ -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 } @@ -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 { @@ -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 { diff --git a/notifications.go b/notifications.go index 64fd2ad..bcd7bc6 100644 --- a/notifications.go +++ b/notifications.go @@ -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, } diff --git a/notifications_test.go b/notifications_test.go index 1de7cb4..8a5dfa9 100644 --- a/notifications_test.go +++ b/notifications_test.go @@ -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) } @@ -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) diff --git a/vicedb.go b/vicedb.go index 78404ec..fa649b5 100644 --- a/vicedb.go +++ b/vicedb.go @@ -3,6 +3,7 @@ package main import ( "context" "database/sql" + "fmt" "time" pqinterval "github.com/sanyokbig/pqinterval" @@ -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(¬ifID); err != nil { return "", err }