Advertisement
Guest User

Untitled

a guest
Mar 26th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.95 KB | None | 0 0
  1. PASTEBINGO  API TOOLS FAQ DEALS
  2. Search...
  3. pasteSIGN IN SIGN UP
  4. Public Pastes
  5. Untitled
  6. PHP | 0 sec ago
  7. Untitled
  8. 2 sec ago
  9. Untitled
  10. 2 sec ago
  11. Untitled
  12. 4 sec ago
  13. Untitled
  14. 5 sec ago
  15. Untitled
  16. 6 sec ago
  17. Untitled
  18. Python | 6 sec ago
  19. Il Piatto Piange TAS
  20. 12 sec ago
  21.  
  22. Untitled
  23. A GUEST  MAR 26TH, 2020  0  NEVER
  24. NOTE: Your guest paste has been posted. If you sign up for a free account, you can edit and delete your pastes!
  25. rawdownloadcloneembedreportprintPHP 1.41 KB
  26. <?php
  27.  
  28. class Message {
  29.     public function __construct($job) {
  30.         //@todo: set data accordingly
  31.     }
  32.  
  33.     public $timestamp = 123;
  34.     public $data      = [];
  35. }
  36.  
  37. class Subscriber {
  38.     private $run = true;
  39.  
  40.     public function __construct() {
  41.         declare(ticks = 1);
  42.         pcntl_signal(SIGTERM, function () {
  43.             $this->run = false;
  44.         });
  45.     }
  46.  
  47.     public function Subscribe(Callable $callback): void {
  48.         while ($this->run) {
  49.             $jobs = $this->selectJobs();
  50.             foreach ($jobs as $job) {
  51.                 $Message = new Message($job);
  52.                 if ($callback($Message)) {
  53.                     $this->markJobDone($job['id']);
  54.                 } else {
  55.                     $this->markJobFailed($job['id']);
  56.                 }
  57.             }
  58.         }
  59.     }
  60.  
  61.     private function selectJobs(): array {
  62.         //@todo: select && lock jobs from musql
  63.         return [];
  64.     }
  65.  
  66.     private function markJobDone(int $job_id) : void {
  67.         //@todo: delete from mysql
  68.     }
  69.  
  70.     private function markJobFailed(int $job_id) : void {
  71.         //@todo: log error, send job TO THE END OF THE QUEUE (otherwise failed jobs may stop queue to being processed)
  72.         //@todo: increase fail_counter. if fail_counter > threshold_value, do not select this job anymore => need manual processing
  73.     }
  74. }
  75.  
  76. class Worker {
  77.     public function run(): void {
  78.         (new Subscriber())->Subscribe([$this, 'processMessage']);
  79.     }
  80.  
  81.     private function processMessage(Message $Message): bool {
  82.         var_dump($Message->data);
  83.         // process message here
  84.         return true;
  85.     }
  86. }
  87.  
  88. (new Worker)->run();
  89. RAW Paste Data
  90. <?php
  91.  
  92. class Message {
  93.     public function __construct($job) {
  94.         //@todo: set data accordingly
  95.     }
  96.  
  97.     public $timestamp = 123;
  98.     public $data      = [];
  99. }
  100.  
  101. class Subscriber {
  102.     private $run = true;
  103.  
  104.     public function __construct() {
  105.         declare(ticks = 1);
  106.         pcntl_signal(SIGTERM, function () {
  107.             $this->run = false;
  108.         });
  109.     }
  110.  
  111.     public function Subscribe(Callable $callback): void {
  112.         while ($this->run) {
  113.             $jobs = $this->selectJobs();
  114.             foreach ($jobs as $job) {
  115.                 $Message = new Message($job);
  116.                 if ($callback($Message)) {
  117.                     $this->markJobDone($job['id']);
  118.                 } else {
  119.                     $this->markJobFailed($job['id']);
  120.                 }
  121.                 if (!$this->run) break;
  122.             }
  123.         }
  124.     }
  125.  
  126.     private function selectJobs(): array {
  127.         //@todo: select && lock jobs from musql
  128.         return [];
  129.     }
  130.  
  131.     private function markJobDone(int $job_id) : void {
  132.         //@todo: delete from mysql
  133.     }
  134.  
  135.     private function markJobFailed(int $job_id) : void {
  136.         //@todo: log error, send job TO THE END OF THE QUEUE (otherwise failed jobs may stop queue to being processed)
  137.         //@todo: increase fail_counter. if fail_counter > threshold_value, do not select this job anymore => need manual processing
  138.     }
  139. }
  140.  
  141. class Worker {
  142.     public function run(): void {
  143.         (new Subscriber())->Subscribe([$this, 'processMessage']);
  144.     }
  145.  
  146.     private function processMessage(Message $Message): bool {
  147.         var_dump($Message->data);
  148.         // process message here
  149.         return true;
  150.     }
  151. }
  152.  
  153. (new Worker)->run();
  154. We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
  155.        
  156. create new paste  /  dealsnew!  /  syntax languages  /  archive  /  faq  /  tools  /  night mode  /  api  /  scraping api
  157. privacy statement  /  cookies policy  /  terms of service  /  security disclosure  /  dmca  /  contact
  158.  
  159. By using Pastebin.com you agree to our cookies policy to enhance your experience.
  160. Site design & logo © 2020 Pastebin; user contributions (pastes) licensed under cc by-sa 3.0 -- FavPNG -- Dedicated Server Hosting by Steadfast
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement