-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathpostsScheduler.go
45 lines (42 loc) · 945 Bytes
/
postsScheduler.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
package main
import (
"time"
)
func (a *goBlog) startPostsScheduler() {
ticker := time.NewTicker(30 * time.Second)
done := make(chan struct{})
go func() {
for {
select {
case <-done:
return
case <-ticker.C:
a.checkScheduledPosts()
}
}
}()
a.shutdown.Add(func() {
ticker.Stop()
done <- struct{}{}
a.info("Posts scheduler stopped")
})
}
func (a *goBlog) checkScheduledPosts() {
postsToPublish, err := a.getPosts(&postsRequestConfig{
status: []postStatus{statusScheduled},
publishedBefore: time.Now(),
})
if err != nil {
a.error("Error getting scheduled posts", "err", err)
return
}
for _, post := range postsToPublish {
post.Status = statusPublished
err := a.replacePost(post, post.Path, statusScheduled, post.Visibility, true)
if err != nil {
a.error("Error publishing scheduled post", "err", err)
continue
}
a.info("Published scheduled post", "path", post.Path)
}
}