-
Notifications
You must be signed in to change notification settings - Fork 559
/
Copy pathtasks.go
174 lines (146 loc) · 4.1 KB
/
tasks.go
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"log/slog"
"os"
"github.com/diggerhq/digger/backend/ci_backends"
"github.com/diggerhq/digger/backend/models"
"github.com/diggerhq/digger/backend/services"
"github.com/diggerhq/digger/backend/utils"
"github.com/diggerhq/digger/libs/scheduler"
"github.com/robfig/cron"
)
func initLogging() {
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo,
})
logger := slog.New(handler)
slog.SetDefault(logger)
}
func main() {
initLogging()
slog.Info("Starting Digger tasks scheduler")
models.ConnectDatabase()
slog.Info("Database connection established")
c := cron.New()
// RunQueues state machine
c.AddFunc("0 * * * * *", func() {
slog.Info("Running RunQueues state machine task")
runQueues, err := models.DB.GetFirstRunQueueForEveryProject()
if err != nil {
slog.Error("Error fetching latest queue item runs", "error", err)
return
}
slog.Debug("Processing run queue items", "count", len(runQueues))
for _, queueItem := range runQueues {
dr := queueItem.DiggerRun
repo := dr.Repo
slog.Debug("Processing run queue item",
"diggerRunId", dr.ID,
slog.Group("repository",
slog.String("fullName", repo.RepoFullName),
slog.String("owner", repo.RepoOrganisation),
slog.String("name", repo.RepoName),
),
"installationId", dr.GithubInstallationId,
)
repoFullName := repo.RepoFullName
repoOwner := repo.RepoOrganisation
repoName := repo.RepoName
service, _, err := utils.GetGithubService(
&utils.DiggerGithubRealClientProvider{},
dr.GithubInstallationId,
repoFullName,
repoOwner,
repoName,
)
if err != nil {
slog.Error("Failed to get GitHub service for DiggerRun",
"diggerRunId", dr.ID,
"repoFullName", repoFullName,
"error", err,
)
continue
}
RunQueuesStateMachine(&queueItem, service, &utils.DiggerGithubRealClientProvider{})
}
slog.Info("Completed RunQueues state machine task", "processedItems", len(runQueues))
})
// Trigger queued jobs for a batch
c.AddFunc("30 * * * * *", func() {
slog.Info("Running trigger queued jobs task")
jobs, err := models.DB.GetDiggerJobsWithStatus(scheduler.DiggerJobQueuedForRun)
if err != nil {
slog.Error("Failed to get queued jobs", "error", err)
return
}
slog.Debug("Processing queued jobs", "count", len(jobs))
processedCount := 0
for _, job := range jobs {
batch := job.Batch
slog.Debug("Processing queued job",
"jobId", job.DiggerJobID,
"batchId", batch.ID,
slog.Group("repository",
slog.String("fullName", batch.RepoFullName),
slog.String("owner", batch.RepoOwner),
slog.String("name", batch.RepoName),
),
"installationId", batch.GithubInstallationId,
)
repoFullName := batch.RepoFullName
repoName := batch.RepoName
repoOwner := batch.RepoOwner
githubInstallationId := batch.GithubInstallationId
service, _, err := utils.GetGithubService(
&utils.DiggerGithubRealClientProvider{},
githubInstallationId,
repoFullName,
repoOwner,
repoName,
)
if err != nil {
slog.Error("Failed to get GitHub service for job",
"jobId", job.DiggerJobID,
"repoFullName", repoFullName,
"error", err,
)
continue
}
ciBackend := ci_backends.GithubActionCi{Client: service.Client}
err = services.ScheduleJob(
ciBackend,
repoFullName,
repoOwner,
repoName,
&batch.ID,
&job,
&utils.DiggerGithubRealClientProvider{},
)
if err != nil {
slog.Error("Failed to schedule job",
"jobId", job.DiggerJobID,
"batchId", batch.ID,
"error", err,
)
} else {
processedCount++
slog.Info("Successfully scheduled job",
"jobId", job.DiggerJobID,
"batchId", batch.ID,
)
}
}
slog.Info("Completed trigger queued jobs task",
"totalJobs", len(jobs),
"successfullyProcessed", processedCount,
)
})
// Start the Cron job scheduler
slog.Info("Starting cron scheduler")
c.Start()
slog.Info("Digger tasks scheduler is running")
// Keep the application running
// TODO: Add proper channels instead of this approach that consumes CPU cycles
for {
}
}