Add cron job functionality and Docker integration
This commit is contained in:
85
cmd/cron/main.go
Normal file
85
cmd/cron/main.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.mchus.pro/mchus/quoteforge/internal/config"
|
||||
"git.mchus.pro/mchus/quoteforge/internal/models"
|
||||
"git.mchus.pro/mchus/quoteforge/internal/repository"
|
||||
"git.mchus.pro/mchus/quoteforge/internal/services/alerts"
|
||||
"git.mchus.pro/mchus/quoteforge/internal/services/pricing"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func main() {
|
||||
configPath := flag.String("config", "config.yaml", "path to config file")
|
||||
cronJob := flag.String("job", "", "type of cron job to run (alerts, update-prices)")
|
||||
flag.Parse()
|
||||
|
||||
cfg, err := config.Load(*configPath)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load config: %v", err)
|
||||
}
|
||||
|
||||
db, err := gorm.Open(mysql.Open(cfg.Database.DSN()), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to database: %v", err)
|
||||
}
|
||||
|
||||
// Ensure tables exist
|
||||
if err := models.Migrate(db); err != nil {
|
||||
log.Fatalf("Migration failed: %v", err)
|
||||
}
|
||||
|
||||
// Initialize repositories
|
||||
statsRepo := repository.NewStatsRepository(db)
|
||||
alertRepo := repository.NewAlertRepository(db)
|
||||
componentRepo := repository.NewComponentRepository(db)
|
||||
priceRepo := repository.NewPriceRepository(db)
|
||||
|
||||
// Initialize services
|
||||
alertService := alerts.NewService(alertRepo, componentRepo, priceRepo, statsRepo, cfg.Alerts, cfg.Pricing)
|
||||
pricingService := pricing.NewService(componentRepo, priceRepo, cfg.Pricing)
|
||||
|
||||
switch *cronJob {
|
||||
case "alerts":
|
||||
log.Println("Running alerts check...")
|
||||
if err := alertService.CheckAndGenerateAlerts(); err != nil {
|
||||
log.Printf("Error running alerts check: %v", err)
|
||||
} else {
|
||||
log.Println("Alerts check completed successfully")
|
||||
}
|
||||
case "update-prices":
|
||||
log.Println("Recalculating all prices...")
|
||||
updated, errors := pricingService.RecalculateAllPrices()
|
||||
log.Printf("Prices recalculated: %d updated, %d errors", updated, errors)
|
||||
case "reset-counters":
|
||||
log.Println("Resetting usage counters...")
|
||||
if err := statsRepo.ResetWeeklyCounters(); err != nil {
|
||||
log.Printf("Error resetting weekly counters: %v", err)
|
||||
}
|
||||
if err := statsRepo.ResetMonthlyCounters(); err != nil {
|
||||
log.Printf("Error resetting monthly counters: %v", err)
|
||||
}
|
||||
log.Println("Usage counters reset completed")
|
||||
case "update-popularity":
|
||||
log.Println("Updating popularity scores...")
|
||||
if err := statsRepo.UpdatePopularityScores(); err != nil {
|
||||
log.Printf("Error updating popularity scores: %v", err)
|
||||
} else {
|
||||
log.Println("Popularity scores updated successfully")
|
||||
}
|
||||
default:
|
||||
log.Println("No valid cron job specified. Available jobs:")
|
||||
log.Println(" - alerts: Check and generate alerts")
|
||||
log.Println(" - update-prices: Recalculate all prices")
|
||||
log.Println(" - reset-counters: Reset usage counters")
|
||||
log.Println(" - update-popularity: Update popularity scores")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user