forked from Scalingo/sclng-backend-test-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_scheduler.go
109 lines (89 loc) · 2.05 KB
/
task_scheduler.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
package main
import (
"sync"
)
type TaskScheduler struct {
repos []map[string]any
githubAccessToken string
}
type GeneratorTaskItem struct {
languagesURL string
repoName string
ID int
}
type LanguagesWorkerTaskItem struct {
languages any
ID int
}
type LicenseWorkerTaskItem struct {
License any
ID int
}
func NewTaskScheduler(token string, repos []map[string]any) *TaskScheduler {
return &TaskScheduler{
githubAccessToken: token,
repos: repos,
}
}
func (t *TaskScheduler) workerLanguages(in chan GeneratorTaskItem) chan LanguagesWorkerTaskItem {
out := make(chan LanguagesWorkerTaskItem)
for repo := range in {
go func(repoIdx int) {
lang := getLanguages(repo.languagesURL, t.githubAccessToken)
out <- LanguagesWorkerTaskItem{
languages: lang,
ID: repoIdx,
}
}(repo.ID)
}
return out
}
func (t *TaskScheduler) workerLicense(in chan GeneratorTaskItem) chan LicenseWorkerTaskItem {
out := make(chan LicenseWorkerTaskItem)
for repo := range in {
go func(repoIdx int) {
license := getLicence(repo.repoName, t.githubAccessToken)
out <- LicenseWorkerTaskItem{
License: license,
ID: repoIdx,
}
}(repo.ID)
}
return out
}
func (t *TaskScheduler) merge() {
var wg sync.WaitGroup
wg.Add(2 * len(t.repos))
chRepos1 := t.generateSourceChannel()
chRepos2 := t.generateSourceChannel()
chLangs := t.workerLanguages(chRepos1)
chLicense := t.workerLicense(chRepos2)
go func() {
for l := range chLangs {
t.repos[l.ID]["languages"] = l.languages
wg.Done()
}
}()
go func() {
for l := range chLicense {
t.repos[l.ID]["license"] = l.License
wg.Done()
}
}()
wg.Wait()
}
func (t *TaskScheduler) generateSourceChannel() chan GeneratorTaskItem {
ch := make(chan GeneratorTaskItem)
go func() {
defer close(ch)
for repoIdx, repo := range t.repos {
item := GeneratorTaskItem{
ID: repoIdx,
languagesURL: repo["languages_url"].(string),
repoName: repo["full_name"].(string),
}
ch <- item
}
}()
return ch
}