telegram message send offset introduced
This commit is contained in:
parent
37033d1e46
commit
8d030f7386
@ -22,6 +22,7 @@ var (
|
||||
TelegramOrganizeMessagePattern = template.Must(template.New("TELEGRAM_ORGANIZE_MESSAGE_PATTERN").Parse(TelegramOrganizeMessagePatternStr))
|
||||
TelegramOrganizeMessageSendCondition = envvars.ObjectSlice("TELEGRAM_ORGANIZE_MESSAGE_SEND_CONDITION", ",", []SendCondition{SendConditionAlways}, SendConditionFromString)
|
||||
TelegramOrganizeMessageSendInterval = envvars.Duration("TELEGRAM_ORGANIZE_MESSAGE_SEND_INTERVAL", 0)
|
||||
TelegramOrganizeMessageSendOffset = envvars.Duration("TELEGRAM_ORGANIZE_MESSAGE_SEND_OFFSET", 0)
|
||||
|
||||
Uid = envvars.Object("UID", 1000, func(s string) (int, error) {
|
||||
if uid, err := strconv.Atoi(s); err == nil {
|
||||
|
@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.milar.in/milarin/adverr"
|
||||
"git.milar.in/milarin/anilist"
|
||||
@ -33,9 +32,7 @@ func SendMessagePeriodically() {
|
||||
var messagesPerInterval <-chan []model.AnimeEpisode
|
||||
|
||||
if TelegramOrganizeMessageSendInterval > 0 {
|
||||
lastCycle := time.Now().Truncate(TelegramOrganizeMessageSendInterval)
|
||||
nextCycle := lastCycle.Add(TelegramOrganizeMessageSendInterval)
|
||||
time.Sleep(time.Until(nextCycle))
|
||||
WaitForNextTelegramSendCycle()
|
||||
sendAllQueuedAnimeEpisodes()
|
||||
|
||||
grouperFunc := func(current []model.AnimeEpisode, value model.AnimeEpisode) []model.AnimeEpisode {
|
||||
|
15
utils.go
15
utils.go
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.milar.in/milarin/channel"
|
||||
"git.milar.in/milarin/gmath"
|
||||
@ -48,3 +49,17 @@ func PrintFileHandle(fh *FileHandle) {
|
||||
panic(err) // TODO error handling
|
||||
}
|
||||
}
|
||||
|
||||
func WaitForNextTelegramSendCycle() {
|
||||
now := time.Now()
|
||||
_, offset := now.Zone()
|
||||
offsetDuration := time.Duration(offset) * time.Second
|
||||
lastCycle := now.Truncate(TelegramOrganizeMessageSendInterval).Add(-offsetDuration).Add(TelegramOrganizeMessageSendOffset)
|
||||
|
||||
if durationUntilLastCycle := time.Until(lastCycle); durationUntilLastCycle > 0 {
|
||||
time.Sleep(durationUntilLastCycle)
|
||||
} else {
|
||||
nextCycle := lastCycle.Add(TelegramOrganizeMessageSendInterval)
|
||||
time.Sleep(time.Until(nextCycle))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user