6
0

autopress.go 1.9 KB

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