task.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package task
  2. import (
  3. "context"
  4. "v2ray.com/core/common/signal"
  5. )
  6. type Task func() error
  7. type executionContext struct {
  8. ctx context.Context
  9. task Task
  10. onSuccess Task
  11. onFailure Task
  12. }
  13. func (c *executionContext) executeTask() error {
  14. if c.ctx == nil && c.task == nil {
  15. return nil
  16. }
  17. if c.ctx == nil {
  18. return c.task()
  19. }
  20. if c.task == nil {
  21. <-c.ctx.Done()
  22. return c.ctx.Err()
  23. }
  24. return executeParallel(func() error {
  25. <-c.ctx.Done()
  26. return c.ctx.Err()
  27. }, c.task)
  28. }
  29. func (c *executionContext) run() error {
  30. err := c.executeTask()
  31. if err == nil && c.onSuccess != nil {
  32. return c.onSuccess()
  33. }
  34. if err != nil && c.onFailure != nil {
  35. return c.onFailure()
  36. }
  37. return err
  38. }
  39. type ExecutionOption func(*executionContext)
  40. func WithContext(ctx context.Context) ExecutionOption {
  41. return func(c *executionContext) {
  42. c.ctx = ctx
  43. }
  44. }
  45. func Parallel(tasks ...Task) ExecutionOption {
  46. return func(c *executionContext) {
  47. c.task = func() error {
  48. return executeParallel(tasks...)
  49. }
  50. }
  51. }
  52. func Sequential(tasks ...Task) ExecutionOption {
  53. return func(c *executionContext) {
  54. c.task = func() error {
  55. return execute(tasks...)
  56. }
  57. }
  58. }
  59. func OnSuccess(task Task) ExecutionOption {
  60. return func(c *executionContext) {
  61. c.onSuccess = task
  62. }
  63. }
  64. func OnFailure(task Task) ExecutionOption {
  65. return func(c *executionContext) {
  66. c.onFailure = task
  67. }
  68. }
  69. func Single(task Task, opts ExecutionOption) Task {
  70. return Run(append([]ExecutionOption{Sequential(task)}, opts)...)
  71. }
  72. func Run(opts ...ExecutionOption) Task {
  73. var c executionContext
  74. for _, opt := range opts {
  75. opt(&c)
  76. }
  77. return func() error {
  78. return c.run()
  79. }
  80. }
  81. // execute runs a list of tasks sequentially, returns the first error encountered or nil if all tasks pass.
  82. func execute(tasks ...Task) error {
  83. for _, task := range tasks {
  84. if err := task(); err != nil {
  85. return err
  86. }
  87. }
  88. return nil
  89. }
  90. // executeParallel executes a list of tasks asynchronously, returns the first error encountered or nil if all tasks pass.
  91. func executeParallel(tasks ...Task) error {
  92. n := len(tasks)
  93. s := signal.NewSemaphore(n)
  94. done := make(chan error, 1)
  95. for _, task := range tasks {
  96. <-s.Wait()
  97. go func(f func() error) {
  98. if err := f(); err != nil {
  99. select {
  100. case done <- err:
  101. default:
  102. }
  103. }
  104. s.Signal()
  105. }(task)
  106. }
  107. for i := 0; i < n; i++ {
  108. select {
  109. case err := <-done:
  110. return err
  111. case <-s.Wait():
  112. }
  113. }
  114. return nil
  115. }