SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/WorkerBase.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 Sprout\Helpers;
  15.  
  16.  
  17. /**
  18.  * Base class for worker jobs, which are run via CLI in a separate process
  19.  */
  20. abstract class WorkerBase {
  21. /**
  22.   * Specify a custom job name by overwriting this
  23.   **/
  24. protected $job_name = '';
  25.  
  26. /**
  27.   * Specify up to three metrics
  28.   **/
  29. protected $metric_names = array(
  30. 1 => '',
  31. 2 => '',
  32. 3 => '',
  33. );
  34.  
  35.  
  36. /*
  37.   public function run(...)
  38.   {
  39.   //
  40.   // Whatever is provided as arguments to WorkerCtrl::start will be provided in the function call
  41.   //
  42.   }
  43.   */
  44.  
  45.  
  46.  
  47. /**
  48.   * Constructor
  49.   *
  50.   * Manually checks if the method 'run' exists,
  51.   * because it's a varags,
  52.   * so can't use the normal abstract function approach
  53.   **/
  54. public final function __construct() {
  55. method_exists($this, 'run');
  56. }
  57.  
  58. /**
  59.   * Gets the job name
  60.   **/
  61. public final function getName() {
  62. if ($this->job_name) return $this->job_name;
  63. return get_class();
  64. }
  65.  
  66. /**
  67.   * Gets the job name
  68.   **/
  69. public final function getMetricNames() {
  70. return $this->metric_names;
  71. }
  72.  
  73. }
  74.  
  75.  
  76.