/
main.go
144 lines (112 loc) · 3.74 KB
/
main.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
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
package main
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/yosiat/npm-downloader/models"
)
const baseDir = "/Users/yosi/code/go/src/github.com/yosiat/npm-downloader"
const downloadDirectory = "/Volumes/Data/npm"
type PackageStatus struct {
Error error
IsDownloaded bool
Package models.Package
}
// findPackagesToDownload given a list of already downloaded packages and the changes feed
// we return a list of packages to download
func findPackagesToDownload(downloadedPackages, changes map[string]models.PackageCommit) []models.PackageCommit {
var packagesToDownload []models.PackageCommit
for pkgID, change := range changes {
if pkgID != "react" {
continue
}
var downloadedPackage models.PackageCommit
var exists bool
// If didn't already downloaded this package
if downloadedPackage, exists = downloadedPackages[pkgID]; !exists {
packagesToDownload = append(packagesToDownload, change)
continue
}
// If there is change in revision
if downloadedPackage.Revision != change.Revision {
packagesToDownload = append(packagesToDownload, change)
}
}
return packagesToDownload
}
func packageWorker(repository NpmRepository,
downloadedPackages map[string]models.PackageCommit,
jobs <-chan models.PackageCommit,
results chan<- PackageStatus) {
for item := range jobs {
// Fetch the package from remote
pkg, err := repository.FetchPackage(item.Id)
if err != nil {
results <- PackageStatus{Error: err, IsDownloaded: true, Package: models.Package{Id: item.Id, Revision: item.Revision}}
return
}
downloadedInfo := downloadedPackages[item.Id]
pkgCommitStatus := models.CreatePackageCommit(pkg)
// Check there are changes..
if !downloadedInfo.IsChanged(pkgCommitStatus) {
results <- PackageStatus{Error: nil, IsDownloaded: false, Package: pkg}
}
// We have changes :)
versionsToDownload := downloadedInfo.VersionsToDownload(pkgCommitStatus)
downloadErr := pkg.Download(downloadDirectory, versionsToDownload)
if downloadErr != nil {
results <- PackageStatus{Error: downloadErr, IsDownloaded: true, Package: pkg}
} else {
results <- PackageStatus{Error: nil, IsDownloaded: true, Package: pkg}
}
}
}
func main() {
log.SetFormatter(&log.TextFormatter{})
fmt.Println("npm-downloader (v0.1)")
skim := NpmRepository{baseURL: "https://skimdb.npmjs.com/registry"}
commitsRepo, err := CreateCommitsRepository(baseDir)
if err != nil {
panic(err)
}
defer commitsRepo.Close()
fmt.Println("Reading changes from file _changes and from the db")
changes := ReadChanges(baseDir)
downloadedPackages := commitsRepo.AllSucessfullPackages()
packagesToDownload := findPackagesToDownload(downloadedPackages, changes)
workersCount := 6
jobsCount := len(packagesToDownload)
results := make(chan PackageStatus, jobsCount)
jobs := make(chan models.PackageCommit, jobsCount)
// Initialize wokrers
fmt.Printf("Starting %v workers\n", workersCount)
for w := 1; w <= workersCount; w++ {
go packageWorker(skim, downloadedPackages, jobs, results)
}
// Submit jobs
fmt.Printf("Submitting %v jobs\n", jobsCount)
for _, change := range packagesToDownload {
jobs <- change
}
close(jobs)
fmt.Println("Waiting for results..")
for a := 1; a <= jobsCount; a++ {
status := <-results
statusText := ""
if status.Error == nil {
statusText = "Downloaded successfuly"
} else {
statusText = fmt.Sprintf("Downloaded with error: %s", status.Error)
}
if !status.IsDownloaded {
statusText = "no changes found"
}
fmt.Printf("[%v] %s %s\n", a, status.Package.Id, statusText)
if status.Error == nil {
commitsRepo.Sucess(status.Package)
} else {
commitsRepo.Error(status.Package.Id, status.Error)
}
fmt.Printf("[%v] Commited %s\n", a, status.Package.Id)
}
fmt.Println("FINISHED!")
}