provider.go 1.9 KB

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