forked from nickdu2009/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
smallfish
committed
Oct 19, 2021
1 parent
f2f1881
commit bd85742
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/google/uuid" | ||
"github.com/smallfish-root/common-pkg/xdelay_queue" | ||
"github.com/smallfish-root/common-pkg/xredis" | ||
"time" | ||
) | ||
|
||
func initDelayQueue() { | ||
conf := []*xdelay_queue.DelayQueue{{ | ||
AppConf: xdelay_queue.AppConf{ | ||
BucketSize: 3, | ||
BucketName: "test_delay_queue_7#", | ||
QueueName: "test_queue_7#", | ||
QueueBlockTimeout: 5, | ||
}, | ||
RedisClientConfig: xredis.RedisClientConfig{ | ||
Alias: "campus", | ||
Address: "addr:port", | ||
Password: "password", | ||
DB: 13, | ||
DialTimeout: 5, | ||
ReadTimeout: 5, | ||
WriteTimeout: 5, | ||
IdleTimeout: 120, | ||
}, | ||
}} | ||
xdelay_queue.InitDelayQueue(conf) | ||
} | ||
|
||
func main() { | ||
initDelayQueue() | ||
dq := xdelay_queue.GetDelayQueue("campus") | ||
//go func() { | ||
// for { | ||
//time.Sleep(3*time.Second) | ||
err := dq.AddJob(&xdelay_queue.Job{ | ||
Topic: "^^^", | ||
Id: uuid.NewString(), | ||
Delay: time.Now().Unix() + 20, | ||
TTR: 3, // time to retry | ||
Body: "test delay queue", | ||
}) | ||
if err != nil { | ||
fmt.Println("^^^^ err:", err) | ||
} | ||
|
||
err = dq.AddJob(&xdelay_queue.Job{ | ||
Topic: "***", | ||
Id: uuid.NewString(), | ||
Delay: time.Now().Unix() + 10, | ||
TTR: 3, | ||
Body: "test delay queue-----", | ||
}) | ||
if err != nil { | ||
fmt.Println("**** err:", err) | ||
} | ||
// } | ||
//}() | ||
|
||
for { | ||
job, err := dq.GetJob([]string{"^^^", "***"}) | ||
if err != nil { | ||
fmt.Println("get job fail:", err) | ||
break | ||
} | ||
if job == nil { | ||
fmt.Println("------------------") | ||
continue | ||
} | ||
|
||
_ = dq.DeleteJob(job.Id) // prevent repeated to exec, avoid invalid execution multiple times!!! be commented out for multi times to exec | ||
fmt.Println("body:", job.Body) | ||
} | ||
} | ||
|