/
run.go
100 lines (91 loc) · 2.69 KB
/
run.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
package cli
import (
"log"
"runtime"
"sync"
"github.com/alecbcs/lookout/ui"
"github.com/DataDrake/cli-ng/v2/cmd"
"github.com/alecbcs/lookout/config"
"github.com/alecbcs/lookout/database"
"github.com/alecbcs/lookout/results"
"github.com/alecbcs/lookout/update"
)
// Run executes a full database scan to see if any applictions are out of date.
var Run = cmd.Sub{
Name: "run",
Alias: "r",
Short: "Run a full scan of the database to check for updates.",
Args: &RunArgs{},
Run: RunFull,
}
// RunArgs handles arguments passed to the run command.
type RunArgs struct {
}
// RunFull creates all nessisary go routines for the scan to run.
func RunFull(r *cmd.Root, c *cmd.Sub) {
update.Init(config.Global.Github.Key)
db := database.Open(config.Global.Database.Path)
// Create the input chan to store database enties.
input := make(chan *results.Entry)
// Create the toUpdate chan to store updated apps to write back to database.
// Buffer chan to improve performance.
toUpdate := make(chan *results.Entry, 32)
workers := genNumWorkers()
// Create Sync WaitGroup to watch goroutines so that we don't close the channels early.
var wg sync.WaitGroup
wg.Add(workers)
// Get all will read db entries and put in queue for workers.
go database.GetAll(db, input)
for i := 0; i < workers; i++ {
go runWorker(&wg, input, toUpdate)
}
// Create a go routine to wait till all workers are finished before closing channel.
go func() {
wg.Wait()
close(toUpdate)
}()
// Update all db entires that are out-of-date.
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
for app := range toUpdate {
database.Update(tx, app)
}
err = tx.Commit()
if err != nil {
log.Fatal(err)
}
defer db.Close()
}
// Generate the number of worker processes to optimize efficiency.
// Subtract 2 from the number of cores because of the main thread and the GetAll function.
func genNumWorkers() int {
if runtime.NumCPU() > 2 {
return runtime.NumCPU() - 2
}
return 1
}
// runWorker defines an update worker process.
func runWorker(wg *sync.WaitGroup, input <-chan *results.Entry, output chan<- *results.Entry) {
// Pull the next app off the queue channel.
for app := range input {
result, found := update.CheckUpdate(app.CurrentURL)
if !found {
ui.PrintRed(app.ID, "Not Found")
continue
}
// If the latest version does not match the database version, mark out-od-date.
if !update.UpToDate(result.Version, app.CurrentVersion) {
app.LatestURL = result.Location
app.LatestVersion = result.Version
app.UpToDate = false
output <- app
ui.PrintRed(app.ID, "New Version Found")
} else {
ui.PrintCyan(app.ID, "Up-To-Date")
}
}
// Tell WaitGroup that go routine has finished.
defer wg.Done()
}