项目作者: changsongl

项目描述 :
delay-queue client in Golang. It is the SDK written in Golang for 有赞 delay queue.
高级语言: Go
项目地址: git://github.com/changsongl/delay-queue-client.git
创建时间: 2021-01-16T08:23:48Z
项目社区:https://github.com/changsongl/delay-queue-client

开源协议:MIT License

下载


delay-queue-client

delay-queue client in Golang. It is the SDK written in Golang for https://github.com/changsongl/delay-queue

How to use?

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/changsongl/delay-queue-client/client"
  5. "github.com/changsongl/delay-queue-client/consumer"
  6. "github.com/changsongl/delay-queue-client/job"
  7. "time"
  8. )
  9. func main() {
  10. // job object
  11. myTopic, myID := "my-topic", "my-id"
  12. j, err := job.New(myTopic, myID, job.DelayOption(2*time.Second), job.TTROption(30*time.Second))
  13. if err != nil {
  14. panic(err)
  15. }
  16. // client
  17. cli := client.NewClient("127.0.0.1:8000")
  18. // adding job to delay queue, if job is exist will be failed
  19. if err = cli.AddJob(j); err != nil {
  20. panic(err)
  21. }
  22. // replace the job, even if the job is exists
  23. if err = cli.ReplaceJob(j); err != nil {
  24. panic(err)
  25. }
  26. // delete the job
  27. if err = cli.DeleteJob(myTopic, myID); err != nil {
  28. panic(err)
  29. }
  30. // pop the job from queue, no recommended. please use consumer.
  31. topic, id, body, delay, ttr, err := cli.PopJob(myTopic, 3*time.Second)
  32. if err != nil {
  33. panic(err)
  34. }
  35. fmt.Println(topic, id, body, delay, ttr)
  36. // finish the job, after having processed the job
  37. if err = cli.FinishJob(myTopic, myID); err != nil {
  38. panic(err)
  39. }
  40. // consumer jobs
  41. c := consumer.New(
  42. cli,
  43. topic,
  44. consumer.WorkerNumOption(1),
  45. consumer.PopTimeoutOption(3*time.Second),
  46. )
  47. ch := c.Consume()
  48. for jobMsg := range ch {
  49. id := jobMsg.GetID()
  50. body := jobMsg.GetBody()
  51. // do your job
  52. fmt.Println(id, body)
  53. if id == "xxx" {
  54. // job is not valid anymore
  55. if err = jobMsg.Finish(); err != nil {
  56. // do something
  57. }
  58. continue
  59. }
  60. if err = jobMsg.Finish(); err != nil {
  61. // do something
  62. }
  63. }
  64. }