Files
timerx/cmd/main.go
T

131 lines
2.8 KiB
Go
Raw Normal View History

2023-08-27 23:39:58 +08:00
package main
import (
"context"
"fmt"
"time"
2024-05-08 13:01:55 +08:00
"github.com/yuninks/timerx"
2023-08-27 23:39:58 +08:00
"github.com/go-redis/redis/v8"
)
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()
2023-09-09 23:24:11 +08:00
worker()
2023-08-27 23:39:58 +08:00
}
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",
Password: "", // no password set
DB: 0, // use default DB
})
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")
2023-11-27 22:37:33 +08:00
cl.Add(ctx, "test1", 1*time.Millisecond, aa, "data")
cl.Add(ctx, "test2", 1*time.Millisecond, aa, "data")
cl.Add(ctx, "test3", 1*time.Millisecond, aa, "data")
cl.Add(ctx, "test4", 1*time.Millisecond, aa, "data")
cl.Add(ctx, "test5", 1*time.Millisecond, aa, "data")
cl.Add(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 {
fmt.Println("执行时间:", time.Now().Format("2006-01-02 15:04:05"))
fmt.Println(data)
2023-09-09 23:24:11 +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)
}
}