Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

utility_functions.go 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package functions
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "time"
  6. "git.x2erp.com/qdy/go-base/types"
  7. "git.x2erp.com/qdy/go-db/factory/rabbitmq"
  8. )
  9. // HealthCheck RabbitMQ健康检查
  10. func HealthCheck(w http.ResponseWriter, r *http.Request, rabbitFactory *rabbitmq.RabbitMQFactory) {
  11. w.Header().Set("Content-Type", "application/json")
  12. // 尝试创建测试通道
  13. _, err := rabbitFactory.CreateChannel("health_check")
  14. if err != nil {
  15. w.WriteHeader(http.StatusServiceUnavailable)
  16. json.NewEncoder(w).Encode(map[string]interface{}{
  17. "status": "down",
  18. "error": err.Error(),
  19. "time": time.Now().Format(time.RFC3339),
  20. })
  21. return
  22. }
  23. // 清理测试通道
  24. rabbitFactory.CloseChannel("health_check")
  25. w.WriteHeader(http.StatusOK)
  26. json.NewEncoder(w).Encode(map[string]interface{}{
  27. "status": "up",
  28. "time": time.Now().Format(time.RFC3339),
  29. })
  30. }
  31. // GetQueueInfo 获取队列信息
  32. func GetQueueInfo(rabbitFactory *rabbitmq.RabbitMQFactory, req types.QueueInfoRequest) *types.QueryResult {
  33. // 设置默认值
  34. if req.ChannelName == "" {
  35. req.ChannelName = "default"
  36. }
  37. // 获取通道
  38. channel, err := rabbitFactory.GetChannel(req.ChannelName)
  39. if err != nil {
  40. return &types.QueryResult{
  41. Success: false,
  42. Error: "Failed to get channel: " + err.Error(),
  43. Time: time.Now().Format(time.RFC3339),
  44. }
  45. }
  46. // 获取队列信息
  47. myQueue, err := channel.QueueInspect(req.QueueName)
  48. if err != nil {
  49. return &types.QueryResult{
  50. Success: false,
  51. Error: "Failed to inspect queue: " + err.Error(),
  52. Time: time.Now().Format(time.RFC3339),
  53. }
  54. }
  55. return &types.QueryResult{
  56. Success: true,
  57. Time: time.Now().Format(time.RFC3339),
  58. Data: map[string]interface{}{
  59. "Consumers": myQueue.Consumers,
  60. "Messages": myQueue.Messages,
  61. "Name": myQueue.Name,
  62. },
  63. }
  64. }