This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
shifter.go
92 lines (75 loc) · 2.39 KB
/
shifter.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
package shifts
import (
"log"
"github.com/CryptoManiac/open-ethereum-pool/storage"
"github.com/CryptoManiac/open-ethereum-pool/util"
)
type ShiftsConfig struct {
Enabled bool `json:"enabled"`
LongInterval string `json:"longInterval"`
ShortInterval string `json:"shortInterval"`
FlushInterval string `json:"flushInterval"`
KeepLong string `json:"keepLong"`
KeepShort string `json:"keepShort"`
}
type ShiftsProcessor struct {
config *ShiftsConfig
backend *storage.RedisClient
}
func NewShiftsProcessor(cfg *ShiftsConfig, backend *storage.RedisClient) *ShiftsProcessor {
s := &ShiftsProcessor{config: cfg, backend: backend}
return s
}
func (u *ShiftsProcessor) Start() {
log.Println("Starting shifts")
// Shifts
longIntv, shortIntv := util.MustParseDuration(u.config.LongInterval), util.MustParseDuration(u.config.ShortInterval)
log.Printf("Set shifting intervals to (%v, %v)", longIntv, shortIntv)
// Windows
longWindow, shortWindow := util.MustParseDuration(u.config.KeepLong), util.MustParseDuration(u.config.KeepShort)
log.Printf("Set shift windows to (%v, %v)", longWindow, shortWindow)
// Flushing
flushIntv := util.MustParseDuration(u.config.FlushInterval)
log.Printf("Set shifts flushing interval to %v", flushIntv)
processFlush := func() {
users, err := u.backend.GetMiners()
if err != nil {
log.Println("Error while retrieving miners from backend:", err)
} else {
entries, err2 := u.backend.FlushShifts(longWindow, shortWindow, users)
if err2 != nil {
log.Println("Error while flushing entries:", err2)
}
log.Printf("Flushed %v entries", entries)
}
}
util.Schedule(u.processLong, longIntv)
util.Schedule(u.processShort, shortIntv)
util.Schedule(processFlush, flushIntv)
}
func (u *ShiftsProcessor) processLong() {
users, err := u.backend.GetMiners()
if err != nil {
log.Println("Error while retrieving miners from backend:", err)
return
}
shiftsDone := 0
for _, login := range users {
u.backend.WriteLongShift(login)
shiftsDone++
}
log.Printf("%v long shifts created", shiftsDone)
}
func (u *ShiftsProcessor) processShort() {
users, err := u.backend.GetMiners()
if err != nil {
log.Println("Error while retrieving miners from backend:", err)
return
}
shiftsDone := 0
for _, login := range users {
u.backend.WriteShortShift(login)
shiftsDone++
}
log.Printf("%v short shifts created", shiftsDone)
}