/
refund.go
87 lines (77 loc) · 2.33 KB
/
refund.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
package jobs
import (
"fmt"
"github.com/go-redsync/redsync"
"github.com/gomodule/redigo/redis"
"github.com/sirupsen/logrus"
"github.com/tietang/go-utils"
"github.com/ybinhome/envelope/core/envelopes"
"github.com/ybinhome/envelope/infra"
"time"
)
type RefundExpiredJobStarter struct {
infra.BaseStarter
ticker *time.Ticker
mutex *redsync.Mutex
}
func (r *RefundExpiredJobStarter) Init(ctx infra.StarterContext) {
d := ctx.Props().GetDurationDefault("jobs.refund.interval", time.Minute)
r.ticker = time.NewTicker(d)
// 使用 redsync 分布式互斥锁 - 1. 构建连接池
maxIdle := ctx.Props().GetIntDefault("redis.maxIdle", 2)
maxActive := ctx.Props().GetIntDefault("redis.maxActive", 5)
idleTimeout := ctx.Props().GetDurationDefault("redis.idleTimeout", 20*time.Second)
addr := ctx.Props().GetDefault("redis.addr", "127.0.0.1:6379")
pools := make([]redsync.Pool, 0)
pool := &redis.Pool{
Dial: func() (conn redis.Conn, err error) {
return redis.Dial("tcp", addr)
},
MaxIdle: maxIdle,
MaxActive: maxActive,
IdleTimeout: idleTimeout,
}
pools = append(pools, pool)
// 使用 redsync 分布式互斥锁 - 2. 创建 redsync 对象
rsync := redsync.New(pools)
// 使用 redsync 分布式互斥锁 - 3. 创建互斥锁
// - 获取节点 ip,用于展示那个节点获得了分布式互斥锁
ip, err := utils.GetExternalIP()
if err != nil {
ip = "127.0.0.1"
}
// - 获取分布式互斥锁
r.mutex = rsync.NewMutex(
// 设置锁名称
"lock:RefundExpired",
// 设置锁过期时间
redsync.SetExpiry(50*time.Second),
// 设置重试次数
redsync.SetRetryDelay(3),
// 设置互斥锁的值,格式为 时间: ip 地址
redsync.SetGenValueFunc(func() (s string, err error) {
now := time.Now()
logrus.Infof("节点 %s 正在执行过期红包退款任务", ip)
return fmt.Sprintf("%d: %s", now.Unix(), ip), nil
}),
)
}
func (r *RefundExpiredJobStarter) Start(ctx infra.StarterContext) {
go func() {
for {
c := <-r.ticker.C
err := r.mutex.Lock()
if err == nil {
logrus.Debug("过期红包退款开始...", c)
// 红包业务逻辑代码
domain := envelopes.ExpiredEnvelopeDomain{}
domain.Expired()
} else {
logrus.Info("已经有节点在运行该任务了")
}
r.mutex.Unlock()
}
}()
}
func (r *RefundExpiredJobStarter) Stop(ctx infra.StarterContext) {
}