-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunc.go
More file actions
executable file
·152 lines (127 loc) · 3.86 KB
/
Copy pathfunc.go
File metadata and controls
executable file
·152 lines (127 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"context"
"encoding/json"
"io"
"log/slog"
"os"
"time"
)
func DeleteCacheFunc(cachePathDir string, period time.Duration) func() {
return func() {
u := time.Now().Add(-period)
files, err := os.ReadDir(cachePathDir)
if err != nil {
slog.Error("couldn't read all directories", "error", err)
return
}
for _, f := range files {
info, err := f.Info()
if err != nil {
slog.Error("couldn't read info files", "error", err, "file name", f.Name())
continue
}
if info.ModTime().Before(u) {
filePath := cachePathDir + "/" + info.Name()
slog.Debug("removing cache", "file name", filePath)
if err := os.Remove(filePath); err != nil {
slog.Error("cannot remove the file", "error", err, "file path", filePath)
continue
}
}
}
}
}
func MailerFunc(cachePathDir, configPath string, location *time.Location) func() {
return func() {
slog.Debug("job started")
config, err := LoadConfig(configPath)
if err != nil {
slog.Error("Failed to reload config", "error", err, "path", configPath)
return
}
slog.SetLogLoggerLevel(slog.Level(config.LogLevel))
for subject, c := range config.Clients {
smtp, ok := config.SMTP[c.SMTP]
if !ok {
slog.Error("Cannot map between smtp config and client config", "smtp name", c.SMTP)
continue
}
mail := NewMailClient(smtp, location)
for _, billID := range append(c.BillIDs, c.BillID) {
data, err := PlannedBlackOut(context.Background(), config.AuthToken, billID, time.Now().AddDate(0, 0, -1), time.Now().AddDate(0, 0, 5))
if err != nil {
slog.Error("PlannedBlackOut failed", "error", err)
continue
}
for _, d := range data {
if err := processOutage(cachePathDir, location, mail, subject, billID, c.Recipients, d); err != nil {
slog.Error("Failed to process outage", "error", err, "bill_id", billID, "outage_number", d.OutageNumber)
}
}
time.Sleep(time.Second * time.Duration(config.WaitTime))
}
}
slog.Debug("all clients sent, waiting for next cron cycle")
}
}
func processOutage(cachePathDir string, location *time.Location, mail Mail, subject, billID string, recipients []string, d Data) error {
startDate, endDate, err := d.ParseTime(location)
if err != nil {
slog.Error("Failed to parse time", "error", err)
return err
}
f, err := LoadOrCreateFile(cachePathDir, billID, d.OutageNumber, startDate)
if err != nil {
slog.Error("couldn't load or create file", "error", err)
return err
}
defer f.Close()
cached, hasCache, err := readCacheContent(f)
if err != nil {
slog.Error("couldn't read cache file", "error", err)
return err
}
decision := DecideOutageSend(cached, hasCache, startDate, endDate, recipients)
if decision.Action == OutageSendSkip {
slog.Info("This data is already sent as email", "file name", FileName(billID, d.OutageNumber, startDate))
return nil
}
fcf, err := d.ToFileContent(location, billID, decision.Recipients, decision.Sequence)
if err != nil {
slog.Error("Failed to convert data to file content", "error", err)
return err
}
kind := MailKindNew
if decision.Action == OutageSendUpdate {
kind = MailKindUpdate
}
if err := mail.Do(fcf, subject, kind); err != nil {
slog.Error("Failed to send mail", "error", err)
return err
}
// Persist the full current recipient list so removals and additions stick.
fcf.Recipients = copyStrings(recipients)
if err := fcf.Write(f); err != nil {
slog.Error("Failed to cache data", "error", err)
return err
}
return nil
}
func readCacheContent(f *os.File) (*FileContent, bool, error) {
if _, err := f.Seek(0, 0); err != nil {
return nil, false, err
}
fileData, err := io.ReadAll(f)
if err != nil {
return nil, false, err
}
if len(fileData) == 0 {
return nil, false, nil
}
fcf := new(FileContent)
if err := json.Unmarshal(fileData, fcf); err != nil {
return nil, false, err
}
return fcf, true, nil
}