server.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package manager
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. . "github.com/tuna/tunasync/internal"
  7. )
  8. const (
  9. _errorKey = "error"
  10. _infoKey = "message"
  11. )
  12. type managerServer struct {
  13. *gin.Engine
  14. adapter dbAdapter
  15. }
  16. // listAllJobs repond with all jobs of specified workers
  17. func (s *managerServer) listAllJobs(c *gin.Context) {
  18. mirrorStatusList, err := s.adapter.ListAllMirrorStatus()
  19. if err != nil {
  20. err := fmt.Errorf("failed to list all mirror status: %s",
  21. err.Error(),
  22. )
  23. c.Error(err)
  24. s.returnErrJSON(c, http.StatusInternalServerError, err)
  25. return
  26. }
  27. c.JSON(http.StatusOK, mirrorStatusList)
  28. }
  29. // listWrokers respond with informations of all the workers
  30. func (s *managerServer) listWorkers(c *gin.Context) {
  31. var workerInfos []WorkerInfoMsg
  32. workers, err := s.adapter.ListWorkers()
  33. if err != nil {
  34. err := fmt.Errorf("failed to list workers: %s",
  35. err.Error(),
  36. )
  37. c.Error(err)
  38. s.returnErrJSON(c, http.StatusInternalServerError, err)
  39. return
  40. }
  41. for _, w := range workers {
  42. workerInfos = append(workerInfos,
  43. WorkerInfoMsg{w.ID, w.LastOnline})
  44. }
  45. c.JSON(http.StatusOK, workerInfos)
  46. }
  47. // registerWorker register an newly-online worker
  48. func (s *managerServer) registerWorker(c *gin.Context) {
  49. var _worker workerStatus
  50. c.BindJSON(&_worker)
  51. newWorker, err := s.adapter.CreateWorker(_worker)
  52. if err != nil {
  53. err := fmt.Errorf("failed to register worker: %s",
  54. err.Error(),
  55. )
  56. c.Error(err)
  57. s.returnErrJSON(c, http.StatusInternalServerError, err)
  58. return
  59. }
  60. // create workerCmd channel for this worker
  61. c.JSON(http.StatusOK, newWorker)
  62. }
  63. // listJobsOfWorker respond with all the jobs of the specified worker
  64. func (s *managerServer) listJobsOfWorker(c *gin.Context) {
  65. workerID := c.Param("id")
  66. mirrorStatusList, err := s.adapter.ListMirrorStatus(workerID)
  67. if err != nil {
  68. err := fmt.Errorf("failed to list jobs of worker %s: %s",
  69. workerID, err.Error(),
  70. )
  71. c.Error(err)
  72. s.returnErrJSON(c, http.StatusInternalServerError, err)
  73. return
  74. }
  75. c.JSON(http.StatusOK, mirrorStatusList)
  76. }
  77. func (s *managerServer) returnErrJSON(c *gin.Context, code int, err error) {
  78. c.JSON(code, gin.H{
  79. _errorKey: err.Error(),
  80. })
  81. }
  82. func (s *managerServer) updateJobOfWorker(c *gin.Context) {
  83. workerID := c.Param("id")
  84. var status mirrorStatus
  85. c.BindJSON(&status)
  86. mirrorName := status.Name
  87. newStatus, err := s.adapter.UpdateMirrorStatus(workerID, mirrorName, status)
  88. if err != nil {
  89. err := fmt.Errorf("failed to update job %s of worker %s: %s",
  90. mirrorName, workerID, err.Error(),
  91. )
  92. c.Error(err)
  93. s.returnErrJSON(c, http.StatusInternalServerError, err)
  94. return
  95. }
  96. c.JSON(http.StatusOK, newStatus)
  97. }
  98. func (s *managerServer) handleClientCmd(c *gin.Context) {
  99. var clientCmd ClientCmd
  100. c.BindJSON(&clientCmd)
  101. workerID := clientCmd.WorkerID
  102. if workerID == "" {
  103. // TODO: decide which worker should do this mirror when WorkerID is null string
  104. logger.Error("handleClientCmd case workerID == \" \" not implemented yet")
  105. c.AbortWithStatus(http.StatusInternalServerError)
  106. return
  107. }
  108. w, err := s.adapter.GetWorker(workerID)
  109. if err != nil {
  110. err := fmt.Errorf("worker %s is not registered yet", workerID)
  111. s.returnErrJSON(c, http.StatusBadRequest, err)
  112. return
  113. }
  114. workerURL := w.URL
  115. // parse client cmd into worker cmd
  116. workerCmd := WorkerCmd{
  117. Cmd: clientCmd.Cmd,
  118. MirrorID: clientCmd.MirrorID,
  119. Args: clientCmd.Args,
  120. }
  121. // post command to worker
  122. _, err = postJSON(workerURL, workerCmd)
  123. if err != nil {
  124. err := fmt.Errorf("post command to worker %s(%s) fail: %s", workerID, workerURL, err.Error())
  125. c.Error(err)
  126. s.returnErrJSON(c, http.StatusInternalServerError, err)
  127. return
  128. }
  129. // TODO: check response for success
  130. c.JSON(http.StatusOK, gin.H{_infoKey: "successfully send command to worker " + workerID})
  131. }
  132. func (s *managerServer) setDBAdapter(adapter dbAdapter) {
  133. s.adapter = adapter
  134. }
  135. func makeHTTPServer(debug bool) *managerServer {
  136. // create gin engine
  137. if !debug {
  138. gin.SetMode(gin.ReleaseMode)
  139. }
  140. s := &managerServer{
  141. gin.Default(),
  142. nil,
  143. }
  144. // common log middleware
  145. s.Use(contextErrorLogger)
  146. s.GET("/ping", func(c *gin.Context) {
  147. c.JSON(http.StatusOK, gin.H{_infoKey: "pong"})
  148. })
  149. // list jobs, status page
  150. s.GET("/jobs", s.listAllJobs)
  151. // list workers
  152. s.GET("/workers", s.listWorkers)
  153. // worker online
  154. s.POST("/workers", s.registerWorker)
  155. // workerID should be valid in this route group
  156. workerValidateGroup := s.Group("/workers", s.workerIDValidator)
  157. // get job list
  158. workerValidateGroup.GET(":id/jobs", s.listJobsOfWorker)
  159. // post job status
  160. workerValidateGroup.POST(":id/jobs/:job", s.updateJobOfWorker)
  161. // for tunasynctl to post commands
  162. s.POST("/cmd", s.handleClientCmd)
  163. return s
  164. }