Skip to content

caophuocthanh/QueueManager

Repository files navigation

QueueManager

Feature:

Phase 1:

  • Can set a scenario to execute.
  • Queue can run with many scenarios inside.
  • State of the queue: working, resting.
  • Callback when a scenario is done.
  • Callback when a task is done. Can add listen to callback much time.
  • Queue handle with minimum tasks - Remove duplicate task - Reduce tasks to execute.
  • Set duration to ignore tasks. Or set ignore == false
  • A lib can easier to apply to an existing project.

Phase 2:

  • A task can have required condition tasks these must be done before running this task.
  • If task adapted conditions. A task can run immediately. No need to wait its turn

Code:

  • Lib
  • Demo

How it work:

Input:

   (begin):
         [scenario #1] -- task1 - task2 - task3 - [task4, task5, task6] - task7
   (after 1s):
      [
         [scenario #2] -- task1 - task2 - task3 - task8
         [scenario #3] -- task1 - task2 - task3 - task9
         [scenario #4] -- task1 - task2 - task3 - task10
         [scenario #5] --task1 - task2 - task3 - [task4, task5, task6] - task7
      ]
   (after 60s):
         [scenario #6] -- task1 - task2 - task3 - [task4, task5, task6] - task7
        
    // with task8, task9, task10 set irnore = false

After prepare and will excute:

queue[]:  task1 - task2 - task3 - [task4, task5, task6] - task7 - (finish #1) - task8 - (finish #2) - task9 - (finish #3) - task10- (finish #4) - (finish #5) - task1 - task2 - task3  - [task4, task5, task6] - task7 - (finish #6)
 - (finish #6)

So, with the scenario #2 , #3, #5, #5. the scenario not execute . The scenario run faster. After 60s, #2 , #3, #5. more than duration. The tasks #2 , #3, #5 can run with another scenario.

How to use:

Import

#import AnyQueueManager

Define workers

    public static var task1: Worker = {
        let worker = Worker(name: "task1") { (makeCompleted) in
            // TODO:
            // Hanlde task here
            // Finally, makeCompleted()
            DispatchQueue.global().asyncAfter(deadline: .now() + 2) {
                print("✪ makeCompleted  => push_contact")
                makeCompleted()
            }
        }
        // Duration for next excute
        worker.duration = 0
        // Accept ignore after duration time
        worker.ignore = false
        return worker
    }()

Create a scenario

    func scenario() {
        return QueueManager.Scenario(
            name: "\(Date().timeIntervalSince1970)_scenario",
            tasks: [
                .async([
                    task1,
                    task2,
                    task3,
                ]),
                .sync(task4)
            ],
            completed: {
                print("first sync completed \n\n\n\n")
            })
    }

Execute

       let scenario = scenario()
        QueueManager.run(scenario)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published