Files
timerx/cmd/main.go
T

170 lines
4.0 KiB
Go
Raw Normal View History

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-10-09 17:03:54 +08:00
// cluster()
once()
2024-05-22 15:02:39 +08:00
select {}
2023-08-27 23:39:58 +08:00
}
2024-10-09 17:03:54 +08:00
func once() {
client := getRedis()
ctx := context.Background()
w := OnceWorker{}
one := timerx.InitOnce(ctx, client, "test", w)
err := one.Save("test", "test", 1*time.Second, map[string]interface{}{})
if err != nil {
fmt.Println(err)
}
}
type OnceWorker struct{}
func (l OnceWorker) Worker(ctx context.Context, taskType string, taskId string, attachData interface{}) (timerx.WorkerCode, time.Duration) {
fmt.Println("执行时间:", time.Now().Format("2006-01-02 15:04:05"))
fmt.Println(taskId, taskType)
fmt.Println(attachData)
return timerx.WorkerCodeAgain, time.Millisecond
}
2024-05-22 15:02:39 +08:00
func cluster() {
client := getRedis()
ctx := context.Background()
cluster := timerx.InitCluster(ctx, client, "test")
2024-05-31 13:55:05 +08:00
err := cluster.EverySpace(ctx, "test_space", 1*time.Second, aa, "这是秒任务")
2024-05-22 15:02:39 +08:00
fmt.Println(err)
2024-05-31 13:55:05 +08:00
err = cluster.EveryMinute(ctx, "test_min", 15, aa, "这是分钟任务")
2024-05-22 15:02:39 +08:00
fmt.Println(err)
2024-05-31 13:55:05 +08:00
err = cluster.EveryHour(ctx, "test_hour", 30, 0, aa, "这是小时任务")
2024-05-22 15:02:39 +08:00
fmt.Println(err)
2024-05-31 13:55:05 +08:00
err = cluster.EveryDay(ctx, "test_day", 11, 0, 0, aa, "这是天任务")
2024-05-22 15:02:39 +08:00
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{})
2024-07-02 21:18:16 +08:00
w.Save("test", "test", 1*time.Second, map[string]interface{}{
2023-09-09 23:24:11 +08:00
"test": "test",
})
2024-07-02 21:18:16 +08:00
w.Save("test2", "test", 1*time.Second, map[string]interface{}{
2023-09-09 23:24:11 +08:00
"test": "test",
})
2024-07-02 21:18:16 +08:00
w.Save("test3", "test", 1*time.Second, map[string]interface{}{
2023-09-09 23:24:11 +08:00
"test": "test",
})
2024-07-02 21:18:16 +08:00
w.Save("test4", "test", 1*time.Second, map[string]interface{}{
2023-09-09 23:24:11 +08:00
"test": "test",
})
2024-07-02 21:18:16 +08:00
w.Save("test5", "test", 1*time.Second, map[string]interface{}{
2023-09-09 23:24:11 +08:00
"test": "test",
})
select {}
}
type Worker struct{}
2024-05-28 17:28:20 +08:00
func (w *Worker) Worker(ctx context.Context, 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-31 13:55:05 +08:00
cl.EverySpace(ctx, "test1", 1*time.Millisecond, aa, "data")
cl.EverySpace(ctx, "test2", 1*time.Millisecond, aa, "data")
cl.EverySpace(ctx, "test3", 1*time.Millisecond, aa, "data")
cl.EverySpace(ctx, "test4", 1*time.Millisecond, aa, "data")
cl.EverySpace(ctx, "test5", 1*time.Millisecond, aa, "data")
cl.EverySpace(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)
}
}