msg.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package internal
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "time"
  7. )
  8. // A MirrorStatus represents a msg when
  9. // a worker has done syncing
  10. type MirrorStatus struct {
  11. Name string `json:"name"`
  12. Worker string `json:"worker"`
  13. IsMaster bool `json:"is_master"`
  14. Status SyncStatus `json:"status"`
  15. LastUpdate time.Time `json:"last_update"`
  16. LastStarted time.Time `json:"last_started"`
  17. LastEnded time.Time `json:"last_ended"`
  18. Scheduled time.Time `json:"next_schedule"`
  19. Upstream string `json:"upstream"`
  20. Size string `json:"size"`
  21. ErrorMsg string `json:"error_msg"`
  22. }
  23. // A WorkerStatus is the information struct that describe
  24. // a worker, and sent from the manager to clients.
  25. type WorkerStatus struct {
  26. ID string `json:"id"`
  27. URL string `json:"url"` // worker url
  28. Token string `json:"token"` // session token
  29. LastOnline time.Time `json:"last_online"` // last seen
  30. LastRegister time.Time `json:"last_register"` // last register time
  31. }
  32. type MirrorSchedules struct {
  33. Schedules []MirrorSchedule `json:"schedules"`
  34. }
  35. type MirrorSchedule struct {
  36. MirrorName string `json:"name"`
  37. NextSchedule time.Time `json:"next_schedule"`
  38. }
  39. // A CmdVerb is an action to a job or worker
  40. type CmdVerb uint8
  41. const (
  42. // CmdStart start a job
  43. CmdStart CmdVerb = iota
  44. // CmdStop stop syncing, but keep the job
  45. CmdStop
  46. // CmdDisable disable the job (stops goroutine)
  47. CmdDisable
  48. // CmdRestart restart a syncing job
  49. CmdRestart
  50. // CmdPing ensures the goroutine is alive
  51. CmdPing
  52. // CmdReload tells a worker to reload mirror config
  53. CmdReload
  54. )
  55. func (c CmdVerb) String() string {
  56. mapping := map[CmdVerb]string{
  57. CmdStart: "start",
  58. CmdStop: "stop",
  59. CmdDisable: "disable",
  60. CmdRestart: "restart",
  61. CmdPing: "ping",
  62. CmdReload: "reload",
  63. }
  64. return mapping[c]
  65. }
  66. func NewCmdVerbFromString(s string) CmdVerb {
  67. mapping := map[string]CmdVerb{
  68. "start": CmdStart,
  69. "stop": CmdStop,
  70. "disable": CmdDisable,
  71. "restart": CmdRestart,
  72. "ping": CmdPing,
  73. "reload": CmdReload,
  74. }
  75. return mapping[s]
  76. }
  77. // Marshal and Unmarshal for CmdVerb
  78. func (s CmdVerb) MarshalJSON() ([]byte, error) {
  79. buffer := bytes.NewBufferString(`"`)
  80. buffer.WriteString(s.String())
  81. buffer.WriteString(`"`)
  82. return buffer.Bytes(), nil
  83. }
  84. func (s *CmdVerb) UnmarshalJSON(b []byte) error {
  85. var j string
  86. err := json.Unmarshal(b, &j)
  87. if err != nil {
  88. return err
  89. }
  90. *s = NewCmdVerbFromString(j)
  91. return nil
  92. }
  93. // A WorkerCmd is the command message send from the
  94. // manager to a worker
  95. type WorkerCmd struct {
  96. Cmd CmdVerb `json:"cmd"`
  97. MirrorID string `json:"mirror_id"`
  98. Args []string `json:"args"`
  99. Options map[string]bool `json:"options"`
  100. }
  101. func (c WorkerCmd) String() string {
  102. if len(c.Args) > 0 {
  103. return fmt.Sprintf("%v (%s, %v)", c.Cmd, c.MirrorID, c.Args)
  104. }
  105. return fmt.Sprintf("%v (%s)", c.Cmd, c.MirrorID)
  106. }
  107. // A ClientCmd is the command message send from client
  108. // to the manager
  109. type ClientCmd struct {
  110. Cmd CmdVerb `json:"cmd"`
  111. MirrorID string `json:"mirror_id"`
  112. WorkerID string `json:"worker_id"`
  113. Args []string `json:"args"`
  114. Options map[string]bool `json:"options"`
  115. }