2
0

msg.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. Options map[string]bool `json:"options"`
  67. }
  68. func (c WorkerCmd) String() string {
  69. if len(c.Args) > 0 {
  70. return fmt.Sprintf("%v (%s, %v)", c.Cmd, c.MirrorID, c.Args)
  71. }
  72. return fmt.Sprintf("%v (%s)", c.Cmd, c.MirrorID)
  73. }
  74. // A ClientCmd is the command message send from client
  75. // to the manager
  76. type ClientCmd struct {
  77. Cmd CmdVerb `json:"cmd"`
  78. MirrorID string `json:"mirror_id"`
  79. WorkerID string `json:"worker_id"`
  80. Args []string `json:"args"`
  81. Options map[string]bool `json:"options"`
  82. }