2023-08-27 23:39:58 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/go-redis/redis/v8"
|
2024-05-22 15:02:39 +08:00
|
|
|
"github.com/yuninks/timerx"
|
2023-08-27 23:39:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
// m := make(map[string]time.Time)
|
|
|
|
|
// m["sss"] = time.Now()
|
|
|
|
|
|
|
|
|
|
// b, _ := json.Marshal(m)
|
|
|
|
|
|
|
|
|
|
// fmt.Println(string(b))
|
|
|
|
|
|
|
|
|
|
// mm := make(map[string]time.Time)
|
|
|
|
|
// json.Unmarshal(b, &mm)
|
|
|
|
|
|
|
|
|
|
// fmt.Println(mm)
|
|
|
|
|
|
2023-09-09 23:24:11 +08:00
|
|
|
// re()
|
2023-09-02 13:32:04 +08:00
|
|
|
// d()
|
2024-05-22 15:02:39 +08:00
|
|
|
cluster()
|
|
|
|
|
|
|
|
|
|
select {}
|
2023-08-27 23:39:58 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-22 15:02:39 +08:00
|
|
|
func cluster() {
|
|
|
|
|
client := getRedis()
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
cluster := timerx.InitCluster(ctx, client, "test")
|
|
|
|
|
err := cluster.AddSpace(ctx, "test_space", 1*time.Second, aa, "这是秒任务")
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
err = cluster.AddMinute(ctx, "test_min", 15, aa, "这是分钟任务")
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
err = cluster.AddHour(ctx, "test_hour", 30, 0, aa, "这是小时任务")
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
err = cluster.AddDay(ctx, "test_day", 11, 0, 0, aa, "这是天任务")
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-09 23:24:11 +08:00
|
|
|
func worker() {
|
|
|
|
|
client := getRedis()
|
2024-04-04 10:58:57 +08:00
|
|
|
w := timerx.InitOnce(context.Background(), client, "test", &Worker{})
|
2023-09-09 23:32:37 +08:00
|
|
|
w.Add("test", "test", 1*time.Second, map[string]interface{}{
|
2023-09-09 23:24:11 +08:00
|
|
|
"test": "test",
|
|
|
|
|
})
|
2023-09-09 23:32:37 +08:00
|
|
|
w.Add("test2", "test", 1*time.Second, map[string]interface{}{
|
2023-09-09 23:24:11 +08:00
|
|
|
"test": "test",
|
|
|
|
|
})
|
2023-09-09 23:32:37 +08:00
|
|
|
w.Add("test3", "test", 1*time.Second, map[string]interface{}{
|
2023-09-09 23:24:11 +08:00
|
|
|
"test": "test",
|
|
|
|
|
})
|
2023-09-09 23:32:37 +08:00
|
|
|
w.Add("test4", "test", 1*time.Second, map[string]interface{}{
|
2023-09-09 23:24:11 +08:00
|
|
|
"test": "test",
|
|
|
|
|
})
|
2023-09-09 23:32:37 +08:00
|
|
|
w.Add("test5", "test", 1*time.Second, map[string]interface{}{
|
2023-09-09 23:24:11 +08:00
|
|
|
"test": "test",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
select {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Worker struct{}
|
|
|
|
|
|
2024-04-04 10:58:57 +08:00
|
|
|
func (w *Worker) Worker(jobType string, uniqueKey string, data interface{}) (timerx.WorkerCode, time.Duration) {
|
2023-09-09 23:24:11 +08:00
|
|
|
fmt.Println("执行时间:", time.Now().Format("2006-01-02 15:04:05"))
|
|
|
|
|
fmt.Println(uniqueKey, jobType)
|
|
|
|
|
fmt.Println(data)
|
2024-04-04 10:58:57 +08:00
|
|
|
return timerx.WorkerCodeAgain, time.Second
|
2023-09-09 23:24:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getRedis() *redis.Client {
|
2023-08-27 23:39:58 +08:00
|
|
|
client := redis.NewClient(&redis.Options{
|
|
|
|
|
Addr: "127.0.0.1" + ":" + "6379",
|
2024-05-28 15:02:57 +08:00
|
|
|
Password: "123456", // no password set
|
|
|
|
|
DB: 0, // use default DB
|
2023-08-27 23:39:58 +08:00
|
|
|
})
|
|
|
|
|
if client == nil {
|
2023-09-09 23:24:11 +08:00
|
|
|
panic("redis init error")
|
2023-08-27 23:39:58 +08:00
|
|
|
}
|
2023-09-09 23:24:11 +08:00
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func re() {
|
|
|
|
|
|
|
|
|
|
client := getRedis()
|
2023-08-27 23:39:58 +08:00
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2024-04-04 10:58:57 +08:00
|
|
|
cl := timerx.InitCluster(ctx, client, "kkkk")
|
2024-05-20 09:35:12 +08:00
|
|
|
cl.AddSpace(ctx, "test1", 1*time.Millisecond, aa, "data")
|
|
|
|
|
cl.AddSpace(ctx, "test2", 1*time.Millisecond, aa, "data")
|
|
|
|
|
cl.AddSpace(ctx, "test3", 1*time.Millisecond, aa, "data")
|
|
|
|
|
cl.AddSpace(ctx, "test4", 1*time.Millisecond, aa, "data")
|
|
|
|
|
cl.AddSpace(ctx, "test5", 1*time.Millisecond, aa, "data")
|
|
|
|
|
cl.AddSpace(ctx, "test6", 1*time.Millisecond, aa, "data")
|
2023-08-27 23:39:58 +08:00
|
|
|
|
|
|
|
|
select {}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-27 22:37:33 +08:00
|
|
|
func aa(ctx context.Context, data interface{}) error {
|
2024-05-28 15:02:57 +08:00
|
|
|
fmt.Println("-执行时间:", data, time.Now().Format("2006-01-02 15:04:05"))
|
|
|
|
|
// fmt.Println(data)
|
2024-05-22 15:02:39 +08:00
|
|
|
// time.Sleep(time.Second * 5)
|
2023-11-27 22:37:33 +08:00
|
|
|
return nil
|
2023-08-27 23:39:58 +08:00
|
|
|
}
|
2023-09-02 13:32:04 +08:00
|
|
|
|
|
|
|
|
func d() {
|
|
|
|
|
|
|
|
|
|
client := redis.NewClient(&redis.Options{
|
|
|
|
|
Addr: "127.0.0.1" + ":" + "6379",
|
|
|
|
|
Password: "", // no password set
|
|
|
|
|
DB: 0, // use default DB
|
|
|
|
|
})
|
|
|
|
|
if client == nil {
|
|
|
|
|
fmt.Println("redis init error")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client.ZAdd(context.Background(), "lockx:test2", &redis.Z{
|
|
|
|
|
Score: 50,
|
|
|
|
|
Member: "test",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
script := `
|
|
|
|
|
local token = redis.call('zrangebyscore',KEYS[1],ARGV[1],ARGV[2])
|
|
|
|
|
for i,v in ipairs(token) do
|
|
|
|
|
redis.call('zrem',KEYS[1],v)
|
|
|
|
|
redis.call('lpush',KEYS[2],v)
|
|
|
|
|
end
|
|
|
|
|
return "OK"
|
|
|
|
|
`
|
|
|
|
|
res, err := client.Eval(context.Background(), script, []string{"lockx:test2", "lockx:push"}, 0, 100).Result()
|
|
|
|
|
fmt.Println(res, err)
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
|
l, e := client.RPop(context.Background(), "lockx:push").Result()
|
|
|
|
|
fmt.Println(l, e)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|