SproutCMS

This is the code documentation for the SproutCMS project

source of /modules/Demo/Helpers/DemoWorker.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace SproutModules\Karmabunny\Demo\Helpers;
  15.  
  16. use Sprout\Helpers\Sprout;
  17. use Sprout\Helpers\Worker;
  18. use Sprout\Helpers\WorkerBase;
  19.  
  20.  
  21. /**
  22.  * A basic demonstration of how worker jobs run, using {@see WorkerBase}
  23.  */
  24. class DemoWorker extends WorkerBase
  25. {
  26. protected $job_name = 'Demo worker';
  27. protected $metric_names = [
  28. 1 => 'First run',
  29. 2 => 'Second run',
  30. 3 => 'Final run',
  31. ];
  32.  
  33. /**
  34.   * Do the work which a worker must do
  35.   */
  36. public function run($a, $b, $c)
  37. {
  38. Worker::message("It's running! {$a}");
  39. for ($i = 1; $i <= $a; ++$i) {
  40. usleep(10000);
  41. Worker::metric(1, $i);
  42. }
  43.  
  44. Worker::message("Still running!");
  45. Worker::message('$_SERVER:');
  46. Worker::message(print_r($_SERVER, true));
  47. for ($i = 1; $i <= $b; ++$i) {
  48. usleep(10000);
  49. Worker::metric(2, $i);
  50. }
  51.  
  52. Worker::message("Almost there!");
  53. Worker::message('Sprout::absRoot():');
  54. Worker::message(Sprout::absRoot());
  55. for ($i = 1; $i <= $c; ++$i) {
  56. usleep(10000);
  57. Worker::metric(3, $i);
  58. }
  59.  
  60. Worker::message('Finished');
  61. Worker::success();
  62. }
  63.  
  64. }
  65.