msg.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package internal
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. // A MirrorStatus represents a msg when
  7. // a worker has done syncing
  8. type MirrorStatus struct {
  9. Name string `json:"name"`
  10. Worker string `json:"worker"`
  11. IsMaster bool `json:"is_master"`
  12. Status SyncStatus `json:"status"`
  13. LastUpdate time.Time `json:"last_update"`
  14. LastEnded time.Time `json:"last_ended"`
  15. Upstream string `json:"upstream"`
  16. Size string `json:"size"`
  17. ErrorMsg string `json:"error_msg"`
  18. }
  19. // A WorkerStatus is the information struct that describe
  20. // a worker, and sent from the manager to clients.
  21. type WorkerStatus struct {
  22. ID string `json:"id"`
  23. URL string `json:"url"` // worker url
  24. Token string `json:"token"` // session token
  25. LastOnline time.Time `json:"last_online"` // last seen
  26. }
  27. // A CmdVerb is an action to a job or worker
  28. type CmdVerb uint8
  29. const (
  30. // CmdStart start a job
  31. CmdStart CmdVerb = iota
  32. // CmdStop stop syncing, but keep the job
  33. CmdStop
  34. // CmdDisable disable the job (stops goroutine)
  35. CmdDisable
  36. // CmdRestart restart a syncing job
  37. CmdRestart
  38. // CmdPing ensures the goroutine is alive
  39. CmdPing
  40. // CmdReload tells a worker to reload mirror config
  41. CmdReload
  42. )
  43. func (c CmdVerb) String() string {
  44. switch c {
  45. case CmdStart:
  46. return "start"
  47. case CmdStop:
  48. return "stop"
  49. case CmdDisable:
  50. return "disable"
  51. case CmdRestart:
  52. return "restart"
  53. case CmdPing:
  54. return "ping"
  55. case CmdReload:
  56. return "reload"
  57. }
  58. return "unknown"
  59. }
  60. // A WorkerCmd is the command message send from the
  61. // manager to a worker
  62. type WorkerCmd struct {
  63. Cmd CmdVerb `json:"cmd"`
  64. MirrorID string `json:"mirror_id"`
  65. Args []string `json:"args"`
  66. }
  67. func (c WorkerCmd) String() string {
  68. if len(c.Args) > 0 {
  69. return fmt.Sprintf("%v (%s, %v)", c.Cmd, c.MirrorID, c.Args)
  70. }
  71. return fmt.Sprintf("%v (%s)", c.Cmd, c.MirrorID)
  72. }
  73. // A ClientCmd is the command message send from client
  74. // to the manager
  75. type ClientCmd struct {
  76. Cmd CmdVerb `json:"cmd"`
  77. MirrorID string `json:"mirror_id"`
  78. WorkerID string `json:"worker_id"`
  79. Args []string `json:"args"`
  80. }