task.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. if len(tasks) == 0 {
  47. return
  48. }
  49. if len(tasks) == 1 {
  50. c.tasks = append(c.tasks, tasks[0])
  51. return
  52. }
  53. c.tasks = append(c.tasks, func() error {
  54. return execute(tasks...)
  55. })
  56. }
  57. }
  58. func OnSuccess(task Task) ExecutionOption {
  59. return func(c *executionContext) {
  60. c.onSuccess = task
  61. }
  62. }
  63. func OnFailure(task Task) ExecutionOption {
  64. return func(c *executionContext) {
  65. c.onFailure = task
  66. }
  67. }
  68. func Single(task Task, opts ExecutionOption) Task {
  69. return Run(append([]ExecutionOption{Sequential(task)}, opts)...)
  70. }
  71. func Run(opts ...ExecutionOption) Task {
  72. var c executionContext
  73. for _, opt := range opts {
  74. opt(&c)
  75. }
  76. return func() error {
  77. return c.run()
  78. }
  79. }
  80. // execute runs a list of tasks sequentially, returns the first error encountered or nil if all tasks pass.
  81. func execute(tasks ...Task) error {
  82. for _, task := range tasks {
  83. if err := task(); err != nil {
  84. return err
  85. }
  86. }
  87. return nil
  88. }
  89. // executeParallel executes a list of tasks asynchronously, returns the first error encountered or nil if all tasks pass.
  90. func executeParallel(ctx context.Context, tasks []Task) error {
  91. n := len(tasks)
  92. s := semaphore.New(n)
  93. done := make(chan error, 1)
  94. for _, task := range tasks {
  95. <-s.Wait()
  96. go func(f func() error) {
  97. if err := f(); err != nil {
  98. select {
  99. case done <- err:
  100. default:
  101. }
  102. }
  103. s.Signal()
  104. }(task)
  105. }
  106. for i := 0; i < n; i++ {
  107. select {
  108. case err := <-done:
  109. return err
  110. case <-ctx.Done():
  111. return ctx.Err()
  112. case <-s.Wait():
  113. }
  114. }
  115. return nil
  116. }