task.go 2.3 KB

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