@@ -16,6 +16,7 @@ import (
16
16
"sync"
17
17
"syscall"
18
18
19
+ "github.com/alitto/pond"
19
20
"github.com/oklog/run"
20
21
"github.com/prometheus/client_golang/prometheus"
21
22
"github.com/prometheus/client_golang/prometheus/collectors"
@@ -24,8 +25,6 @@ import (
24
25
25
26
"github.com/cluttrdev/cli"
26
27
27
- "github.com/cluttrdev/gitlab-exporter/pkg/worker"
28
-
29
28
"github.com/cluttrdev/gitlab-exporter/internal/config"
30
29
"github.com/cluttrdev/gitlab-exporter/internal/exporter"
31
30
"github.com/cluttrdev/gitlab-exporter/internal/gitlab"
@@ -134,6 +133,7 @@ func (c *RunConfig) Exec(ctx context.Context, _ []string) error {
134
133
}
135
134
136
135
// gather projects from config
136
+ slog .Info ("Resolving projects to export..." )
137
137
projects , err := resolveProjects (ctx , cfg , gitlabclient )
138
138
if err != nil {
139
139
return fmt .Errorf ("error resolving projects: %w" , err )
@@ -155,25 +155,12 @@ func (c *RunConfig) Exec(ctx context.Context, _ []string) error {
155
155
156
156
g := & run.Group {}
157
157
158
- pool := worker .NewWorkerPool (42 )
159
- { // worker pool
160
- ctx , cancel := context .WithCancel (context .Background ())
161
-
162
- g .Add (func () error { // execute
163
- slog .Info ("Starting worker pool" )
164
- pool .Start (ctx )
165
- <- ctx .Done ()
166
- return ctx .Err ()
167
- }, func (err error ) { // interrupt
168
- defer cancel ()
169
- slog .Info ("Stopping worker pool..." )
170
- pool .Stop ()
171
- slog .Info ("Stopping worker pool... done" )
172
- })
173
- }
174
-
175
158
if len (projects ) > 0 { // jobs
176
- ctx , cancel := context .WithCancel (context .Background ())
159
+ slog .Info (fmt .Sprintf ("Found %d projects to export" , len (projects )))
160
+ ctxJobs , cancelJobs := context .WithCancel (context .Background ())
161
+
162
+ slog .Info ("Starting worker pool" )
163
+ pool := pond .New (42 , 1024 , pond .Context (ctxJobs ))
177
164
178
165
g .Add (func () error { // execute
179
166
var wg sync.WaitGroup
@@ -189,7 +176,7 @@ func (c *RunConfig) Exec(ctx context.Context, _ []string) error {
189
176
wg .Add (1 )
190
177
go func () {
191
178
defer wg .Done ()
192
- job .Run (ctx )
179
+ job .Run (ctxJobs )
193
180
}()
194
181
}
195
182
@@ -203,16 +190,15 @@ func (c *RunConfig) Exec(ctx context.Context, _ []string) error {
203
190
wg .Add (1 )
204
191
go func () {
205
192
defer wg .Done ()
206
- job .Run (ctx )
193
+ job .Run (ctxJobs )
207
194
}()
208
195
}
209
196
210
197
wg .Wait ()
211
198
return nil
212
199
}, func (err error ) { // interrupt
213
200
slog .Info ("Cancelling jobs..." )
214
- cancel ()
215
- <- ctx .Done ()
201
+ cancelJobs ()
216
202
slog .Info ("Cancelling jobs... done" )
217
203
})
218
204
} else {
@@ -296,43 +282,47 @@ func serveHTTP(cfg config.HTTP, reg *prometheus.Registry) (func() error, func(er
296
282
}
297
283
298
284
func resolveProjects (ctx context.Context , cfg config.Config , glab * gitlab.Client ) ([]config.Project , error ) {
299
- pm := make (map [int64 ]config.Project )
285
+ projectConfigs := make (map [int64 ]config.Project )
300
286
301
287
opt := gitlab.ListNamespaceProjectsOptions {}
302
288
for _ , namespace := range cfg .Namespaces {
303
289
opt .Kind = namespace .Kind
304
- opt .Visibility = (* gitlab .VisibilityValue )(& namespace .Visibility )
290
+ opt .Visibility = (* _gitlab .VisibilityValue )(& namespace .Visibility )
305
291
opt .WithShared = namespace .WithShared
306
292
opt .IncludeSubgroups = namespace .IncludeSubgroups
307
293
308
- ps , err := glab .ListNamespaceProjects (ctx , namespace .Id , opt )
309
- if err != nil {
310
- return nil , err
311
- }
312
-
313
- for _ , p := range ps {
314
- pm [p .Id ] = config.Project {
315
- ProjectSettings : namespace .ProjectSettings ,
316
- Id : p .Id ,
294
+ err := glab .ListNamespaceProjects (ctx , namespace .Id , opt , func (projects []* _gitlab.Project ) bool {
295
+ for _ , project := range projects {
296
+ projectID := int64 (project .ID )
297
+ projectConfigs [projectID ] = config.Project {
298
+ ProjectSettings : namespace .ProjectSettings ,
299
+ Id : projectID ,
300
+ }
317
301
}
318
- }
319
302
320
- for _ , pid := range namespace .ExcludeProjects {
321
- p , _ , err := glab .Client ().Projects .GetProject (pid , nil , _gitlab .WithContext (ctx ))
322
- if err != nil {
323
- return nil , fmt .Errorf ("error getting project %q: %w" , pid , err )
303
+ for _ , pid := range namespace .ExcludeProjects {
304
+ p , _ , err := glab .Client ().Projects .GetProject (pid , nil , _gitlab .WithContext (ctx ))
305
+ if err != nil {
306
+ slog .Error ("error getting namespace project" , "namespace_id" , namespace .Id , "project" , pid , "error" , err )
307
+ return false
308
+ }
309
+ delete (projectConfigs , int64 (p .ID ))
324
310
}
325
- delete (pm , int64 (p .ID ))
311
+
312
+ return true
313
+ })
314
+ if err != nil {
315
+ return nil , err
326
316
}
327
317
}
328
318
329
319
// overwrite with explicitly configured projects
330
320
for _ , p := range cfg .Projects {
331
- pm [p .Id ] = p
321
+ projectConfigs [p .Id ] = p
332
322
}
333
323
334
- projects := make ([]config.Project , 0 , len (pm ))
335
- for _ , p := range pm {
324
+ projects := make ([]config.Project , 0 , len (projectConfigs ))
325
+ for _ , p := range projectConfigs {
336
326
projects = append (projects , p )
337
327
}
338
328
0 commit comments