-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (62 loc) · 1.52 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
package main
import (
"fmt"
mapset "github.com/deckarep/golang-set"
"os"
"time"
)
var logFileName string
const inputChanSize = 512
const outputChanSize = 32
func archiveWorker(input, done chan string, inProgress mapset.Set) {
for id := range input {
if inProgress.Contains(id) {
continue
}
inProgress.Add(id)
shouldMarkAsArchived := archiveID(id)
if shouldMarkAsArchived {
done <- id
} else {
inProgress.Remove(id)
}
}
}
func markAsArchivedWorker(done chan string, inProgress mapset.Set) {
var buffer []string
for id := range done {
buffer = append(buffer, id)
inProgress.Remove(id)
if len(buffer) > outputChanSize {
err := markIDsArchived(buffer...)
buffer = nil
if err != nil {
//TODO: better error message
fmt.Fprintf(os.Stderr, "Error while marking IDs as archived: %s\n", err.Error())
}
}
}
}
func main() {
// Parse arguments
parseArgs(os.Args)
// Generate log file name based on current time
t := time.Now()
logFileName = t.Format("20060102150405") + ".log"
inputIds := make(chan string, inputChanSize)
doneIds := make(chan string, outputChanSize)
// To avoid working on the same video in multiple goroutines
inProgress := mapset.NewSet()
// "Mark as archived" worker
go markAsArchivedWorker(doneIds, inProgress)
// "Archive" worker
for i := 0; i < arguments.Concurrency; i++ {
go archiveWorker(inputIds, doneIds, inProgress)
}
// "Fetch IDs" worker
for {
for _, id := range getID(arguments.Secret, 0, inputChanSize*2) {
inputIds <- id
}
}
}