-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
58 lines (46 loc) · 1.31 KB
/
main.go
File metadata and controls
58 lines (46 loc) · 1.31 KB
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
package main
import (
"con-currency/config"
"con-currency/db"
"con-currency/exchangerate"
"con-currency/service"
logger "github.com/sirupsen/logrus"
"time"
)
func main() {
// Initialize configurations
err := config.Init("config")
if err != nil {
logger.WithField("error in config file", err.Error()).Error("Exit")
return
}
interval := config.GetInt("time_interval")
for {
start := time.Now()
fetchCurrency()
elapsed := time.Since(start)
logger.WithField("info:", elapsed).Info("Execution time")
elapsedSeconds := elapsed.Round(time.Second).Seconds()
remaining := float64(interval) - elapsedSeconds
logger.WithField("info: ", remaining).Info("Sleeping")
time.Sleep(time.Duration(remaining) * time.Second)
logger.Info("Waking up")
}
}
func fetchCurrency() {
converter := exchangerate.New() // will ret interface
storer, err := db.Init() // will ret interface
if err != nil {
logger.WithField("err", err.Error()).Error("Cannot connect database")
return
}
// close database connection
defer storer.Close()
if err = storer.CreateTableIfMissing(); err != nil {
logger.WithField("err", err.Error()).Error("Cannot create table")
}
currencies := config.GetStringSlice("currency_list")
logger.Info(currencies)
// Starting the process
service.StartProcess(currencies, converter, storer)
}