Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. use Ds\Queue;
  4.  
  5. class ThingDoer
  6. {
  7.     private $jobs;
  8.     private $running = false;
  9.  
  10.     public function __construct()
  11.     {
  12.         $this->jobs = new Queue;
  13.     }
  14.  
  15.     private executeJob(Job $job)
  16.     {
  17.         // do stuff here
  18.     }
  19.  
  20.     private run()
  21.     {
  22.         $this->running = true;
  23.  
  24.         while (!$this->jobs->isEmpty()) {
  25.             yield from $this->executeJob($this->jobs->pop());
  26.         }
  27.  
  28.         $this->running = false;
  29.     }
  30.  
  31.     public function enqueueJob(Job $job)
  32.     {
  33.         $this->jobs->push($job);
  34.  
  35.         if (!$this->running) {
  36.             resolve($this->run());
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement