This repository was archived by the owner on Jan 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.go
147 lines (121 loc) · 2.86 KB
/
service.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
145
146
147
package service
import (
d "osinniy/cryptobot/internal/data"
"osinniy/cryptobot/internal/models"
"osinniy/cryptobot/internal/store"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
const MODULE = "service"
type Service struct {
running bool
updateInterval time.Duration
keepAlive time.Duration
cleanupInterval time.Duration
ticker *time.Ticker
cleanupTicker *time.Ticker
repo store.DataRepository
logger zerolog.Logger
}
// Creates new service with provided interval
func New(repo store.DataRepository, interval time.Duration, keepAlive time.Duration) *Service {
return &Service{
updateInterval: interval,
keepAlive: keepAlive,
cleanupInterval: keepAlive / 100,
repo: repo,
logger: log.With().Str("module", MODULE).Logger(),
}
}
// Starts service with interval.
// Corrects starting time if service was restarted
func (s *Service) Start() {
if s.running {
return
}
s.running = true
// Cleanup job
go func() {
s.cleanup()
s.cleanupTicker = time.NewTicker(s.cleanupInterval)
for range s.cleanupTicker.C {
s.cleanup()
}
}()
// Obtain last run time
data, _ := s.repo.Latest()
// Check last run time and correct first run time if needed
if data != nil {
delay := s.updateInterval - time.Since(time.Unix(data.Upd, 0))
if delay > 0 {
time.Sleep(time.Duration(delay))
}
}
if !s.running {
return
}
// First run
s.run()
if !s.running {
return
}
// Then run task every timer tick
s.ticker = time.NewTicker(s.updateInterval)
for range s.ticker.C {
s.run()
}
}
func (s *Service) Stop() {
if !s.running {
return
}
if s.ticker != nil {
s.ticker.Stop()
s.ticker = nil
}
if s.cleanupTicker != nil {
s.cleanupTicker.Stop()
s.cleanupTicker = nil
}
s.running = false
}
func (s Service) IsRunning() bool {
return s.running
}
// Do single job
func (s *Service) run() {
timestamp := time.Now()
cmc, coinglass, err := s.update()
if err != nil {
s.logger.Error().Dur("elapsed_ms", time.Since(timestamp)).Msg("update failed")
return
}
latestData := models.BuildData(*cmc, *coinglass)
if err := s.repo.Save(latestData); err == nil {
s.logger.Info().Dur("elapsed_ms", time.Since(timestamp)).Msg("data updated")
}
}
// Triggers data endpoints and returns their responses
func (s *Service) update() (cmc *d.CMCMetricsResponse, coinglass *d.CoinglassResponse, err error) {
cmc, err = d.LatestMarketStats()
if err != nil {
return
}
coinglass, err = d.CoinglassStats()
if err != nil {
return
}
return
}
// Toggles [store.DataRepository.Cleanup]
func (s *Service) cleanup() {
affected, err := s.repo.Cleanup(time.Now().Add(-s.keepAlive))
if err != nil {
s.logger.Error().Err(err).Msg("cleanup failed")
return
}
if affected > 0 {
s.logger.Info().Int64("entries_removed", affected).Msgf("cleanup done. next in %s", s.cleanupInterval.String())
}
}