msg.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package internal
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. // A StatusUpdateMsg 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. Upstream string `json:"upstream"`
  15. Size string `json:"size"`
  16. ErrorMsg string `json:"error_msg"`
  17. }
  18. // A WorkerStatus is the information struct that describe
  19. // a worker, and sent from the manager to clients.
  20. type WorkerStatus struct {
  21. ID string `json:"id"`
  22. URL string `json:"url"` // worker url
  23. Token string `json:"token"` // session token
  24. LastOnline time.Time `json:"last_online"` // last seen
  25. }
  26. type CmdVerb uint8
  27. const (
  28. CmdStart CmdVerb = iota
  29. CmdStop // stop syncing keep the job
  30. CmdDisable // disable the job (stops goroutine)
  31. CmdRestart // restart syncing
  32. CmdPing // ensure the goroutine is alive
  33. )
  34. func (c CmdVerb) String() string {
  35. switch c {
  36. case CmdStart:
  37. return "start"
  38. case CmdStop:
  39. return "stop"
  40. case CmdDisable:
  41. return "disable"
  42. case CmdRestart:
  43. return "restart"
  44. case CmdPing:
  45. return "ping"
  46. }
  47. return "unknown"
  48. }
  49. // A WorkerCmd is the command message send from the
  50. // manager to a worker
  51. type WorkerCmd struct {
  52. Cmd CmdVerb `json:"cmd"`
  53. MirrorID string `json:"mirror_id"`
  54. Args []string `json:"args"`
  55. }
  56. func (c WorkerCmd) String() string {
  57. if len(c.Args) > 0 {
  58. return fmt.Sprintf("%v (%s, %v)", c.Cmd, c.MirrorID, c.Args)
  59. }
  60. return fmt.Sprintf("%v (%s)", c.Cmd, c.MirrorID)
  61. }
  62. // A ClientCmd is the command message send from client
  63. // to the manager
  64. type ClientCmd struct {
  65. Cmd CmdVerb `json:"cmd"`
  66. MirrorID string `json:"mirror_id"`
  67. WorkerID string `json:"worker_id"`
  68. Args []string `json:"args"`
  69. }