6
0

autopress.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package reservation
  2. import (
  3. utilNsq "dashoo.cn/micro_libary/nsq"
  4. "encoding/json"
  5. "github.com/gogf/gf/frame/g"
  6. "github.com/gogf/gf/os/glog"
  7. "github.com/nsqio/go-nsq"
  8. "time"
  9. )
  10. // AutoSignOut 自动写入签退时间
  11. type AutoSignOut struct {
  12. Id int `json:"id"`
  13. Tenant string `json:"tenant"`
  14. }
  15. func (a *AutoSignOut) HandleMessage(message *nsq.Message) error {
  16. if message.Body != nil {
  17. glog.Info("收到会议室预约的延时消息,内容为:", string(message.Body))
  18. err := json.Unmarshal(message.Body, a)
  19. if err != nil {
  20. glog.Error("nsq body json unmarshal err:", err.Error())
  21. }
  22. if a == nil {
  23. glog.Error("json unmarshal is null")
  24. }
  25. if a.Tenant == "" || a.Id == 0 {
  26. glog.Error("参数错误, 本次操作终止执行")
  27. }
  28. err = NewSrv(a.Tenant).AutoEnding(a.Id)
  29. if err != nil {
  30. glog.Error("auto press error:", err)
  31. }
  32. return nil
  33. }
  34. return nil
  35. }
  36. // ReceiverReservation 延迟消息接收
  37. func ReceiverReservation(topic string) {
  38. // 从配置文件中获取主题
  39. nsqChannel := utilNsq.NsqChannlNameForSrv()
  40. consumer, err := utilNsq.NewConsumerForHandler(topic, "reservation"+nsqChannel, new(AutoSignOut))
  41. if err != nil {
  42. glog.Error("create nsq consumer err:", err.Error())
  43. }
  44. <-consumer.StopChan
  45. }
  46. // AutoProcess 发送延迟消息
  47. func AutoProcess(id int, tenant string, delayTime time.Duration) {
  48. topic := g.Cfg().GetString("nsq.nsqReservationAutoProcess")
  49. expire := AutoSignOut{
  50. Tenant: tenant,
  51. Id: id,
  52. }
  53. msg, err := json.Marshal(expire)
  54. if err != nil {
  55. glog.Error("appoint auto process json marshal err:", err.Error())
  56. }
  57. // 发送延时消息
  58. err = utilNsq.NSqProducer.DeferredPublish(topic, delayTime, msg)
  59. if err != nil {
  60. glog.Error("发送预约类的延时消息失败,err:", err.Error())
  61. }
  62. glog.Info("发送延时消息, 内容为:", string(msg), "延时时间为:", delayTime)
  63. }