Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utility_functions.go 1.8KB

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