summaryrefslogtreecommitdiff
path: root/rill.go
blob: bd25d4e4fe5d87cdb469514a825086df3268b87c (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package forgepb

import (
	"fmt"
	"sync"
	"time"

	"github.com/destel/rill"
	"go.wit.com/lib/cobol"
	"go.wit.com/lib/config"
	"go.wit.com/lib/env"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

// rill is awesome. long live rill
//
// x is the size of the queued up pool (shouldn't matter here for this I think)
// y is how many simultanous functions will run
// todo: tune and compute x,y by # of CPUs and disk io
// todo: store x,y in forge config ? (or compute them. notsure)
func (f *Forge) RillReload() int {
	var all []*gitpb.Repo
	for repo := range f.Repos.IterAll() {
		if !repo.IsValidDir() {
			log.Printf("%s %-50s\n", "got an invalid repo in forgepb.RillReload()", repo.GetFullPath())
			f.Repos.Delete(repo)
			config.SetChanged("repos", true)
			continue
		}
		all = append(all, repo)
	}
	// Convert a slice of user IDs into a channel
	ids := rill.FromSlice(all, nil)

	var counter int
	// Read users from the API.
	// Concurrency = 20
	dirs := rill.Map(ids, cobol.Int(env.Get("RillX")), func(repo *gitpb.Repo) (*gitpb.Repo, error) {
		return repo, nil
	})

	rill.ForEach(dirs, cobol.Int(env.Get("RillY")), func(repo *gitpb.Repo) error {
		if err := repo.HasChanged(); err != nil {
			counter += 1
			return err
		}
		return nil
	})

	return counter
}

// x is the size of the queued up pool (shouldn't matter here for this I think)
// y is how many simultanous functions will run
// todo: tune and compute x,y by # of CPUs and disk io
// todo: store x,y in forge config ? (or compute them. notsure)
func (f *Forge) RillFuncError(rillf func(*gitpb.Repo) error) map[string]*RillStats {
	return f.RillRepos(rillf)
}

func (f *Forge) ConfigRill(rillX int, rillY int) {
	env.Set("RillX", fmt.Sprintf("%d", rillX))
	env.Set("RillY", fmt.Sprintf("%d", rillY))
	// log.Infof("Setting rill values to %d,%d\n", f.Config.RillX, f.Config.RillY)
}

type RillStats struct {
	Name  string
	Err   error
	Start time.Time
	End   time.Time
}

var rillMu sync.Mutex

func rillSetError(stats map[string]*RillStats, fullpath string, err error) {
	rillMu.Lock()
	defer rillMu.Unlock()
	if s, ok := stats[fullpath]; ok {
		s.Err = err
		return
	}
	log.Info("WHAT THE FUCK STATS ERROR", fullpath)
}

func rillSetStartTime(stats map[string]*RillStats, fullpath string) {
	rillMu.Lock()
	defer rillMu.Unlock()
	if s, ok := stats[fullpath]; ok {
		s.Start = time.Now()
		return
	}
	var s *RillStats
	s = new(RillStats)
	s.Start = time.Now()
	stats[fullpath] = s
}

func rillSetEndTime(stats map[string]*RillStats, fullpath string) {
	rillMu.Lock()
	defer rillMu.Unlock()
	if s, ok := stats[fullpath]; ok {
		s.End = time.Now()
		return
	}
	log.Info("WHAT THE FUCK STATS END TIME", fullpath)
}

// x is the size of the queued up pool (shouldn't matter here for this I think)
// y is how many simultanous functions will run
// todo: tune and compute x,y by # of CPUs and disk io
// todo: store x,y in forge config ? (or compute them. notsure)
func (f *Forge) RillRepos(rillf func(*gitpb.Repo) error) map[string]*RillStats {
	var all []*gitpb.Repo

	var stats map[string]*RillStats
	stats = make(map[string]*RillStats)

	for repo := range f.Repos.IterAll() {
		if !repo.IsValidDir() {
			log.Printf("got an invalid repo in forgepb.RillRepos() %-50s\n", repo.GetFullPath())
			f.Repos.Delete(repo)
			config.SetChanged("repos", true)
			continue
		}
		all = append(all, repo)
	}
	// log.Info("Rill Repos len =", len(all))
	// Convert a slice of user IDs into a channel
	ids := rill.FromSlice(all, nil)

	var counter int
	var watch int = 10

	// Read users from the API.
	// Concurrency = 20
	dirs := rill.Map(ids, cobol.Int(env.Get("RillX")), func(id *gitpb.Repo) (*gitpb.Repo, error) {
		return id, nil
	})

	rill.ForEach(dirs, cobol.Int(env.Get("RillY")), func(repo *gitpb.Repo) error {
		// todo: make this a goroutine to show stats to the user
		rillMu.Lock()
		counter += 1
		if counter > watch {
			// log.Info("Processed", watch, "repos") // this doesn't work
			watch += 50
		}
		rillMu.Unlock()

		rillSetStartTime(stats, repo.GetFullPath())
		if err := rillf(repo); err != nil {
			rillSetError(stats, repo.GetFullPath(), err)
		}
		rillSetEndTime(stats, repo.GetFullPath())
		return nil
	})

	return stats
}

// returns the set of failed repos
func (f *Forge) RunOnRepos(repos *gitpb.Repos, rillf func(*gitpb.Repo) error) *gitpb.Repos {
	stats := f.RunOnReposOld(repos, rillf)
	failed := gitpb.NewRepos()
	for s, stat := range stats {
		if stat.Err == nil {
			continue
		}
		found := f.Repos.FindByFullPath(s)
		if found == nil {
			log.Info("found", found, "'"+s+"'")
			panic("wrong namespace logic. couldn't find repo from stats")
		}
		newr := failed.Clone(found)
		newr.State = fmt.Sprintf("%v", stat.Err)
	}

	return failed
}

// dumb & slow, but needed. This is old school dumb. Before we had the awesome machines we have today.
// Use this if you think SMP processing might be the problem.
// if this works, and GO Rill doesn't work, then you, yes you, are the problem. Your code sucks.
// fix it, happy hacking
func (f *Forge) RunOnReposSlow(repos *gitpb.Repos, rillf func(*gitpb.Repo) error) *gitpb.Repos {
	failed := gitpb.NewRepos()
	counter := 1
	for repo := range repos.IterAll() {
		err := rillf(repo)
		if err == nil {
			continue
		}
		newr := failed.Clone(repo)
		newr.State = fmt.Sprintf("%v) (%d)", err, counter)
		counter += 1
	}

	return failed
}

// x is the size of the queued up pool (shouldn't matter here for this I think)
// y is how many simultanous functions will run
// todo: tune and compute x,y by # of CPUs and disk io
// todo: store x,y in forge config ? (or compute them. notsure)
func (f *Forge) RunOnReposOld(repos *gitpb.Repos, rillf func(*gitpb.Repo) error) map[string]*RillStats {
	var all []*gitpb.Repo

	var stats map[string]*RillStats
	stats = make(map[string]*RillStats)

	for repo := range repos.IterAll() {
		if !repo.IsValidDir() {
			log.Printf("got an invalid repo in forgepb.RillRepos() %-50s\n", repo.GetFullPath())
			continue
		}
		all = append(all, repo)
	}
	// log.Info("Rill Repos len =", len(all))
	// Convert a slice of user IDs into a channel
	ids := rill.FromSlice(all, nil)

	var counter int
	var watch int = 10

	// Read users from the API.
	// Concurrency = 20
	dirs := rill.Map(ids, cobol.Int(env.Get("RillX")), func(id *gitpb.Repo) (*gitpb.Repo, error) {
		return id, nil
	})

	rill.ForEach(dirs, cobol.Int(env.Get("RillY")), func(repo *gitpb.Repo) error {
		// todo: make this a goroutine to show stats to the user
		rillMu.Lock()
		counter += 1
		if counter > watch {
			// log.Info("Processed", watch, "repos") // this doesn't work
			watch += 50
		}
		rillMu.Unlock()

		rillSetStartTime(stats, repo.GetFullPath())
		if err := rillf(repo); err != nil {
			rillSetError(stats, repo.GetFullPath(), err)
		}
		rillSetEndTime(stats, repo.GetFullPath())
		return nil
	})

	return stats
}