Clean up daily package
parent
1f0f38d38e
commit
c3a3746639
|
@ -17,12 +17,14 @@ import (
|
||||||
const secondsPerDay = 60 * 60 * 24
|
const secondsPerDay = 60 * 60 * 24
|
||||||
|
|
||||||
var (
|
var (
|
||||||
dailyRunScheduler = gocron.NewScheduler(time.UTC)
|
scheduler = gocron.NewScheduler(time.UTC)
|
||||||
dailyRunSecret []byte
|
secret []byte
|
||||||
)
|
)
|
||||||
|
|
||||||
func Init() error {
|
func Init() error {
|
||||||
secret, err := os.ReadFile("secret.key")
|
var err error
|
||||||
|
|
||||||
|
secret, err = os.ReadFile("secret.key")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !os.IsNotExist(err) {
|
if !os.IsNotExist(err) {
|
||||||
return fmt.Errorf("failed to read daily seed secret: %s", err)
|
return fmt.Errorf("failed to read daily seed secret: %s", err)
|
||||||
|
@ -42,41 +44,37 @@ func Init() error {
|
||||||
secret = newSecret
|
secret = newSecret
|
||||||
}
|
}
|
||||||
|
|
||||||
dailyRunSecret = secret
|
err = recordNewRun()
|
||||||
|
|
||||||
err = db.TryAddDailyRun(Seed())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Daily Run Seed: %s", Seed())
|
log.Printf("Daily Run Seed: %s", Seed())
|
||||||
|
|
||||||
scheduleRefresh()
|
scheduler.Every(1).Day().At("00:00").Do(recordNewRun())
|
||||||
|
scheduler.StartAsync()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Seed() string {
|
func Seed() string {
|
||||||
return base64.StdEncoding.EncodeToString(deriveDailyRunSeed(time.Now().UTC()))
|
return base64.StdEncoding.EncodeToString(deriveSeed(time.Now().UTC()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func scheduleRefresh() {
|
func deriveSeed(seedTime time.Time) []byte {
|
||||||
dailyRunScheduler.Every(1).Day().At("00:00").Do(func() error {
|
|
||||||
err := Init()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}())
|
|
||||||
dailyRunScheduler.StartAsync()
|
|
||||||
}
|
|
||||||
|
|
||||||
func deriveDailyRunSeed(seedTime time.Time) []byte {
|
|
||||||
day := make([]byte, 8)
|
day := make([]byte, 8)
|
||||||
binary.BigEndian.PutUint64(day, uint64(seedTime.Unix()/secondsPerDay))
|
binary.BigEndian.PutUint64(day, uint64(seedTime.Unix()/secondsPerDay))
|
||||||
|
|
||||||
hashedSeed := md5.Sum(append(day, dailyRunSecret...))
|
hashedSeed := md5.Sum(append(day, secret...))
|
||||||
|
|
||||||
return hashedSeed[:]
|
return hashedSeed[:]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func recordNewRun() error {
|
||||||
|
err := db.TryAddDailyRun(Seed())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue