-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
127 lines (106 loc) · 2.91 KB
/
utils.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
package swissknife
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"os/signal"
"strconv"
"syscall"
"time"
simplecron "github.com/sagleft/simple-cron"
"gopkg.in/yaml.v3"
)
// Ternary operator. conditional operator
// usage example: var res = ternary(val > 0, "positive", "negative")
func Ternary(statement bool, a, b interface{}) interface{} {
if statement {
return a
}
return b
}
// RunInBackground - blocking method with no exit
func RunInBackground() {
forever := make(chan bool)
// background work
<-forever
}
func WaitForAppFinish() {
exit := make(chan os.Signal, 1)
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)
<-exit
fmt.Println()
}
// ConnFunc - some func
type ConnFunc func() error
type ReconnectTask struct {
ConnectionDescription string // connection name
ReconnectionAttemptsNumber int
ConnectionTimeout time.Duration
ReconnectAfterTimeout time.Duration
WaitingBetweenAttempts time.Duration
// callbacks
ConnCallback ConnFunc
ErrorCallback func(error)
LogCallback func(string)
}
// Reconnect - setup reconnect
func Reconnect(task ReconnectTask) {
isConnected := false
for !isConnected {
for i := 0; i < task.ReconnectionAttemptsNumber; i++ {
isTimeIsUP := simplecron.NewRuntimeLimitHandler(
task.ConnectionTimeout*time.Second,
func() {
err := task.ConnCallback()
if err == nil {
// connection established
isConnected = true
return
}
task.LogCallback(err.Error())
time.Sleep(task.ReconnectAfterTimeout)
},
).Run()
if isTimeIsUP {
task.LogCallback(task.ConnectionDescription + " connection went into timeout")
}
}
if isConnected {
task.LogCallback("`" + task.ConnectionDescription + "` connection established")
return
}
task.ErrorCallback(errors.New("failed to connect to " +
task.ConnectionDescription + " after " +
strconv.Itoa(task.ReconnectionAttemptsNumber) + " attempts"))
task.LogCallback("wait " + task.WaitingBetweenAttempts.String() + " between attempts...")
time.Sleep(task.WaitingBetweenAttempts)
}
}
func ParseStructFromJSON(jsonBytes []byte, destinationPointer interface{}) error {
return json.Unmarshal(jsonBytes, destinationPointer)
}
func ParseStructFromYaml(jsonBytes []byte, destinationPointer interface{}) error {
return yaml.Unmarshal(jsonBytes, destinationPointer)
}
func ParseStructFromJSONFile(filepath string, destinationPointer interface{}) error {
dataBytes, err := ReadFileToBytes(filepath)
if err != nil {
return err
}
return ParseStructFromJSON(dataBytes, destinationPointer)
}
func ParseStructFromYamlFile(filepath string, destinationPointer interface{}) error {
dataBytes, err := ReadFileToBytes(filepath)
if err != nil {
return err
}
return ParseStructFromYaml(dataBytes, destinationPointer)
}
// MD5 - calc MD5 checksum
func MD5(val []byte) string {
hash := md5.Sum(val)
return hex.EncodeToString(hash[:])
}