58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os"
|
|
"os/signal"
|
|
"time"
|
|
|
|
"git.amiirkhl.ir/starcoach-hackathon/zarinpal-analytics-backend/internal/coach"
|
|
"git.amiirkhl.ir/starcoach-hackathon/zarinpal-analytics-backend/internal/config"
|
|
"git.amiirkhl.ir/starcoach-hackathon/zarinpal-analytics-backend/internal/database"
|
|
)
|
|
|
|
func main() {
|
|
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
logger.Error("configuration failed", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
defer cancel()
|
|
if !cfg.AICoachEnabled {
|
|
logger.Info("growth coach scheduler disabled")
|
|
<-ctx.Done()
|
|
return
|
|
}
|
|
pool, err := database.Open(ctx, cfg.DatabaseURL)
|
|
if err != nil {
|
|
logger.Error("database connection failed", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
defer pool.Close()
|
|
service := coach.NewService(coach.NewRepository(pool), nil)
|
|
run := func() {
|
|
runCtx, runCancel := context.WithTimeout(ctx, time.Minute)
|
|
defer runCancel()
|
|
created, err := service.ScheduleDue(runCtx)
|
|
if err != nil {
|
|
logger.Error("coach scheduling failed", "error", err)
|
|
return
|
|
}
|
|
logger.Info("coach scheduling complete", "created_jobs", created)
|
|
}
|
|
run()
|
|
for {
|
|
timer := time.NewTimer(time.Until(coach.NextTehranMidnight(time.Now())))
|
|
select {
|
|
case <-ctx.Done():
|
|
timer.Stop()
|
|
return
|
|
case <-timer.C:
|
|
run()
|
|
}
|
|
}
|
|
}
|