-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprocessor.go
More file actions
73 lines (65 loc) · 2.01 KB
/
Copy pathprocessor.go
File metadata and controls
73 lines (65 loc) · 2.01 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
package main
import (
"fmt"
"time"
"github.com/go-redis/redis"
)
// This processes the scheduled jobs from redis set
func processor(id int) {
fmt.Println("[INFO] Starting prcessor", id)
for {
job := fetchJob()
if job.Member == nil {
fmt.Println("Routine:", id, "Waiting for a job")
continue
}
fmt.Println("Routine:", id, "Current time:", currentTime())
fmt.Println("Time:", job.Score, " Job:", job.Member)
if int64(job.Score)-currentTime() > maxTimeWindow {
fmt.Println("Rescheduling job ...")
reScheduleJob(job.Score, fmt.Sprintf("%v", job.Member))
time.Sleep(2 * time.Second)
} else {
for {
if int64(job.Score)-currentTime() <= 0 {
break
}
time.Sleep(500 * time.Millisecond)
}
fmt.Println("Routine:", id, "Current time:", currentTime())
runJob(job)
}
}
}
// This is a feedback process to check for missed scheduled jobs
func feedBack() {
for {
for _, jobWithScore := range findJobByTime(float64(currentTime() - 10)) {
fmt.Println("[ERROR] Time :", jobWithScore.Score, " Job : ", jobWithScore.Member)
reScheduleJob(jobWithScore.Score, fmt.Sprintf("%v", jobWithScore.Member))
}
fmt.Println("Feedback polling routing running")
time.Sleep(5 * time.Second)
}
}
// Add code related to processing your job here
func runJob(job redis.ZWithKey) {
fmt.Println("Running job :: Time:", job.Score, " Job:", job.Member)
//Remove from backup set
removeJobFromRedis(backupRedisKey, fmt.Sprintf("%v", job.Member))
msgCh <- "JOB SCHEDULED" //fmt.Sprint("Running job :: Time:", job.Score, " Job:", job.Member)
}
// Adds job to main set
func reScheduleJob(timestamp float64, jobName string) {
addJobToRedis(scheduleRedisKey, timestamp, jobName)
}
// Add jobs to both sets
func addJob(timestamp float64, jobName string) {
addJobToRedis(scheduleRedisKey, timestamp, jobName)
addJobToRedis(backupRedisKey, timestamp, jobName)
}
// Remove jobs from both sets
func removeJob(jobName string) {
removeJobFromRedis(scheduleRedisKey, jobName)
removeJobFromRedis(backupRedisKey, jobName)
}