-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (75 loc) · 2.09 KB
/
main.go
File metadata and controls
86 lines (75 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"context"
"github.com/jackc/pgtype"
_ "github.com/jackc/pgx/v5/stdlib"
"log/slog"
"os"
"strconv"
"sync"
"time"
)
var databaseClient *DatabaseClient
type Game struct {
ID int `sql:"id"`
HomeTeam_ID int `sql:"home_team_id"`
AwayTeam_ID int `sql:"away_team_id"`
Date pgtype.Date `sql:"date"`
}
func init() {
slog.Info("Initializing...")
databaseClient = NewDatabaseClient()
slog.Info("Setup complete!")
}
func StatsJob(GameID int, group *sync.WaitGroup) {
defer group.Done()
for {
slog.Info("Running stats job for game " + strconv.Itoa(GameID) + "...")
finished, err := handleGameStats(GameID, databaseClient)
if err != nil {
slog.Error("Error handling game stats for game " + strconv.Itoa(GameID) + "...")
slog.Error(err.Error())
break
}
if finished {
slog.Info("Stats job for game " + strconv.Itoa(GameID) + " complete (finished), exiting...")
break
}
slog.Info("Stats job for game " + strconv.Itoa(GameID) + " complete (scheduled or in progress), waiting 20 seconds for next query...")
time.Sleep(20 * time.Second)
}
}
func main() {
// Get information about the games for today
databaseClient.dbMut.Lock()
rows, err := databaseClient.db.Query(
context.Background(),
`SELECT id, "homeTeam_id", "awayTeam_id", date FROM games WHERE date = $1`,
time.Now().Format("2006-01-02"))
if err != nil {
slog.Error("Could not obtain games for today...")
slog.Error(err.Error())
os.Exit(1)
}
var games = make([]Game, 0)
for rows.Next() {
var game Game
err := rows.Scan(&game.ID, &game.HomeTeam_ID, &game.AwayTeam_ID, &game.Date)
if err != nil {
slog.Error("Could not scan game...")
slog.Error(err.Error())
os.Exit(1)
}
games = append(games, game)
}
rows.Close()
databaseClient.dbMut.Unlock()
universalWait := &sync.WaitGroup{}
for _, game := range games {
universalWait.Add(1)
go StatsJob(game.ID, universalWait)
time.Sleep(500 * time.Millisecond) // Just to space things out a little.
}
universalWait.Wait()
slog.Info("All games for today have been processed, exiting...")
}