Files
timerx/cmd/main.go
T

290 lines
6.8 KiB
Go
Raw Normal View History

2023-08-27 23:39:58 +08:00
package main
import (
"context"
"fmt"
2025-09-18 15:34:01 +08:00
"os"
2023-08-27 23:39:58 +08:00
"time"
2025-10-04 22:00:08 +08:00
"github.com/redis/go-redis/v9"
2025-10-05 16:30:41 +08:00
"github.com/robfig/cron/v3"
2024-05-22 15:02:39 +08:00
"github.com/yuninks/timerx"
2025-08-28 17:45:17 +08:00
"github.com/yuninks/timerx/priority"
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()
2025-10-04 21:33:57 +08:00
cluster()
// once()
2025-09-19 18:52:33 +08:00
// prioritys()
2024-05-22 15:02:39 +08:00
select {}
2023-08-27 23:39:58 +08:00
}
2025-09-18 15:34:01 +08:00
func prioritys() {
client := getRedis()
ctx := context.Background()
2025-09-24 17:26:33 +08:00
pro, _ := priority.InitPriority(ctx, client, "test", 10)
2025-09-18 15:34:01 +08:00
for {
b := pro.IsLatest(ctx)
fmt.Println("isLatest", b)
2025-09-19 18:52:33 +08:00
time.Sleep(time.Millisecond * 100)
2025-09-18 15:34:01 +08:00
}
}
2024-10-09 17:03:54 +08:00
func once() {
client := getRedis()
ctx := context.Background()
w := OnceWorker{}
2025-08-28 17:45:17 +08:00
ver, err := priority.PriorityByVersion("v2.2.3.4.5")
if err != nil {
panic(err)
}
ops := []timerx.Option{
2025-09-24 17:26:33 +08:00
timerx.WithPriority(ver),
2025-08-28 17:45:17 +08:00
}
2025-09-24 17:26:33 +08:00
one, err := timerx.InitOnce(ctx, client, "test_once", w, ops...)
if err != nil {
panic(err)
}
2024-10-09 17:03:54 +08:00
2024-10-09 19:37:13 +08:00
d := OnceData{
2024-10-11 16:17:22 +08:00
Num: 3,
2024-10-09 19:37:13 +08:00
}
2024-10-11 16:17:22 +08:00
// dy, _ := json.Marshal(d)
2024-10-09 19:37:13 +08:00
2025-08-28 17:45:17 +08:00
err = one.Create("test", "test3", 1*time.Second, d)
2024-10-11 16:17:22 +08:00
if err != nil {
fmt.Println(err)
}
// d = OnceData{
// Num: 4,
// }
2025-09-24 17:26:33 +08:00
// dd := 123
2024-10-11 16:17:22 +08:00
// dy, _ = json.Marshal(d)
2025-09-24 17:26:33 +08:00
// err = one.Save("test", "test4", 2*time.Second, dd)
// if err != nil {
// fmt.Println(err)
// }
2025-08-28 17:45:17 +08:00
2025-09-24 17:26:33 +08:00
// err = one.Save("test", "test5", 5*time.Second, dd)
// if err != nil {
// fmt.Println(err)
// }
2024-10-09 17:03:54 +08:00
}
2024-10-09 19:37:13 +08:00
type OnceData struct {
Num int
}
2024-10-09 17:03:54 +08:00
type OnceWorker struct{}
2025-08-27 15:52:09 +08:00
func (l OnceWorker) Worker(ctx context.Context, taskType timerx.OnceTaskType, taskId string, attachData interface{}) *timerx.OnceWorkerResp {
2025-09-24 17:26:33 +08:00
// 追加写入文件
2025-10-05 16:30:41 +08:00
file, err := os.OpenFile("./test3.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
2025-09-24 17:26:33 +08:00
if err != nil {
panic(err)
}
defer file.Close()
file.WriteString(fmt.Sprintf("执行时间:%s\n", time.Now().Format("2006-01-02 15:04:05")))
2024-10-09 17:03:54 +08:00
fmt.Println("执行时间:", time.Now().Format("2006-01-02 15:04:05"))
2025-09-24 17:53:28 +08:00
// fmt.Println(taskType, taskId)
2024-10-11 16:17:22 +08:00
2025-09-24 17:53:28 +08:00
// fmt.Printf("原来的参数:%+v %T\n", attachData, attachData)
2025-09-24 17:53:28 +08:00
// v, ok := attachData.(int64)
// fmt.Println("vvvvvvv", v, ok)
// fmt.Printf()
2024-10-09 19:37:13 +08:00
2024-10-11 16:17:22 +08:00
// d := OnceData{}
2024-10-09 19:37:13 +08:00
2024-10-11 16:17:22 +08:00
// json.Unmarshal(ab, &d)
2024-10-09 19:37:13 +08:00
2024-10-11 16:17:22 +08:00
// d.Num++
2024-10-09 19:37:13 +08:00
2024-10-11 16:17:22 +08:00
// fmt.Println(d)
2024-10-09 19:37:13 +08:00
2024-10-11 16:17:22 +08:00
// dy, _ := json.Marshal(d)
2024-10-10 10:05:28 +08:00
2024-10-09 19:37:13 +08:00
return &timerx.OnceWorkerResp{
Retry: true,
2024-10-11 16:17:22 +08:00
AttachData: attachData,
2025-09-24 17:26:33 +08:00
DelayTime: time.Second,
2024-10-09 19:37:13 +08:00
}
2024-10-09 17:03:54 +08:00
}
2024-05-22 15:02:39 +08:00
func cluster() {
client := getRedis()
ctx := context.Background()
2025-09-19 18:52:33 +08:00
2025-09-23 11:20:38 +08:00
// log := loggerx.NewLogger(ctx,loggerx.SetToConsole(),loggerx.SetEscapeHTML(false))
// _ = log
2025-09-19 18:52:33 +08:00
2025-10-05 16:30:41 +08:00
cluster, _ := timerx.InitCluster(ctx, client, "test2", timerx.WithPriority(104))
2025-09-19 18:52:33 +08:00
err := cluster.EverySpace(ctx, "test_space1", 1*time.Second, aa, "这是秒任务1")
fmt.Println(err)
err = cluster.EverySpace(ctx, "test_space2", 2*time.Second, aa, "这是秒任务2")
fmt.Println(err)
err = cluster.EverySpace(ctx, "test_space3", 5*time.Second, aa, "这是秒任务3")
fmt.Println(err)
err = cluster.EveryMinute(ctx, "test_min1", 15, aa, "这是分钟任务1")
2024-05-22 15:02:39 +08:00
fmt.Println(err)
2025-09-19 18:52:33 +08:00
err = cluster.EveryMinute(ctx, "test_min2", 30, aa, "这是分钟任务2")
fmt.Println(err)
err = cluster.EveryHour(ctx, "test_hour1", 30, 0, aa, "这是小时任务1")
fmt.Println(err)
err = cluster.EveryHour(ctx, "test_hour2", 30, 15, aa, "这是小时任务2")
fmt.Println(err)
err = cluster.EveryDay(ctx, "test_day1", 5, 0, 0, aa, "这是天任务1")
2024-05-22 15:02:39 +08:00
fmt.Println(err)
2025-09-19 18:52:33 +08:00
err = cluster.EveryDay(ctx, "test_day2", 9, 20, 0, aa, "这是天任务2")
2024-05-22 15:02:39 +08:00
fmt.Println(err)
2025-09-19 18:52:33 +08:00
err = cluster.EveryDay(ctx, "test_day3", 10, 30, 30, aa, "这是天任务3")
2024-05-22 15:02:39 +08:00
fmt.Println(err)
2025-10-05 16:30:41 +08:00
// 默认秒级表达式
err = cluster.Cron(ctx, "test_cron1", "*/5 * * * * ?", aa, "这是cron任务1")
fmt.Println(err)
err = cluster.Cron(ctx, "test_cron2", "0/5 * * * * ?", aa, "这是cron任务2")
fmt.Println("这是cron任务2:", err)
// 自定义解析器
err = cluster.Cron(ctx, "test_cron3", "@every 2s", aa, "这是cron任务3", timerx.WithCronParserOption(cron.Descriptor))
fmt.Println("这是cron任务3:", err)
// Linux标准解析器
err = cluster.Cron(ctx, "test_cron4", "*/5 * * * *", aa, "这是cron任务4", timerx.WithCronParserLinux())
fmt.Println("这是cron任务4:", err)
2024-05-22 15:02:39 +08:00
}
2023-09-09 23:24:11 +08:00
func worker() {
2024-10-10 10:05:28 +08:00
// client := getRedis()
// w := timerx.InitOnce(context.Background(), client, "test", &OnceWorker{})
// w.Save("test", "test", 1*time.Second, map[string]interface{}{
// "test": "test",
// })
// w.Save("test2", "test", 1*time.Second, map[string]interface{}{
// "test": "test",
// })
// w.Save("test3", "test", 1*time.Second, map[string]interface{}{
// "test": "test",
// })
// w.Save("test4", "test", 1*time.Second, map[string]interface{}{
// "test": "test",
// })
// w.Save("test5", "test", 1*time.Second, map[string]interface{}{
// "test": "test",
// })
2023-09-09 23:24:11 +08:00
select {}
}
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()
2025-09-24 17:26:33 +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 {
2025-09-18 15:34:01 +08:00
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)
2025-09-18 15:34:01 +08:00
// 追加到文件
file, err := os.OpenFile("./test.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("打开文件失败:", err)
return err
}
defer file.Close()
_, err = file.WriteString(fmt.Sprintf("-执行时间:%v %s\n", data, time.Now().Format("2006-01-02 15:04:05")))
if err != nil {
fmt.Println("写入文件失败:", err)
return err
}
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
}
2025-10-04 22:00:08 +08:00
client.ZAdd(context.Background(), "lockx:test2", redis.Z{
2023-09-02 13:32:04 +08:00
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)
}
}