2
0

msg.go 2.5 KB

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