-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunc.go
More file actions
executable file
·130 lines (105 loc) · 3.26 KB
/
func.go
File metadata and controls
executable file
·130 lines (105 loc) · 3.26 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
package main
import (
"bufio"
"context"
"encoding/json"
"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 string, config Config, location *time.Location) func() {
return func() {
slog.Debug("job started")
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 {
startDate, endDate, err := d.ParseTime(location)
if err != nil {
slog.Error("Failed to parse time", "error", err)
continue
}
f, err := LoadOrCreateFile(cachePathDir, billID, d.OutageNumber, startDate)
if err != nil {
slog.Error("couldn't load or create file", "error", err)
continue
}
defer f.Close()
var fileData []byte
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fileData = append(fileData, scanner.Bytes()...)
}
if err := scanner.Err(); err != nil {
slog.Error("scanner returns error", "error", err)
continue
}
fcf := new(FileContent)
var sequence uint
if len(fileData) != 0 {
if err := json.Unmarshal(fileData, fcf); err != nil {
slog.Error("decode the file data failed", "error", err)
continue
}
// Checks that the file loaded the start and end datetime is changed or not.
// If it doesn't changes, ignore it; If it changes, update it.
if fcf.StartOutageDateTime.Equal(startDate) || fcf.EndOutageDateTime.Equal(endDate) {
slog.Info("This data is already sent as email", "file name", fcf.FileName())
continue
}
sequence = fcf.Sequence + 1
}
fcf, err = d.ToFileContent(location, billID, c.Recipients, sequence)
if err != nil {
slog.Error("Failed to convert data to file content", "error", err)
continue
}
if err := mail.Do(fcf, subject); err != nil {
slog.Error("Failed to send mail", "error", err)
continue
}
if err := fcf.Write(f); err != nil {
slog.Error("Failed to cache data", "error", err)
}
}
time.Sleep(time.Second * time.Duration(config.WaitTime))
}
}
slog.Debug("all clients sent, waiting for next cron cycle")
}
}