asses.Next returns midnight if outside working hours

main
bel 2025-05-17 21:17:26 -06:00
parent be148f5de5
commit 75b7e21bec
4 changed files with 18 additions and 12 deletions

View File

@ -13,6 +13,10 @@ func Next(ctx context.Context) (time.Time, error) {
return time.Time{}, err return time.Time{}, err
} }
if deadline := Deadline(); time.Since(deadline) > time.Minute {
return time.Parse("2006-01-02", time.Now().Add(24*time.Hour).Format("2006-01-02"))
}
type Did struct { type Did struct {
Did time.Time Did time.Time
} }

View File

@ -13,7 +13,7 @@ func TestNextRecord(t *testing.T) {
if v, err := asses.Next(ctx); err != nil { if v, err := asses.Next(ctx); err != nil {
t.Fatal(err) t.Fatal(err)
} else if zero := v.IsZero(); !zero { } else if zero := v.IsZero(); !zero && time.Now().Hour() < 8 {
t.Fatal(v) t.Fatal(v)
} }
@ -23,7 +23,7 @@ func TestNextRecord(t *testing.T) {
if v, err := asses.Next(ctx); err != nil { if v, err := asses.Next(ctx); err != nil {
t.Fatal(err) t.Fatal(err)
} else if since := time.Since(v); since > time.Minute { } else if since := time.Since(v); since > time.Minute && time.Now().Hour() < 8 {
t.Fatal(since) t.Fatal(since)
} }
} }

11
src/asses/deadline.go Normal file
View File

@ -0,0 +1,11 @@
package asses
import "time"
func Deadline() time.Time {
midnightLastNight, err := time.Parse("2006-01-02", time.Now().Format("2006-01-02"))
if err != nil {
panic(err)
}
return midnightLastNight.Add(8 * time.Hour) // midnight-8AM
}

View File

@ -3,12 +3,10 @@ package asses
import ( import (
"context" "context"
"io/fs" "io/fs"
"log"
"path" "path"
"path/filepath" "path/filepath"
"show-rss/src/asses" "show-rss/src/asses"
"show-rss/src/cron" "show-rss/src/cron"
"time"
) )
var rootDs = []string{ var rootDs = []string{
@ -19,14 +17,7 @@ var rootDs = []string{
type CB func(context.Context, string) error type CB func(context.Context, string) error
func Main(ctx context.Context) error { func Main(ctx context.Context) error {
midnightLastNight, err := time.Parse("2006-01-02", time.Now().Format("2006-01-02")) ctx, can := context.WithDeadline(ctx, asses.Deadline())
if err != nil {
panic(err)
}
deadline := midnightLastNight.Add(8 * time.Hour) // midnight-8AM
log.Printf("assing until %v (%v)", deadline, time.Until(deadline))
ctx, can := context.WithDeadline(ctx, deadline)
defer can() defer can()
return cron.Cron(ctx, asses.Next, One) return cron.Cron(ctx, asses.Next, One)