-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
131 lines (106 loc) · 2.63 KB
/
main.go
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
package main
import (
"fmt"
"log"
"strings"
"time"
"github.com/robfig/cron/v3"
)
func main() {
const specTime = "*/15 * * * *"
// everyMinute, _ := cron.ParseStandard("* * * * *")
c := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(log.Default())))
loop, _ := c.AddFunc(specTime, func() {
retryCount := 0
for ; retryCount < 3; retryCount++ {
log.Println("Check and notification")
if err := checkAndNotification(); err != nil {
log.Println("checkAndNotification error", err)
time.Sleep(time.Second * 5) // retry after 5 seconds
continue
}
log.Println(strings.Repeat("-", 70))
break
}
if retryCount >= 3 {
log.Println("Retry 3 times, skip check")
}
})
entry := c.Entry(loop)
go entry.Job.Run() // first run
// oldSchedule := entry.Schedule
// entry.Schedule = everyMinute
// entry.Next = time.Now().In(c.Location())
c.Run() // loop start
}
func checkAndNotification() error {
data, err := GetClosedSchool()
if err != nil {
log.Println("GetClosedSchool error", err)
return err
}
areaNamesMap := map[string]bool{}
notifications := []WorkSchoolCloseData{}
for _, v := range ConfigData.AreaNames {
areaNamesMap[v] = true
}
tmpData := GetTmpDate()
for county, v := range data.Data {
countyTmpData, countyExists := tmpData[county]
if !countyExists {
tmpData[county] = map[string]bool{}
}
details := []string{}
for _, detail := range v.Details {
absoluteDetail := ConvertRelativeToAbsoluteTime(detail, *data.Date)
if _, ok := countyTmpData[absoluteDetail]; ok {
continue
}
details = append(details, detail)
tmpData[county][absoluteDetail] = true
}
notifications = append(notifications, WorkSchoolCloseData{
County: county,
Details: details,
})
}
if len(notifications) > 0 {
text := ""
sendNotifications := []WorkSchoolCloseData{}
for _, v := range notifications {
if len(v.Details) == 0 {
continue
}
text += fmt.Sprintf("%s: \n %s\n", v.County, strings.Join(v.Details, "\n "))
if !areaNamesMap[v.County] {
continue
}
sendNotifications = append(sendNotifications, v)
}
if text != "" {
fmt.Println(text)
}
if len(sendNotifications) > 0 {
notification(WorkSchoolClose{Date: data.Date, Data: sendNotifications})
}
}
// clear timeout data
nowTime := time.Now()
for _, data := range tmpData {
for k := range data {
if HasStatusIsOld(k, nowTime) {
delete(data, k)
}
}
}
WriteTmpDate(tmpData)
return nil
}
func notification(notifications WorkSchoolClose) {
if ConfigData.Discord.Enable {
go NotifyDiscord(notifications)
}
if ConfigData.Line.Enable {
go NotifyLine(notifications)
}
}