-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_scheduler.go
161 lines (145 loc) · 4.55 KB
/
redis_scheduler.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package kafkaservice
import (
"context"
"encoding/json"
"fmt"
"math"
"os"
"os/signal"
"syscall"
"consumer/internal/slack"
rediscache "consumer/pkg/utl/rediscache"
"github.com/gocraft/work"
hashstructure "github.com/mitchellh/hashstructure/v2"
)
// Cont ...
type Cont struct {
}
var GO_KAFKA_EXAMPLE = "go-kafka-example"
var PRUNE_CACHE = "prune_cache"
var SCHEDULED_KAFKA_RETRY = "scheduled_kafka_retry"
var SCHEDULED_CACHE_PRUNE = "scheduled_cache_prune"
var ExponentialBackOffExp int64 = 5
var ExponentialBackOffInterval int64 = 60
var ExponentialBackOffMaxRetry = 5
func getExponentialBackOff(count int) int64 {
count = count - 1
if count < 0 {
return 0
} else if count == 0 {
return ExponentialBackOffInterval
}
exp := math.Pow(float64(ExponentialBackOffExp), float64(count))
return ExponentialBackOffInterval*int64(exp) + getExponentialBackOff(count)
}
func InitScheduledJobs() {
pool := work.NewWorkerPool(Cont{}, 10, GO_KAFKA_EXAMPLE, rediscache.RedisPool)
fmt.Println("-----------InitScheduledJobs---------------")
pool.Job(SCHEDULED_KAFKA_RETRY, KakfaRetryProcessor)
pool.Job(SCHEDULED_CACHE_PRUNE, CachePruningProcessor)
// Start processing jobs
pool.Start()
// Wait for a signal to quit:
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
<-signalChan
// Stop the pool
pool.Stop()
}
func KakfaRetryProcessor(job *work.Job) error {
byteRretryMessage, err := json.Marshal(job.Args["retryMessage"])
if err != nil {
fmt.Println("there was an error while marshaling bro")
}
var retryMessage RetryMessage
err = json.Unmarshal(byteRretryMessage, &retryMessage)
if err != nil {
fmt.Println("there was an error while unmarshaling bro")
}
fmt.Println("\nrepublishing message to kafka ", retryMessage)
Produce(
context.Background(),
KAFKA_TOPIC(retryMessage.Topic),
[]byte(retryMessage.Key),
retryMessage.Message,
)
return err
}
func CachePruningProcessor(job *work.Job) error {
hash, _ := job.Args["hash"].(string)
a, err := rediscache.RemoveKeyValue(hash)
fmt.Println("a,err", a, err)
return nil
}
func ScheduleCachePruning(hash string) {
enqueuer := work.NewEnqueuer(GO_KAFKA_EXAMPLE, rediscache.RedisPool)
secondsFromNow := getExponentialBackOff(ExponentialBackOffMaxRetry + 1)
fmt.Println("ScheduleCachePruning for hash:", hash, " ", secondsFromNow, "seconds from now")
job, err := enqueuer.EnqueueIn(SCHEDULED_CACHE_PRUNE, secondsFromNow, map[string]interface{}{"hash": hash})
if err != nil {
fmt.Println("Error in enqueuing job for cache pruning ", err)
} else {
var name string = "couldn't get name"
if job != nil {
name = job.Name
}
fmt.Println("\n---------------Job enqueued: ", name, "--------------------")
}
}
func ScheduleKafkaRetry(retryMessage RetryMessage) {
hash, err := hashstructure.Hash(map[string]string{
"topic": retryMessage.Topic,
"message": string(retryMessage.Message),
}, hashstructure.FormatV2, nil)
if err != nil {
slack.SendMessage(
fmt.Sprintf("\n error while creating a hash in the ScheduleKafkaRetry function. %s \n", err),
)
}
count := 1
val, err := rediscache.GetKeyValue(fmt.Sprint(hash))
if err != nil {
slack.SendMessage(
fmt.Sprintf("\n error while getting value from redis in the ScheduleKafkaRetry function. %s \n", err),
)
}
if val == nil {
ScheduleCachePruning(fmt.Sprint(hash))
} else {
b := val.([]byte)
err = json.Unmarshal(b, &count)
if err != nil {
slack.SendMessage(
fmt.Sprintf("\n error while unmarshaling count from redis in the ScheduleKafkaRetry function. %s \n", err),
)
}
count = count + 1
}
if count < ExponentialBackOffMaxRetry {
err := rediscache.SetKeyValue(fmt.Sprint(hash), count)
if err != nil {
slack.SendMessage(
fmt.Sprintf("\n error while udpating the count in redis. %s \n", err),
)
}
} else {
// sending items to the dead letter queue
go Produce(context.Background(), DEAD_LETTER_QUEUE, []byte(retryMessage.Key),
retryMessage.Message)
}
enqueuer := work.NewEnqueuer(GO_KAFKA_EXAMPLE, rediscache.RedisPool)
secondsFromNow := getExponentialBackOff(count)
fmt.Println("\nScheduling kafka retry in: ", secondsFromNow, " seconds from now. Topic:",
retryMessage.Topic, "Count: ", count)
job, err := enqueuer.EnqueueIn(SCHEDULED_KAFKA_RETRY, secondsFromNow,
map[string]interface{}{"retryMessage": retryMessage})
if err != nil {
fmt.Println("Error in enqueuing job for kafka retry ", err)
} else {
var name string = "couldn't get name"
if job != nil {
name = job.Name
}
fmt.Println("\n---------------Job enqueued: ", name, "--------------------")
}
}