resque /
php-resque
| 1 | <?php |
||||
| 2 | |||||
| 3 | /** |
||||
| 4 | * Status tracker/information for a job. |
||||
| 5 | * |
||||
| 6 | * @package Resque/Job |
||||
| 7 | * @author Chris Boulton <[email protected]> |
||||
| 8 | * @license http://www.opensource.org/licenses/mit-license.php |
||||
| 9 | */ |
||||
| 10 | class Resque_Job_Status |
||||
| 11 | { |
||||
| 12 | const STATUS_WAITING = 1; |
||||
| 13 | const STATUS_RUNNING = 2; |
||||
| 14 | const STATUS_FAILED = 3; |
||||
| 15 | const STATUS_COMPLETE = 4; |
||||
| 16 | |||||
| 17 | /** |
||||
| 18 | * @var string The prefix of the job status id. |
||||
| 19 | */ |
||||
| 20 | private $prefix; |
||||
| 21 | |||||
| 22 | /** |
||||
| 23 | * @var string The ID of the job this status class refers back to. |
||||
| 24 | */ |
||||
| 25 | private $id; |
||||
| 26 | |||||
| 27 | /** |
||||
| 28 | * @var mixed Cache variable if the status of this job is being monitored or not. |
||||
| 29 | * True/false when checked at least once or null if not checked yet. |
||||
| 30 | */ |
||||
| 31 | private $isTracking = null; |
||||
| 32 | |||||
| 33 | /** |
||||
| 34 | * @var array Array of statuses that are considered final/complete. |
||||
| 35 | */ |
||||
| 36 | private static $completeStatuses = array( |
||||
| 37 | self::STATUS_FAILED, |
||||
| 38 | self::STATUS_COMPLETE |
||||
| 39 | ); |
||||
| 40 | |||||
| 41 | /** |
||||
| 42 | * Setup a new instance of the job monitor class for the supplied job ID. |
||||
| 43 | * |
||||
| 44 | * @param string $id The ID of the job to manage the status for. |
||||
| 45 | 19 | */ |
|||
| 46 | public function __construct($id, $prefix = '') |
||||
| 47 | 19 | { |
|||
| 48 | 19 | $this->id = $id; |
|||
| 49 | $this->prefix = empty($prefix) ? '' : "${prefix}_"; |
||||
| 50 | } |
||||
| 51 | |||||
| 52 | /** |
||||
| 53 | * Create a new status monitor item for the supplied job ID. Will create |
||||
| 54 | * all necessary keys in Redis to monitor the status of a job. |
||||
| 55 | * |
||||
| 56 | * @param string $id The ID of the job to monitor the status of. |
||||
| 57 | */ |
||||
| 58 | public static function create($id, $prefix = "") |
||||
| 59 | 11 | { |
|||
| 60 | $status = new self($id, $prefix); |
||||
| 61 | 11 | $statusPacket = array( |
|||
| 62 | 11 | 'status' => self::STATUS_WAITING, |
|||
| 63 | 11 | 'updated' => time(), |
|||
| 64 | 'started' => time(), |
||||
| 65 | 'result' => null, |
||||
| 66 | 11 | ); |
|||
| 67 | Resque::redis()->set((string) $status, json_encode($statusPacket)); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 68 | 11 | ||||
| 69 | return $status; |
||||
| 70 | } |
||||
| 71 | |||||
| 72 | /** |
||||
| 73 | * Check if we're actually checking the status of the loaded job status |
||||
| 74 | * instance. |
||||
| 75 | * |
||||
| 76 | * @return boolean True if the status is being monitored, false if not. |
||||
| 77 | */ |
||||
| 78 | public function isTracking() |
||||
| 79 | 18 | { |
|||
| 80 | if ($this->isTracking === false) { |
||||
| 81 | return false; |
||||
| 82 | } |
||||
| 83 | 18 | ||||
| 84 | 9 | if (!Resque::redis()->exists((string)$this)) { |
|||
|
0 ignored issues
–
show
The method
exists() does not exist on Resque_Redis. Since you implemented __call, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 85 | 9 | $this->isTracking = false; |
|||
| 86 | return false; |
||||
| 87 | } |
||||
| 88 | 10 | ||||
| 89 | 10 | $this->isTracking = true; |
|||
| 90 | return true; |
||||
| 91 | } |
||||
| 92 | |||||
| 93 | /** |
||||
| 94 | * Update the status indicator for the current job with a new status. |
||||
| 95 | * |
||||
| 96 | * @param int The status of the job (see constants in Resque_Job_Status) |
||||
|
0 ignored issues
–
show
The type
The was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 97 | */ |
||||
| 98 | public function update($status, $result = null) |
||||
| 99 | 12 | { |
|||
| 100 | $status = (int) $status; |
||||
| 101 | 12 | ||||
| 102 | 6 | if (!$this->isTracking()) { |
|||
| 103 | return; |
||||
| 104 | } |
||||
| 105 | 6 | ||||
| 106 | if ($status < self::STATUS_WAITING || $status > self::STATUS_COMPLETE) { |
||||
| 107 | return; |
||||
| 108 | } |
||||
| 109 | |||||
| 110 | 6 | $statusPacket = array( |
|||
| 111 | 6 | 'status' => $status, |
|||
| 112 | 6 | 'updated' => time(), |
|||
| 113 | 6 | 'started' => $this->fetch('started'), |
|||
| 114 | 'result' => $result, |
||||
| 115 | 6 | ); |
|||
| 116 | Resque::redis()->set((string)$this, json_encode($statusPacket)); |
||||
| 117 | |||||
| 118 | 6 | // Expire the status for completed jobs after 24 hours |
|||
| 119 | 1 | if (in_array($status, self::$completeStatuses)) { |
|||
| 120 | Resque::redis()->expire((string)$this, 86400); |
||||
|
0 ignored issues
–
show
The method
expire() does not exist on Resque_Redis. Since you implemented __call, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 121 | 6 | } |
|||
| 122 | } |
||||
| 123 | |||||
| 124 | /** |
||||
| 125 | * Fetch the status for the job being monitored. |
||||
| 126 | * |
||||
| 127 | * @return mixed False if the status is not being monitored, otherwise the status |
||||
| 128 | * as an integer, based on the Resque_Job_Status constants. |
||||
| 129 | */ |
||||
| 130 | public function get() |
||||
| 131 | 14 | { |
|||
| 132 | return $this->status(); |
||||
| 133 | } |
||||
| 134 | |||||
| 135 | /** |
||||
| 136 | * Fetch the status for the job being monitored. |
||||
| 137 | * |
||||
| 138 | * @return mixed False if the status is not being monitored, otherwise the status |
||||
| 139 | * as an integer, based on the Resque_Job_Status constants. |
||||
| 140 | */ |
||||
| 141 | public function status() |
||||
| 142 | 14 | { |
|||
| 143 | return $this->fetch('status'); |
||||
| 144 | } |
||||
| 145 | |||||
| 146 | /** |
||||
| 147 | * Fetch the last update timestamp of the job being monitored. |
||||
| 148 | * |
||||
| 149 | * @return mixed False if the job is not being monitored, otherwise the |
||||
| 150 | * update timestamp. |
||||
| 151 | */ |
||||
| 152 | public function updated() |
||||
| 153 | { |
||||
| 154 | return $this->fetch('updated'); |
||||
| 155 | } |
||||
| 156 | |||||
| 157 | /** |
||||
| 158 | * Fetch the start timestamp of the job being monitored. |
||||
| 159 | * |
||||
| 160 | * @return mixed False if the job is not being monitored, otherwise the |
||||
| 161 | * start timestamp. |
||||
| 162 | */ |
||||
| 163 | public function started() |
||||
| 164 | { |
||||
| 165 | return $this->fetch('started'); |
||||
| 166 | } |
||||
| 167 | |||||
| 168 | /** |
||||
| 169 | * Fetch the result of the job being monitored. |
||||
| 170 | * |
||||
| 171 | * @return mixed False if the job is not being monitored, otherwise the result |
||||
| 172 | * as mixed |
||||
| 173 | */ |
||||
| 174 | public function result() |
||||
| 175 | { |
||||
| 176 | return $this->fetch('result'); |
||||
| 177 | } |
||||
| 178 | |||||
| 179 | /** |
||||
| 180 | * Stop tracking the status of a job. |
||||
| 181 | */ |
||||
| 182 | public function stop() |
||||
| 183 | 1 | { |
|||
| 184 | 1 | Resque::redis()->del((string)$this); |
|||
|
0 ignored issues
–
show
The method
del() does not exist on Resque_Redis. Since you implemented __call, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 185 | } |
||||
| 186 | |||||
| 187 | /** |
||||
| 188 | * Generate a string representation of this object. |
||||
| 189 | * |
||||
| 190 | * @return string String representation of the current job status class. |
||||
| 191 | */ |
||||
| 192 | public function __toString() |
||||
| 193 | 19 | { |
|||
| 194 | return 'job:' . $this->prefix . $this->id . ':status'; |
||||
| 195 | } |
||||
| 196 | |||||
| 197 | /** |
||||
| 198 | * Fetch a value from the status packet for the job being monitored. |
||||
| 199 | * |
||||
| 200 | * @return mixed False if the status is not being monitored, otherwise the |
||||
| 201 | * requested value from the status packet. |
||||
| 202 | */ |
||||
| 203 | protected function fetch($value = null) |
||||
| 204 | 14 | { |
|||
| 205 | 6 | if (!$this->isTracking()) { |
|||
| 206 | return false; |
||||
| 207 | } |
||||
| 208 | 9 | ||||
| 209 | 9 | $statusPacket = json_decode(Resque::redis()->get((string)$this), true); |
|||
|
0 ignored issues
–
show
The method
get() does not exist on Resque_Redis. Since you implemented __call, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 210 | if (!$statusPacket) { |
||||
| 211 | return false; |
||||
| 212 | } |
||||
| 213 | 9 | ||||
| 214 | if (empty($value)) { |
||||
| 215 | return $statusPacket; |
||||
| 216 | 9 | } else { |
|||
| 217 | 9 | if (isset($statusPacket[$value])) { |
|||
| 218 | return $statusPacket[$value]; |
||||
| 219 | } else { |
||||
| 220 | return null; |
||||
| 221 | } |
||||
| 222 | } |
||||
| 223 | } |
||||
| 224 | } |
||||
| 225 |