forked from refiito/pipes-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_sync.go
113 lines (103 loc) · 1.96 KB
/
auto_sync.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
package main
import (
"github.com/bugsnag/bugsnag-go"
"log"
"math/rand"
"strings"
"sync"
"time"
)
var wg sync.WaitGroup
const (
workersCount = 5
sleepMin = 300
sleepMax = 900
)
func runPipes() {
wg.Add(workersCount)
for i := 0; i < workersCount; i++ {
go pipeWorker()
}
}
func pipeWorker() {
for {
pipes, err := getPipesFromQueue()
if err != nil {
bugsnag.Notify(err)
break
}
if pipes == nil {
break
}
for _, pipe := range pipes {
pipe.run()
err := setQueuedPipeSynced(pipe)
if err != nil {
BugsnagNotifyPipe(pipe, err)
}
}
}
wg.Done()
}
func runPipesStub() {
wg.Add(workersCount)
for i := 0; i < workersCount; i++ {
go pipeWorkerStub()
}
}
func pipeWorkerStub() {
ranCount := 0
gotCount := 0
for {
pipes, err := getPipesFromQueue()
if err != nil {
bugsnag.Notify(err)
break
}
if pipes == nil {
break
}
gotCount += len(pipes)
for _, pipe := range pipes {
// NO PIPE RUN HERE
err := setQueuedPipeSynced(pipe)
if err != nil {
log.Printf("ERROR: %s\n", err.Error())
}
ranCount++
}
}
log.Printf("Got %d pipes, ran %d pipes\n", gotCount, ranCount)
wg.Done()
}
func autoSyncRunner() {
for {
time.Sleep(time.Duration(rand.Intn(sleepMax-sleepMin)+sleepMin) * time.Second)
log.Println("-- Autosync started")
runPipes()
wg.Wait()
log.Println("-- Autosync finished")
}
}
func autoSyncRunnerStub() {
for {
time.Sleep(time.Duration(rand.Intn(sleepMax-sleepMin)+sleepMin) * time.Second)
log.Println("-- AutosyncStub started")
runPipesStub()
wg.Wait()
log.Println("-- AutosyncStub finished")
}
}
func autoSyncQueuer() {
for {
time.Sleep(time.Duration(rand.Intn(sleepMax-sleepMin)+sleepMin) * time.Second)
log.Println("-- Queuer started")
_, err := db.Exec(queueAutomaticPipesSQL)
if err != nil {
if !strings.Contains(err.Error(), `duplicate key value violates unique constraint`) {
bugsnag.Notify(err)
}
}
log.Println("-- Queuer finished")
}
}