-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifier.go
119 lines (111 loc) · 2.95 KB
/
notifier.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
package main
import (
"fmt"
"os"
"strings"
)
type qconfig struct {
QConnectionString string
QName string
}
type webconfig struct {
QueueConfig []qconfig
}
var GlobalConfig webconfig
var TelegramApiUri string
func throw(err error) {
if err != nil {
panic(err)
}
}
func getEnvVars() error {
gramPath := os.Getenv("telegramapipath")
PQConStr := os.Getenv("pqconnectionstringpath")
PQName := os.Getenv("pqname")
PQServerAddress := os.Getenv("pqserveraddress")
SQServerAddress := os.Getenv("sqserveraddress")
SQConStr := os.Getenv("sqconnectionstringpath")
SQName := os.Getenv("sqname")
if gramPath == "" {
return fmt.Errorf("cannot get telegramapipath environment variable")
}
teleBytes, err := os.ReadFile(gramPath)
if err != nil {
return err
}
TelegramApiUri = strings.Split(string(teleBytes), "\n")[0]
if PQConStr == "" {
return fmt.Errorf("cannot get pqconnectionstringpath environment variable")
}
if PQName == "" {
return fmt.Errorf("cannot get pqname environment variable")
}
if PQServerAddress == "" {
return fmt.Errorf("cannot get pqserveraddress environment variable")
}
if SQConStr == "" {
return fmt.Errorf("cannot get sqconnectionstringpath environment variable")
}
if SQServerAddress == "" {
return fmt.Errorf("cannot get sqserveraddress environment variable")
}
if SQName == "" {
return fmt.Errorf("cannot get sqname environment variable")
}
pqpassbytes, err := os.ReadFile(PQConStr)
if err != nil {
return err
}
sqpassbytes, err := os.ReadFile(SQConStr)
if err != nil {
return err
}
pqpass := strings.Split(string(pqpassbytes), "\n")[0]
sqpass := strings.Split(string(sqpassbytes), "\n")[0]
pqconnectionstring := fmt.Sprintf("amqp://%s@%s", pqpass, PQServerAddress)
sqconnectionstring := fmt.Sprintf("amqp://%s@%s", sqpass, SQServerAddress)
q1 := qconfig{QConnectionString: pqconnectionstring, QName: PQName}
q2 := qconfig{QConnectionString: sqconnectionstring, QName: SQName}
qconf := []qconfig{q1, q2}
wconf := webconfig{qconf}
GlobalConfig = wconf
return nil
}
func main() {
err := getEnvVars()
throw(err)
FMP := GetFileManagerDefaultInstance(GlobalConfig.QueueConfig[0])
FMS := GetFileManagerDefaultInstance(GlobalConfig.QueueConfig[1])
myRetrierP := GetRetrierOverloadInstance(FMP.StartReceive)
myRetrierS := GetRetrierOverloadInstance(FMS.StartReceive)
messages, err := myRetrierP.Do()
throw(err)
messagesfromSecondary, err := myRetrierS.Do()
throw(err)
forever := make(chan bool)
go func() {
for message := range messages {
err := FMP.Process(message, FMP.Delay)
if err != nil {
if strings.Contains(fmt.Sprint(err), "cannot acknowledge") {
myRetrierP.Open()
} else {
panic(err)
}
}
}
}()
go func() {
for message := range messagesfromSecondary {
err := FMS.Process(message, FMS.Delay)
if err != nil {
if strings.Contains(fmt.Sprint(err), "cannot acknowledge") {
myRetrierS.Open()
} else {
panic(err)
}
}
}
}()
<-forever
}