summaryrefslogtreecommitdiff
path: root/rill.go
blob: 34881461050e3aa9bbd5e19e8ba16097ad5a7ab0 (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package forgepb

import (
	"sync"
	"time"

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

/*
// rill is awesome. long live rill
// attempt scan with rill
func (f *Forge) rillUpdate(pool1 int, pool2 int) (int, error) {
	var repos []*gitpb.Repo
	all := f.Repos.SortByFullPath()
	for all.Scan() {
		repo := all.Next()
		repos = append(repos, repo)
	}

	// Convert a slice of user IDs into a channel
	ids := rill.FromSlice(repos, nil)

	// Read users from the API.
	// Concurrency = 20
	rills := rill.Map(ids, pool1, func(repo *gitpb.Repo) (*gitpb.Repo, error) {
		return repo, nil
	})

	var counter int
	// Activate users.
	// Concurrency = 10
	err := rill.ForEach(rills, pool2, func(repo *gitpb.Repo) error {
		counter += 1
		return f.updateRepo(repo)
	})

	return counter, err
}
*/

/*
func (f *Forge) updateRepo(repo *gitpb.Repo) error {
	if !repo.IsValidDir() {
		log.Printf("%10s %-50s gopath=%s\n", "git dir is missing\n", repo.FullPath, repo.GetNamespace())
		f.Repos.DeleteByFullPath(repo.FullPath)
		return nil
	}

	if err := repo.HasChanged(); err != nil {
		// something changed in the repo
		return err
	}

	// log.Info("repo did not change", repo.FullPath, repo.StateChange)
	if f.Config.IsReadOnly(repo.GetGoPath()) {
		if repo.ReadOnly {
		} else {
			log.Info("readonly flag on repo is wrong", repo.GetGoPath())
			repo.ReadOnly = true
		}
	}
	return nil
}
*/

// var RillX int = 10
// var RillY int = 20

// 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)
			log.Info("reposSave = true")
			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, int(f.Config.RillX), func(repo *gitpb.Repo) (*gitpb.Repo, error) {
		return repo, nil
	})

	rill.ForEach(dirs, int(f.Config.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) {
	f.Config.RillX = int32(rillX)
	f.Config.RillY = int32(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)
			log.Info("reposSave = true")
			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, int(f.Config.RillX), func(id *gitpb.Repo) (*gitpb.Repo, error) {
		return id, nil
	})

	rill.ForEach(dirs, int(f.Config.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
}

// 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) RunOnRepos(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, int(f.Config.RillX), func(id *gitpb.Repo) (*gitpb.Repo, error) {
		return id, nil
	})

	rill.ForEach(dirs, int(f.Config.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
}