telegram message send offset introduced

This commit is contained in:
milarin 2023-01-19 10:16:43 +01:00
parent 37033d1e46
commit 8d030f7386
3 changed files with 17 additions and 4 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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))
}
}