-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathredis.go
More file actions
177 lines (155 loc) · 3.82 KB
/
redis.go
File metadata and controls
177 lines (155 loc) · 3.82 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"encoding/json"
"fmt"
"log"
"time"
"github.com/joho/godotenv"
"github.com/redis/go-redis/v9"
)
var rdb *redis.Client
var workerPubSub *redis.PubSub
var intervalTicker *time.Ticker
var intervalDoneChan chan bool
var intervalDuration time.Duration = 1 * time.Second
type RedisServiceCommand struct {
SenderId string `json:"senderId"`
Command string `json:"command"`
Payload string `json:"payload"`
Targets []string `json:"targets"`
};
type RedisWorkerResponse struct {
WorkerId string `json:"workerId"`
Command string `json:"command"`
Payload RedisWorkerResponsePayload `json:"payload"`
};
type RedisWorkerResponsePayload struct {
WorkerId string `json:"workerId"`
RunningJobs []string `json:"runningJobs"`
RunningJobsSummary []ExecutionsCurrentSummary `json:"runningJobsSummary"`
FreeMem float32 `json:"freeMem"`
TotalMem float32 `json:"totalMem"`
Uptime float32 `json:"uptime"`
LoadAvg []float32 `json:"loadAvg"`
Cpus string `json:"cpus"`
Arch string `json:"arch"`
Platform string `json:"platform"`
Hostname string `json:"hostname"`
Net []string `json:"net"`
};
type ExecutionsCurrentSummary struct {
JobId string `json:"jobId"`
ExecutionId string `json:"executionId"`
WorkflowName string `json:"workflowName"`
WorkflowId string `json:"workflowId"`
RetryOf string `json:"retryOf"`
StartedAt string `json:"startedAt"`
Status string `json:"status"`
}
func connect() {
errDotEnv := godotenv.Load()
if errDotEnv != nil {
log.Fatal("Could not load .env file")
}
rdb = redis.NewClient(&redis.Options{
Addr: getEnv("REDIS_HOST", "127.0.0.1")+":"+getEnv("REDIS_PORT", ":6379"),
DialTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
PoolSize: 10,
PoolTimeout: 30 * time.Second,
Username: getEnv("REDIS_USER", ""),
Password: getEnv("REDIS_PASS", ""),
})
workerPubSub = rdb.Subscribe(ctx, getEnv("PUBSUB_WORKER_RESPONSE_CHANNEL", "n8n.worker-response"))
}
func listenToMessages() {
for {
msg, err := workerPubSub.ReceiveMessage(ctx)
if err != nil {
log.Println(err)
}
var m RedisWorkerResponse
json.Unmarshal([]byte(msg.Payload), &m);
switch m.Command {
case "getStatus":
Ui.Send(m)
case "getId":
Ui.Send(m)
}
}
}
func SendGetStatusInterval() {
intervalTicker = time.NewTicker(intervalDuration)
intervalDoneChan = make(chan bool)
go func() {
for {
select {
case <-intervalDoneChan:
return
case <-intervalTicker.C:
SendGetStatus()
}
}
}()
}
func StopInterval() {
intervalTicker.Stop()
intervalDoneChan <- true
}
func IncreaseInterval() {
intervalDuration += 100 * time.Millisecond
ResetInterval()
}
func DecreaseInterval() {
if (intervalDuration > 200*time.Millisecond) {
intervalDuration -= 100 * time.Millisecond
ResetInterval()
}
}
func ResetInterval() {
intervalTicker.Reset(intervalDuration)
}
func SendCommandMessage(msg RedisServiceCommand) {
bytes, marshalErr := json.Marshal(msg)
if marshalErr != nil {
log.Fatal(marshalErr)
}
err := rdb.Publish(ctx, getEnv("PUBSUB_COMMAND_CHANNEL", "n8n.commands"), string(bytes)).Err()
if err != nil {
fmt.Println(err)
log.Fatal("Could not send command message")
}
}
func SendGetId() {
msg := RedisServiceCommand{
SenderId: "workergo",
Command: "getId",
Payload: "",
}
SendCommandMessage(msg);
}
func SendGetStatus() {
msg := RedisServiceCommand{
SenderId: "workergo",
Command: "getStatus",
}
SendCommandMessage(msg);
}
func SendGetStatusByName(target string) {
msg := RedisServiceCommand{
SenderId: "workergo",
Command: "getStatus",
Targets: []string{target},
}
SendCommandMessage(msg);
}
func ConnectToRedis() {
connect()
pong, err := rdb.Ping(ctx).Result()
if err != nil || pong != "PONG" {
log.Fatal("Could not contact Redis db (no pong to my ping)")
log.Fatal(err)
}
go listenToMessages();
}