provider.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package worker
  2. // mirror provider is the wrapper of mirror jobs
  3. type providerType uint8
  4. const (
  5. _WorkingDirKey = "working_dir"
  6. _LogDirKey = "log_dir"
  7. _LogFileKey = "log_file"
  8. )
  9. // A mirrorProvider instance
  10. type mirrorProvider interface {
  11. // name
  12. Name() string
  13. // TODO: implement Run, Terminate and Hooks
  14. // run mirror job
  15. Run()
  16. // terminate mirror job
  17. Terminate()
  18. // job hooks
  19. Hooks()
  20. Interval() int
  21. WorkingDir() string
  22. LogDir() string
  23. LogFile() string
  24. // enter context
  25. EnterContext() *Context
  26. // exit context
  27. ExitContext() *Context
  28. // return context
  29. Context() *Context
  30. }
  31. type baseProvider struct {
  32. ctx *Context
  33. name string
  34. interval int
  35. }
  36. func (p *baseProvider) Name() string {
  37. return p.name
  38. }
  39. func (p *baseProvider) EnterContext() *Context {
  40. p.ctx = p.ctx.Enter()
  41. return p.ctx
  42. }
  43. func (p *baseProvider) ExitContext() *Context {
  44. p.ctx, _ = p.ctx.Exit()
  45. return p.ctx
  46. }
  47. func (p *baseProvider) Context() *Context {
  48. return p.ctx
  49. }
  50. func (p *baseProvider) Interval() int {
  51. return p.interval
  52. }
  53. func (p *baseProvider) WorkingDir() string {
  54. if v, ok := p.ctx.Get(_WorkingDirKey); ok {
  55. if s, ok := v.(string); ok {
  56. return s
  57. }
  58. }
  59. panic("working dir is impossible to be non-exist")
  60. }
  61. func (p *baseProvider) LogDir() string {
  62. if v, ok := p.ctx.Get(_LogDirKey); ok {
  63. if s, ok := v.(string); ok {
  64. return s
  65. }
  66. }
  67. panic("log dir is impossible to be unavailable")
  68. }
  69. func (p *baseProvider) LogFile() string {
  70. if v, ok := p.ctx.Get(_LogFileKey); ok {
  71. if s, ok := v.(string); ok {
  72. return s
  73. }
  74. }
  75. panic("log dir is impossible to be unavailable")
  76. }