GoBlog/postsScheduler.go

46 lines
939 B
Go
Raw Permalink Normal View History

2021-12-11 18:43:40 +00:00
package main
import (
"time"
)
func (a *goBlog) startPostsScheduler() {
ticker := time.NewTicker(30 * time.Second)
done := make(chan struct{})
2021-12-11 18:43:40 +00:00
go func() {
for {
select {
case <-done:
return
case <-ticker.C:
a.checkScheduledPosts()
}
}
}()
a.shutdown.Add(func() {
ticker.Stop()
done <- struct{}{}
2023-12-27 10:37:58 +00:00
a.info("Posts scheduler stopped")
2021-12-11 18:43:40 +00:00
})
}
func (a *goBlog) checkScheduledPosts() {
postsToPublish, err := a.getPosts(&postsRequestConfig{
status: []postStatus{statusScheduled},
2021-12-11 18:43:40 +00:00
publishedBefore: time.Now(),
})
if err != nil {
2023-12-27 10:37:58 +00:00
a.error("Error getting scheduled posts", "err", err)
2021-12-11 18:43:40 +00:00
return
}
for _, post := range postsToPublish {
post.Status = statusPublished
err := a.replacePost(post, post.Path, statusScheduled, post.Visibility)
2021-12-11 18:43:40 +00:00
if err != nil {
2023-12-27 10:37:58 +00:00
a.error("Error publishing scheduled post", "err", err)
2021-12-11 18:43:40 +00:00
continue
}
2023-12-27 10:37:58 +00:00
a.info("Published scheduled post", "path", post.Path)
2021-12-11 18:43:40 +00:00
}
}