2021-12-11 18:43:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"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{}{}
|
2021-12-11 18:43:40 +00:00
|
|
|
log.Println("Posts scheduler stopped")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
log.Println("Error getting scheduled posts:", err)
|
|
|
|
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 {
|
|
|
|
log.Println("Error publishing scheduled post:", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Println("Published scheduled post:", post.Path)
|
|
|
|
}
|
|
|
|
}
|