Skip to content

Commit 6aa7f6c

Browse files
committed
Add env to disable success message after job run
1 parent a1b9726 commit 6aa7f6c

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func main() {
6464
}
6565

6666
c := scheduler.New()
67-
n := notify.New(env.NtfyUrl, env.NtfyTopic, env.NtfyToken)
67+
n := notify.New(env.NtfyUrl, env.NtfyTopic, env.NtfyToken, env.SendMessageOnSuccess)
6868

6969
js, err := services.NewJobService(configFolder+"db.sqlite", cfg, c, n)
7070
if err != nil {

internal/env/env.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import (
1010
)
1111

1212
type Config struct {
13-
TimeZone string `env:"TZ" envDefault:"Etc/UTC" validate:"timezone"`
14-
Port int `env:"PORT" envDefault:"8156" validate:"omitempty,numeric"`
15-
LogLevel string `env:"LOG_LEVEL" envDefault:"info" validate:"oneof=debug info warn error"`
16-
NtfyUrl string `env:"NTFY_URL" envDefault:"https://ntfy.sh/" validate:"omitempty,url,endswith=/"`
17-
NtfyTopic string `env:"NTFY_TOPIC" envDefault:"gocron"`
18-
NtfyToken string `env:"NTFY_TOKEN,unset"`
13+
TimeZone string `env:"TZ" envDefault:"Etc/UTC" validate:"timezone"`
14+
Port int `env:"PORT" envDefault:"8156" validate:"omitempty,numeric"`
15+
LogLevel string `env:"LOG_LEVEL" envDefault:"info" validate:"oneof=debug info warn error"`
16+
NtfyUrl string `env:"NTFY_URL" envDefault:"https://ntfy.sh/" validate:"omitempty,url,endswith=/"`
17+
NtfyTopic string `env:"NTFY_TOPIC" envDefault:"gocron"`
18+
NtfyToken string `env:"NTFY_TOKEN,unset"`
19+
SendMessageOnSuccess bool `env:"SEND_ON_SUCCESS" envDefault:"true" validate:"omitempty,boolean"`
1920
}
2021

2122
var errParse = errors.New("error parsing environment variables")

internal/notify/notify.go

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,59 @@ import (
88
)
99

1010
type Notifier struct {
11-
URL string
12-
Topic string
13-
Token string
11+
URL string
12+
Topic string
13+
Token string
14+
SendMessageOnSuccess bool
1415
}
1516

16-
func New(url, topic, token string) *Notifier {
17+
func New(url, topic, token string, sendMessageOnSuccess bool) *Notifier {
1718
return &Notifier{
18-
URL: url,
19-
Topic: topic,
20-
Token: token,
19+
URL: url,
20+
Topic: topic,
21+
Token: token,
22+
SendMessageOnSuccess: sendMessageOnSuccess,
2123
}
2224
}
2325

24-
func (n *Notifier) Send(title, message string, tags []string) {
26+
type priorityLevel uint8
27+
28+
const (
29+
MIN priorityLevel = iota + 1
30+
LOW
31+
DEFAULT
32+
HIGH
33+
URGENT
34+
)
35+
36+
func (p priorityLevel) String() string {
37+
switch p {
38+
case MIN:
39+
return "min"
40+
case LOW:
41+
return "low"
42+
case HIGH:
43+
return "high"
44+
case URGENT:
45+
return "urgent"
46+
default:
47+
return "default"
48+
}
49+
}
50+
51+
func (n *Notifier) Send(title, message string, priority priorityLevel, tags []string) {
52+
if !n.SendMessageOnSuccess {
53+
return
54+
}
55+
2556
req, _ := http.NewRequest("POST", n.URL+n.Topic, strings.NewReader(message))
2657
req.Header.Set("Title", title)
27-
req.Header.Set("Priority", "urgent")
58+
req.Header.Set("Priority", priority.String())
2859
req.Header.Set("Tags", strings.Join(tags, ","))
2960
if n.Token != "" {
3061
req.Header.Set("Authorization", "Bearer "+n.Token)
3162
}
63+
3264
body, err := http.DefaultClient.Do(req)
3365
if err != nil {
3466
log.Errorf("Failed to send notification (url: %s, topic: %s): %v", n.URL, n.Topic, err)
@@ -39,5 +71,5 @@ func (n *Notifier) Send(title, message string, tags []string) {
3971
log.Warnf("Failed to send notification (url: %s, topic: %s): %s", n.URL, n.Topic, body.Status)
4072
return
4173
}
42-
log.Printf("Notification sent (url: %s, topic: %s)", n.URL, n.Topic)
74+
log.Debugf("Notification sent (url: %s, topic: %s)", n.URL, n.Topic)
4375
}

services/jobs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func (js *JobService) ExecuteJobs(jobs []jobs.Job) {
229229
js.ExecuteJob(&jobs[i])
230230
names = append(names, jobs[i].Name)
231231
}
232-
js.Notify.Send("Backup finished", fmt.Sprintf("Time: %s\nJobs: %s", time.Now().Format(time.RFC1123), strings.Join(names, ", ")), []string{"tada"})
232+
js.Notify.Send("Backup finished", fmt.Sprintf("Time: %s\nJobs: %s", time.Now().Format(time.RFC1123), strings.Join(names, ", ")), notify.DEFAULT, []string{"tada"})
233233
}
234234

235235
func (js *JobService) ExecuteJob(job *jobs.Job) {
@@ -264,7 +264,7 @@ func (js *JobService) ExecuteJob(job *jobs.Job) {
264264
severity = Info
265265
if err != nil {
266266
severity = Error
267-
js.Notify.Send(fmt.Sprintf("Error - %s", job.Name), fmt.Sprintf("Command: \"%s\"\nResult: \"%s\"", cmd, out), []string{"rotating_light"})
267+
js.Notify.Send(fmt.Sprintf("Error - %s", job.Name), fmt.Sprintf("Command: \"%s\"\nResult: \"%s\"", cmd, out), notify.URGENT, []string{"rotating_light"})
268268
}
269269
if out == "" {
270270
out = "Done - No output"

0 commit comments

Comments
 (0)