msg.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. LastStarted time.Time `json:"last_started"`
  15. LastEnded time.Time `json:"last_ended"`
  16. Scheduled time.Time `json:"next_schedule"`
  17. Upstream string `json:"upstream"`
  18. Size string `json:"size"`
  19. ErrorMsg string `json:"error_msg"`
  20. }
  21. // A WorkerStatus is the information struct that describe
  22. // a worker, and sent from the manager to clients.
  23. type WorkerStatus struct {
  24. ID string `json:"id"`
  25. URL string `json:"url"` // worker url
  26. Token string `json:"token"` // session token
  27. LastOnline time.Time `json:"last_online"` // last seen
  28. }
  29. type MirrorSchedules struct {
  30. Schedules []MirrorSchedule `json:"schedules"`
  31. }
  32. type MirrorSchedule struct {
  33. MirrorName string `json:"name"`
  34. NextSchedule time.Time `json:"next_schedule"`
  35. }
  36. // A CmdVerb is an action to a job or worker
  37. type CmdVerb uint8
  38. const (
  39. // CmdStart start a job
  40. CmdStart CmdVerb = iota
  41. // CmdStop stop syncing, but keep the job
  42. CmdStop
  43. // CmdDisable disable the job (stops goroutine)
  44. CmdDisable
  45. // CmdRestart restart a syncing job
  46. CmdRestart
  47. // CmdPing ensures the goroutine is alive
  48. CmdPing
  49. // CmdReload tells a worker to reload mirror config
  50. CmdReload
  51. )
  52. func (c CmdVerb) String() string {
  53. switch c {
  54. case CmdStart:
  55. return "start"
  56. case CmdStop:
  57. return "stop"
  58. case CmdDisable:
  59. return "disable"
  60. case CmdRestart:
  61. return "restart"
  62. case CmdPing:
  63. return "ping"
  64. case CmdReload:
  65. return "reload"
  66. }
  67. return "unknown"
  68. }
  69. // A WorkerCmd is the command message send from the
  70. // manager to a worker
  71. type WorkerCmd struct {
  72. Cmd CmdVerb `json:"cmd"`
  73. MirrorID string `json:"mirror_id"`
  74. Args []string `json:"args"`
  75. Options map[string]bool `json:"options"`
  76. }
  77. func (c WorkerCmd) String() string {
  78. if len(c.Args) > 0 {
  79. return fmt.Sprintf("%v (%s, %v)", c.Cmd, c.MirrorID, c.Args)
  80. }
  81. return fmt.Sprintf("%v (%s)", c.Cmd, c.MirrorID)
  82. }
  83. // A ClientCmd is the command message send from client
  84. // to the manager
  85. type ClientCmd struct {
  86. Cmd CmdVerb `json:"cmd"`
  87. MirrorID string `json:"mirror_id"`
  88. WorkerID string `json:"worker_id"`
  89. Args []string `json:"args"`
  90. Options map[string]bool `json:"options"`
  91. }