-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.go
277 lines (223 loc) · 7.79 KB
/
scheduler.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
chat "google.golang.org/api/chat/v1"
yaml "gopkg.in/yaml.v2"
)
// ScheduleMgr maintains a description of what the scheduler
// is be able to do
type ScheduleMgr interface {
CreateOnetime(Arguments, GroupMgr, messageResponse) string
CreateRecurring(Arguments, GroupMgr, messageResponse) string
Remove(Arguments, messageResponse) string
List(messageResponse) string
}
// ScheduleMap should be the thing that holds the information
// for all the schedules produced
type ScheduleMap map[string]*Schedule
// Schedule contains information required to schedule a
// message
type Schedule struct {
ID uint `gorm:"primary_key;not null;unique" yaml:"-"`
SessKey string `gorm:"not null" yaml:"-"` // room_id:user_id
Creator string `gorm:"not null" yaml:"creator"`
IsRecurring bool `gorm:"not null" yaml:"recurring"`
DayKey string `yaml:"-"`
CreatedOn time.Time `gorm:"not null" yaml:"createdOn"`
ExecuteOn time.Time `gorm:"not null" yaml:"sendOn"`
UpdatedOn time.Time `yaml:"updatedOn,omitempty"`
CompletedOn time.Time `yaml:"completedOn,omitempty"`
GroupID uint `gorm:"not null" yaml:"-"`
ThreadKey string `gorm:"not null" yaml:"-"`
MessageLabel string `gorm:"not null" yaml:"label"`
MessageText string `gorm:"not null" yaml:"message"`
IsFinished bool `gorm:"not null;default:false" yaml:"-"`
timer *time.Timer
}
// CreateOnetime schedules a message to be sent out once in the future
func (sm ScheduleMap) CreateOnetime(args Arguments, Groups GroupMgr, msgObj messageResponse) string {
var schedule *Schedule
schedKey := msgObj.Room.GID + ":" + args["label"]
if sm.hasSchedule(schedKey) {
schedule = sm.getSchedule(schedKey)
schedule.UpdatedOn = time.Now()
} else {
schedule = new(Schedule)
schedule.CreatedOn = time.Now()
}
schedule.SessKey = msgObj.Room.GID + ":" + msgObj.Message.Sender.GID
schedule.Creator = msgObj.Message.Sender.Name
schedule.IsRecurring = false
schedule.ExecuteOn, _ = time.Parse(time.RFC3339, args["dateTime"])
schedule.GroupID = Groups.GetGroup(args["groupName"]).Model.ID // TODO Setup relationship
schedule.ThreadKey = msgObj.Message.Thread.Name
schedule.MessageLabel = args["label"]
schedule.MessageText = args["message"]
schedule.StartTimer()
sm[schedKey] = schedule
go Logger.SaveSchedule(schedule)
return fmt.Sprintf("Scheduled onetime message %q for group %q to be sent on %q",
schedule.MessageLabel,
args["groupName"],
schedule.ExecuteOn.Format("Monday, 2 January 2006 3:04 PM MST"),
)
}
// CreateRecurring schedules a message to be sent out weekly in the future
func (sm ScheduleMap) CreateRecurring(args Arguments, Groups GroupMgr, msgObj messageResponse) string {
var schedule *Schedule
schedKey := msgObj.Room.GID + ":" + args["label"]
if sm.hasSchedule(schedKey) {
schedule = sm.getSchedule(schedKey)
schedule.UpdatedOn = time.Now()
} else {
schedule = new(Schedule)
schedule.CreatedOn = time.Now()
}
schedule.SessKey = msgObj.Room.GID + ":" + msgObj.Message.Sender.GID
schedule.Creator = msgObj.Message.Sender.Name
schedule.IsRecurring = true
schedule.ExecuteOn, _ = time.Parse(time.RFC3339, args["dateTime"])
schedule.GroupID = Groups.GetGroup(args["groupName"]).Model.ID
schedule.ThreadKey = msgObj.Message.Thread.Name
schedule.MessageLabel = args["label"]
schedule.MessageText = args["message"]
schedule.IsFinished = false
schedule.StartTimer()
sm[schedKey] = schedule
go Logger.SaveSchedule(schedule)
return fmt.Sprintf("Scheduled recurring message %q for group %q to be sent on %s.\n",
schedule.MessageLabel,
args["groupName"],
fmt.Sprintf(
"%s's @ %s, starting on %s",
schedule.ExecuteOn.Weekday(),
schedule.ExecuteOn.Format(time.Kitchen),
schedule.ExecuteOn.Format("Monday, January 2, 2006"),
),
)
}
// List returns a yaml list of the schedules specific to the
// room the request came from
//
// TODO: Fix how timestamps display when marhsalling to yaml.
// Currently, it looks like there is no way to change how a
// time.Time object displayes when using the go-yaml/yaml lib.
// Likely the solution would be to choose a different library.
func (sm ScheduleMap) List(msgObj messageResponse) string {
curRoomID := msgObj.Room.GID
var schedList string
for schedKey, schedule := range sm {
schedRoomID := strings.Split(schedKey, ":")[0]
if !schedule.IsFinished && schedRoomID == curRoomID {
data, err := yaml.Marshal(schedule)
checkError(err)
schedList += "\n" + string(data)
}
}
if schedList == "" {
return fmt.Sprintf("There are upcoming scheduled messages setup for this room as of yet.")
}
return fmt.Sprintf("Here are a list of the scheduled messages for this room ```%s```", schedList)
}
// Remove marks the specified schedule as finished, saves these changes to
// the database, then deletes the key for the schedule.
func (sm ScheduleMap) Remove(args Arguments, msgObj messageResponse) string {
label := args["label"]
roomID := msgObj.Room.GID
schedKey := roomID + ":" + label
if sm.hasSchedule(schedKey) {
schedule := sm.getSchedule(schedKey)
schedule.timer.Stop()
schedule.IsFinished = true
Logger.SaveSchedule(schedule)
delete(sm, schedKey)
return fmt.Sprintf("Removed %q from scheduler.", label)
}
return fmt.Sprintf("Message %q not found to be removed.", label)
}
// GetLabels returns a list of the rooms schedules have been created for
func (sm ScheduleMap) GetLabels() []string {
var labels []string
for label := range sm {
labels = append(labels, label)
}
return labels
}
// GetSchedule returns the schedule for the given label
func (sm ScheduleMap) getSchedule(schedKey string) *Schedule {
return sm[schedKey]
}
// HasSchedule checks to see if the schedule being called
// already exists
func (sm ScheduleMap) hasSchedule(schedKey string) bool {
_, exists := sm[schedKey]
return exists
}
// StartTimer begins the countdown until the message is sent or sends
// immediately if message is overdue
func (s *Schedule) StartTimer() {
go func() {
if s.timer != nil {
s.timer.Stop()
}
if !s.IsFinished && time.Now().After(s.ExecuteOn) {
s.Send()
return
}
s.timer = time.AfterFunc(
time.Until(s.ExecuteOn),
func() { s.Send() },
)
}()
}
// Send will send out the message scheduled
func (s *Schedule) Send() {
if os.Getenv("SERVICE_SEND") != "true" {
log.Printf("Skipping send for schedule %d... to send set USE_CHAT_SERVICE to true", s.ID)
s.complete()
return
}
// As of right now, because the logic to generate the "Notify"
// message soley lives within a method for GroupMap and requires
// a messageResponse object to create, to get the message I have
// to make an instance of GroupMap and messageResponse to retrieve
// the message. Even for MVP this is grody, but a baby's gotta do
// what a baby's gotta do.
groups := make(GroupMap)
group := Logger.GetGroupByID(s.GroupID)
groups[group.Name] = group
msgObj := messageResponse{}
// Mimicking how the message would normally look
msgObj.Message.Text = BotName + " " + group.Name + " " + s.MessageText
msgObj.Message.Sender.Name = s.Creator
msg := groups.Notify(group.Name, msgObj)
chatService := getChatService(getChatClient())
msgService := chat.NewSpacesMessagesService(chatService)
room := strings.Split(s.SessKey, ":")[0]
_, err := msgService.Create(room, &chat.Message{
Text: msg,
Thread: &chat.Thread{
Name: s.ThreadKey,
},
}).Do()
if err != nil {
log.Fatal("Error sending scheduled notification: " + err.Error())
}
s.complete()
}
func (s *Schedule) complete() {
s.CompletedOn = time.Now()
if s.IsRecurring {
// Since the scheduled messages are all weekly, once they've
// completed a run, add 7 days (168 hours)
s.ExecuteOn = s.ExecuteOn.Add(time.Hour * 168)
s.StartTimer()
} else {
s.IsFinished = true
}
Logger.SaveSchedule(s)
}