-
Notifications
You must be signed in to change notification settings - Fork 16
/
tickloop.go
299 lines (265 loc) · 6.49 KB
/
tickloop.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"bytes"
"database/sql"
"log"
"net/http/httptest"
"strconv"
"time"
c "github.com/Azareal/Gosora/common"
"github.com/Azareal/Gosora/routes"
"github.com/Azareal/Gosora/uutils"
"github.com/pkg/errors"
)
var TickLoop *c.TickLoop
func runHook(name string) error {
if e := c.RunTaskHook(name); e != nil {
return errors.Wrap(e, "Failed at task '"+name+"'")
}
return nil
}
func deferredDailies() error {
lastDailyStr, e := c.Meta.Get("lastDaily")
// TODO: Report this error back correctly...
if e != nil && e != sql.ErrNoRows {
return e
}
lastDaily, _ := strconv.ParseInt(lastDailyStr, 10, 64)
low := time.Now().Unix() - (60 * 60 * 24)
if lastDaily < low {
if e := c.Dailies(); e != nil {
return e
}
}
return nil
}
func handleLogLongTick(name string, cn int64, secs int) {
if !c.Dev.LogLongTick {
return
}
dur := time.Duration(uutils.Nanotime() - cn)
if dur.Seconds() > float64(secs) {
log.Print("tick " + name + " completed in " + dur.String())
}
}
func tickLoop(thumbChan chan bool) error {
tl := c.NewTickLoop()
TickLoop = tl
if e := deferredDailies(); e != nil {
return e
}
if e := c.StartupTasks(); e != nil {
return e
}
startTick := func(ch chan bool) (ret bool) {
if c.Dev.HourDBTimeout {
go func() {
defer c.EatPanics()
ch <- c.StartTick()
}()
return <-ch
}
return c.StartTick()
}
tick := func(name string, tasks c.TaskSet, secs int) error {
tw := c.NewTickWatch()
tw.Name = name
tw.Set(&tw.Start, uutils.Nanotime())
tw.Run()
defer tw.Stop()
ch := make(chan bool)
tw.OutEndChan = ch
if startTick(ch) {
return nil
}
tw.Set(&tw.DBCheck, uutils.Nanotime())
if e := runHook("before_" + name + "_tick"); e != nil {
return e
}
cn := uutils.Nanotime()
tw.Set(&tw.StartHook, cn)
if e := tasks.Run(); e != nil {
return e
}
tw.Set(&tw.Tasks, uutils.Nanotime())
handleLogLongTick(name, cn, secs)
if e := runHook("after_" + name + "_tick"); e != nil {
return e
}
tw.Set(&tw.EndHook, uutils.Nanotime())
//close(tw.OutEndChan)
return nil
}
tl.HalfSecf = func() error {
return tick("half_second", c.Tasks.HalfSec, 2)
}
// TODO: Automatically lock topics, if they're really old, and the associated setting is enabled.
// TODO: Publish scheduled posts.
tl.FifteenMinf = func() error {
return tick("fifteen_minute", c.Tasks.FifteenMin, 5)
}
// TODO: Handle the instance going down a lot better
// TODO: Handle the daily clean-up.
tl.Dayf = func() error {
if c.StartTick() {
return nil
}
cn := uutils.Nanotime()
if e := c.Dailies(); e != nil {
return e
}
handleLogLongTick("day", cn, 5)
return nil
}
tl.Secf = func() (e error) {
if c.StartTick() {
return nil
}
if e = runHook("before_second_tick"); e != nil {
return e
}
cn := uutils.Nanotime()
go func() {
defer c.EatPanics()
thumbChan <- true
}()
if e = c.Tasks.Sec.Run(); e != nil {
return e
}
// TODO: Stop hard-coding this
if e = c.HandleExpiredScheduledGroups(); e != nil {
return e
}
// TODO: Handle delayed moderation tasks
// Sync with the database, if there are any changes
if e = c.HandleServerSync(); e != nil {
return e
}
handleLogLongTick("second", cn, 3)
// TODO: Manage the TopicStore, UserStore, and ForumStore
// TODO: Alert the admin, if CPU usage, RAM usage, or the number of posts in the past second are too high
// TODO: Clean-up alerts with no unread matches which are over two weeks old. Move this to a 24 hour task?
// TODO: Rescan the static files for changes
return runHook("after_second_tick")
}
tl.Hourf = func() error {
if c.StartTick() {
return nil
}
if e := runHook("before_hour_tick"); e != nil {
return e
}
cn := uutils.Nanotime()
jsToken, e := c.GenerateSafeString(80)
if e != nil {
return e
}
c.JSTokenBox.Store(jsToken)
c.OldSessionSigningKeyBox.Store(c.SessionSigningKeyBox.Load().(string)) // TODO: We probably don't need this type conversion
sessionSigningKey, e := c.GenerateSafeString(80)
if e != nil {
return e
}
c.SessionSigningKeyBox.Store(sessionSigningKey)
if e = c.Tasks.Hour.Run(); e != nil {
return e
}
if e = PingLastTopicTick(); e != nil {
return e
}
handleLogLongTick("hour", cn, 5)
return runHook("after_hour_tick")
}
c.CTickLoop = tl
return nil
}
func sched() error {
ws := errors.WithStack
schedStr, err := c.Meta.Get("sched")
// TODO: Report this error back correctly...
if err != nil && err != sql.ErrNoRows {
return ws(err)
}
if schedStr == "recalc" {
log.Print("Cleaning up orphaned data.")
count, err := c.Recalc.Replies()
if err != nil {
return ws(err)
}
log.Printf("Deleted %d orphaned replies.", count)
count, err = c.Recalc.Forums()
if err != nil {
return ws(err)
}
log.Printf("Recalculated %d forum topic counts.", count)
count, err = c.Recalc.Subscriptions()
if err != nil {
return ws(err)
}
log.Printf("Deleted %d orphaned subscriptions.", count)
count, err = c.Recalc.ActivityStream()
if err != nil {
return ws(err)
}
log.Printf("Deleted %d orphaned activity stream items.", count)
err = c.Recalc.Users()
if err != nil {
return ws(err)
}
log.Print("Recalculated user post stats.")
count, err = c.Recalc.Attachments()
if err != nil {
return ws(err)
}
log.Printf("Deleted %d orphaned attachments.", count)
}
return nil
}
var pingLastTopicCount = 1
// TODO: Move somewhere else
func PingLastTopicTick() error {
g, e := c.Groups.Get(c.GuestUser.Group)
if e != nil {
return e
}
tList, _, _, e := c.TopicList.GetListByGroup(g, 1, 0, nil)
if e != nil {
return e
}
if len(tList) == 0 {
return nil
}
w := httptest.NewRecorder()
sid := strconv.Itoa(tList[0].ID)
req := httptest.NewRequest("get", "/topic/"+sid, bytes.NewReader(nil))
cn := uutils.Nanotime()
// Deal with the session stuff, etc.
ucpy, ok := c.PreRoute(w, req)
if !ok {
return errors.New("preroute failed")
}
head, rerr := c.UserCheck(w, req, &ucpy)
if rerr != nil {
return errors.New(rerr.Error())
}
rerr = routes.ViewTopic(w, req, &ucpy, head, sid)
if rerr != nil {
return errors.New(rerr.Error())
}
/*if w.Code != 200 {
return errors.New("topic code not 200")
}*/
dur := time.Duration(uutils.Nanotime() - cn)
if dur.Seconds() > 5 {
c.Log("topic " + sid + " completed in " + dur.String())
} else if c.Dev.Log4thLongRoute {
pingLastTopicCount++
if pingLastTopicCount == 4 {
c.Log("topic " + sid + " completed in " + dur.String())
}
if pingLastTopicCount >= 4 {
pingLastTopicCount = 1
}
}
return nil
}