2021-12-11 18:43:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (a *goBlog) startPostsScheduler() {
|
2022-03-31 13:51:17 +00:00
|
|
|
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()
|
2022-03-31 13:51:17 +00:00
|
|
|
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{
|
2022-09-23 09:05:07 +00:00
|
|
|
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 {
|
2022-06-15 12:44:12 +00:00
|
|
|
post.Status = statusPublished
|
2022-09-23 09:05:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|