3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Phalcon Framework * This source file is subject to the New BSD License that is bundled * with this package in the file docs/LICENSE.txt. * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@phalconphp.com so we can send you a copy immediately. * * @author Nikita Vershinin <endeveit@gmail.com> */ namespace Phalcon\Queue\Beanstalk; use duncan3dc\Helpers\Fork; use Phalcon\Logger\Adapter as LoggerAdapter; use Phalcon\Queue\Beanstalk as Base; /** * \Phalcon\Queue\Beanstalk\Extended * Extended class to access the beanstalk queue service. * Supports tubes prefixes, pcntl-workers and tubes stats. */ class Extended extends Base { const CMD_PUT = "put "; const CMD_PEEKJOB = "peek "; const CMD_PEEK_READY = "peek-ready"; const CMD_PEEK_DELAYED = "peek-delayed"; const CMD_PEEK_BURIED = "peek-buried"; const CMD_RESERVE = "reserve"; const CMD_RESERVE_TIMEOUT = "reserve-with-timeout "; const CMD_DELETE = "delete "; const CMD_RELEASE = "release "; const CMD_BURY = "bury "; const CMD_KICK = "kick "; const CMD_JOBKICK = "kick-job "; const CMD_TOUCH = "touch "; const CMD_STATS = "stats"; const CMD_JOBSTATS = "stats-job "; const CMD_USE = "use "; const CMD_WATCH = "watch "; const CMD_IGNORE = "ignore "; const CMD_LIST_TUBES = "list-tubes"; const CMD_LIST_TUBE_USED = "list-tube-used"; const CMD_LIST_TUBES_WATCHED = "list-tubes-watched"; const CMD_STATS_TUBE = "stats-tube "; const CMD_QUIT = "quit"; const CMD_PAUSE_TUBE = "pause-tube"; const MSG_OK = "OK"; const MSG_OK_FMT = "OK %d"; const MSG_FOUND = "FOUND"; const MSG_NOTFOUND = "NOT_FOUND"; const MSG_RESERVED = "RESERVED"; const MSG_DEADLINE_SOON = "DEADLINE_SOON"; const MSG_TIMED_OUT = "TIMED_OUT"; const MSG_DELETED = "DELETED"; const MSG_RELEASED = "RELEASED"; const MSG_BURIED = "BURIED"; const MSG_KICKED = "KICKED"; const MSG_TOUCHED = "TOUCHED"; const MSG_BURIED_FMT = "BURIED %d"; const MSG_INSERTED_FMT = "INSERTED %d"; const MSG_NOT_IGNORED = "NOT_IGNORED"; /** * The server cannot allocate enough memory for the job. * The client should try again later. */ const MSG_OUT_OF_MEMORY = "OUT_OF_MEMORY"; /** * This indicates a bug in the server.It should never happen. * If it does happen, please report it at http://groups.google.com/group/beanstalk-talk. */ const MSG_INTERNAL_ERROR = "INTERNAL_ERROR"; const MSG_DRAINING = "DRAINING"; /** * The client sent a command line that was not well-formed. * This can happen if the line does not end with , * if non-numeric characters occur where an integer is expected, * if the wrong number of arguments are present, * or if the command line is mal-formed in any other way. */ const MSG_BAD_FORMAT = "BAD_FORMAT"; /** * The client sent a command that the server does not know. */ const MSG_UNKNOWN_COMMAND = "UNKNOWN_COMMAND"; const MSG_EXPECTED_CRLF = "EXPECTED_CRLF"; const MSG_JOB_TOO_BIG = "JOB_TOO_BIG"; /** * Seconds to wait before putting the job in the ready queue. * The job will be in the "delayed" state during this time. * * @const integer */ const DEFAULT_DELAY = 0; /** * Jobs with smaller priority values will be scheduled before jobs with larger priorities. * The most urgent priority is 0, the least urgent priority is 4294967295. * * @const integer */ const DEFAULT_PRIORITY = 1024; /** * Time to run - number of seconds to allow a worker to run this job. * The minimum ttr is 1. * * @const integer */ const DEFAULT_TTR = 60; /** * If provided the errors will be logged here. * * @var \Phalcon\Logger\Adapter */ protected $logger = null; /** * Tubes prefix. * * @var string */ protected $tubePrefix = null; /** * Queue handlers. * * @var array */ protected $workers = array(); /** * {@inheritdoc} * * @param array $options */ public function __construct($options = null) { parent::__construct($options); $logger = null; $tubePrefix = ''; if (is_array($options) || ($options instanceof \ArrayAccess)) { if (isset($options['prefix'])) { $tubePrefix = $options['prefix']; } if (isset($options['logger']) && ($options['logger'] instanceof LoggerAdapter)) { $logger = $options['logger']; } } $this->logger = $logger; $this->tubePrefix = $tubePrefix; } /** * Adds new worker to the pool. * * @param string $tube * @param callable $callback * @throws \InvalidArgumentException */ public function addWorker($tube, $callback) { if (!is_string($tube)) { throw new \InvalidArgumentException('The tube name must be a string.'); } if (!is_callable($callback)) { throw new \InvalidArgumentException('The callback is invalid.'); } $this->workers[$tube] = $callback; } /** * Runs the main worker cycle. * * @param boolean $ignoreErrors */ public function doWork($ignoreErrors = false) { declare (ticks = 1); set_time_limit(0); $fork = new Fork(); $fork->ignoreErrors = $ignoreErrors; foreach ($this->workers as $tube => $worker) { $that = clone $this; // Run the worker in separate process. $fork->call(function () use ($tube, $worker, $that, $fork, $ignoreErrors) { $that->connect(); do { $job = $that->reserveFromTube($tube); if ($job && ($job instanceof Job)) { $fork->call(function () use ($worker, $job) { call_user_func($worker, $job); }); try { $fork->wait(); try { $job->delete(); } catch (\Exception $e) { if (null !== $this->logger) { $this->logger->warning(sprintf( 'Exception thrown while deleting the job: %d — %s', $e->getCode(), $e->getMessage() )); } } } catch (\Exception $e) { if (null !== $this->logger) { $this->logger->warning(sprintf( 'Exception thrown while handling job #%s: %d — %s', $job->getId(), $e->getCode(), $e->getMessage() )); } if (!$ignoreErrors) { return; } } } else { // There is no jobs so let's sleep to not increase CPU usage usleep(rand(7000, 10000)); } } while (true); exit(0); }); } $fork->wait(); } /** * Puts a job on the queue using specified tube. * * @param string $tube * @param string $data * @param array $options * @return string|boolean job id or false */ public function putInTube($tube, $data, $options = null) { if (null === $options) { $options = array(); } if (!array_key_exists('delay', $options)) { $options['delay'] = self::DEFAULT_DELAY; } if (!array_key_exists('priority', $options)) { $options['priority'] = self::DEFAULT_PRIORITY; } if (!array_key_exists('ttr', $options)) { $options['ttr'] = self::DEFAULT_TTR; } $this->choose($this->getTubeName($tube)); return parent::put($data, $options); } /** * Reserves/locks a ready job from the specified tube. * * @param string $tube * @param integer $timeout * @return boolean|\Phalcon\Queue\Beanstalk\Job */ public function reserveFromTube($tube, $timeout = null) { $this->watch($this->getTubeName($tube)); return parent::reserve($timeout); } /** * Returns the names of all tubes on the server. * * @return array */ public function getTubes() { $result = array(); $lines = $this->getResponseLines('list-tubes'); if (null !== $lines) { foreach ($lines as $line) { $line = ltrim($line, '- '); if (empty($this->tubePrefix) || (0 === strpos($line, $this->tubePrefix))) { $result[] = !empty($this->tubePrefix) ? substr($line, strlen($this->tubePrefix)) : $line; } } } return $result; } /** * Returns information about the specified tube if it exists. * * @param string $tube * @return null|array */ public function getTubeStats($tube) { $result = null; $lines = $this->getResponseLines('stats-tube ' . $this->getTubeName($tube)); if (!empty($lines)) { foreach ($lines as $line) { if (false !== strpos($line, ':')) { list($name, $value) = explode(':', $line); if (null !== $value) { $result[$name] = intval($value); } } } } return $result; } /** * Returns information about specified job if it exists * * @param int $job_id * @return null|array */ public function getJobStats($job_id) { $result = null; $lines = $this->getResponseLines('stats-job ' . (int)$job_id); if (!empty($lines)) { foreach ($lines as $line) { if (false !== strpos($line, ':')) { list($name, $value) = explode(':', $line); if (null !== $value) { $result[$name] = intval($value); } } } } return $result; } /** * The kick-job command is a variant of kick that operates with a single * job identified by its job id. If the given job id exists and is in a * buried or delayed state, it will be moved to the ready queue of the the * same tube where it currently belongs. * * @param int $job_id is the job id to kick. * @return boolean */ public function jobKick($job_id) { $this->write(self::CMD_JOBKICK . $job_id); $response = $this->readStatus(); if ($response[0] != self::MSG_KICKED) { return false; } return true; } /** * The stats-job command gives statistical information about the specified * job if it exists. * * <i>return array:</i><br><br> * <b>id</b> is the job id<br> * <b>tube</b> is the name of the tube that contains this job<br> * <b>state</b> is ready or delayed or reserved or buried<br> * <b>pri</b> is the priority value set by the put, release, or bury commands.<br> * <b>age</b> is the time in seconds since the put command that created this job.<br> * <b>time-left</b> is the number of seconds left until the server puts this job into the ready queue. This number is only meaningful if the job is reserved or delayed. If the job is reserved and this amount of time elapses before its state changes, it is considered to have timed out.<br> * <b>file</b> is the number of the earliest binlog file containing this job. If -b wasn't used, this will be 0.<br> * <b>reserves</b> is the number of times this job has been reserved.<br> * <b>timeouts</b> is the number of times this job has timed out during a reservation.<br> * <b>releases</b> is the number of times a client has released this job from a reservation.<br> * <b>buries</b> is the number of times this job has been buried.<br> * <b>kicks</b> is the number of times this job has been kicked.<br> * * @param int $job_id is a job id. * @return boolean | array */ public function jobStats($job_id) { $this->write(self::CMD_JOBSTATS . $job_id); $response = $this->readYaml(); if ($response[0] != self::MSG_OK) { return false; } return $response[2]; } /** * Returns the number of tube watched by current session. * Example return array: array('WATCHED' => 1) * Added on 10-Jan-2014 20:04 IST by Tapan Kumar Thapa @ tapan.thapa@yahoo.com * * @param string $tube * @return null|array */ public function ignoreTube($tube) { $result = null; $lines = $this->getWatchingResponse('ignore ' . $this->getTubeName($tube)); if (!empty($lines)) { list($name, $value) = explode(' ', $lines); if (null !== $value) { $result[$name] = intval($value); } } return $result; } /** * Returns the tube name with prefix. * * @param string|null $tube * @return string */ protected function getTubeName($tube) { if ((null !== $this->tubePrefix) && (null !== $tube)) { $tube = str_replace($this->tubePrefix, '', $tube); if (0 !== strcmp($tube, 'default')) { return $this->tubePrefix . $tube; } } return $tube; } /** * Returns the result of command that wait the list in response from beanstalkd. * * @param string $cmd * @return array|null * @throws \RuntimeException */ protected function getResponseLines($cmd) { $result = null; $this->write(trim($cmd)); $response = $this->read(); $matches = array(); if (!preg_match('#^(OK (\d+))#mi', $response, $matches)) { throw new \RuntimeException(sprintf( 'Unhandled response: %s', $response )); } $result = preg_split("#[\r\n]+#", rtrim($this->read($matches[2]))); // discard header line if (isset($result[0]) && $result[0] == '---') { array_shift($result); } return $result; } /** * Returns the result of command that wait the list in response from beanstalkd. * Added on 10-Jan-2014 20:04 IST by Tapan Kumar Thapa @ tapan.thapa@yahoo.com * * @param string $cmd * @return string|null * @throws \RuntimeException */ protected function getWatchingResponse($cmd) { $result = null; $nbBytes = $this->write($cmd); if ($nbBytes && ($nbBytes > 0)) { $response = $this->read($nbBytes); $matches = array(); if (!preg_match('#^WATCHING (\d+).*?#', $response, $matches)) { throw new \RuntimeException(sprintf( 'Unhandled response: %s', $response )); } $result = $response; } return $result; } public function test() { $rW = $this->write("stats-job 10"); $response = $this->readYaml(); if ($response[0] != "RESERVED") { return false; } return $response[2]; } }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.0.110.0000.00817.02
8.0.100.0070.00016.98
8.0.90.0040.00417.03
8.0.80.0100.00617.17
8.0.70.0040.00417.14
8.0.60.0000.00817.02
8.0.50.0000.00817.10
8.0.30.0030.01517.24
8.0.20.0080.01117.40
8.0.10.0060.00316.99
8.0.00.0100.01116.83
7.4.240.0040.00416.63
7.4.230.0040.00416.40
7.4.220.0070.01816.81
7.4.210.0060.01016.47
7.4.200.0040.00416.51
7.4.160.0000.01716.81
7.4.150.0170.00317.40
7.4.140.0120.01017.86
7.4.130.0110.00716.68
7.4.120.0130.00716.63
7.4.110.0030.01516.58
7.4.100.0190.00616.90
7.4.90.0210.00316.72
7.4.80.0070.01419.39
7.4.70.0120.00616.82
7.4.60.0130.00316.52
7.4.50.0030.00316.67
7.4.40.0060.01216.64
7.4.30.0120.01216.55
7.4.00.0060.00915.14
7.3.300.0040.00416.31
7.3.290.0040.01216.42
7.3.280.0080.01016.43
7.3.270.0090.00917.40
7.3.260.0120.01216.80
7.3.250.0070.01116.58
7.3.240.0200.00316.35
7.3.230.0030.01316.60
7.3.210.0140.00416.65
7.3.200.0000.02116.48
7.3.190.0040.01216.58
7.3.180.0060.01516.61
7.3.170.0120.00916.51
7.3.160.0070.01016.52
7.3.10.0030.01016.63
7.3.00.0090.00616.59
7.2.330.0030.01716.47
7.2.320.0110.01416.84
7.2.310.0070.01016.66
7.2.300.0000.01816.52
7.2.290.0060.01916.50
7.2.130.0070.01016.45
7.2.120.0030.01216.71
7.2.110.0040.00816.45
7.2.100.0090.00616.64
7.2.90.0040.01116.82
7.2.80.0070.00716.77
7.2.70.0150.00316.84
7.2.60.0040.01216.75
7.2.50.0060.00616.60
7.2.40.0030.01016.50
7.2.30.0000.01416.73
7.2.20.0050.00516.95
7.2.10.0070.01016.58
7.2.00.0070.00718.01
7.1.250.0060.00915.60
7.1.200.0040.00715.65
7.1.100.0000.01418.00
7.1.70.0000.00716.81
7.1.60.0100.01619.50
7.1.50.0090.01316.94
7.1.00.0030.07722.51
7.0.200.0040.00416.69
7.0.100.0200.07020.00
7.0.90.0130.04320.00
7.0.80.0200.06720.05
7.0.70.0070.07720.23
7.0.60.0070.04319.95
7.0.50.0130.07320.45
7.0.40.0100.08320.00
7.0.30.0100.06720.06
7.0.20.0200.07720.12
7.0.10.0100.04320.05
7.0.00.0130.05720.01
5.6.280.0030.03320.96
5.6.250.0100.07020.71
5.6.240.0130.05320.60
5.6.230.0100.08320.55
5.6.220.0030.08320.63
5.6.210.0070.04320.63
5.6.200.0100.08321.16
5.6.190.0100.08321.27
5.6.180.0100.04721.21
5.6.170.0070.05721.27
5.6.160.0100.07721.09
5.6.150.0070.08321.16
5.6.140.0030.05721.20
5.6.130.0100.07721.21
5.6.120.0030.09021.11
5.6.110.0070.08021.22
5.6.100.0100.08321.11
5.6.90.0100.07321.19
5.6.80.0070.05320.57
5.6.70.0100.06320.42
5.6.60.0100.05320.64
5.6.50.0100.08020.49
5.6.40.0130.07320.54
5.6.30.0130.07320.51
5.6.20.0030.08020.50
5.6.10.0100.08020.59
5.6.00.0170.07720.38
5.5.380.0030.07320.46
5.5.370.0070.07720.54
5.5.360.0100.07720.39
5.5.350.0070.08720.44
5.5.340.0170.07020.84
5.5.330.0130.07321.04
5.5.320.0070.04720.89
5.5.310.0070.07720.93
5.5.300.0100.08320.91
5.5.290.0030.08721.01
5.5.280.0100.05720.88
5.5.270.0070.07020.98
5.5.260.0070.08321.02
5.5.250.0270.06320.75
5.5.240.0030.08320.41
5.5.230.0070.07320.42
5.5.220.0170.06020.13
5.5.210.0130.06720.25
5.5.200.0100.07320.35
5.5.190.0130.08020.36
5.5.180.0100.07320.34
5.5.160.0100.05320.36
5.5.150.0100.07020.39
5.5.140.0100.07320.23
5.5.130.0200.06720.33
5.5.120.0070.07320.32
5.5.110.0100.06020.38
5.5.100.0070.04320.23
5.5.90.0100.07020.23
5.5.80.0130.06720.26
5.5.70.0030.05720.14
5.5.60.0030.04320.24
5.5.50.0000.04720.23
5.5.40.0170.07020.17
5.5.30.0070.07720.02
5.5.20.0130.07320.06
5.5.10.0100.07020.14
5.5.00.0170.06720.15
5.4.450.0070.05019.40
5.4.440.0030.08719.45
5.4.430.0070.08319.39
5.4.420.0070.08019.38
5.4.410.0000.05019.13
5.4.400.0100.07719.22
5.4.390.0100.04319.22
5.4.380.0100.07719.07
5.4.370.0170.07018.93
5.4.360.0070.07719.06
5.4.350.0200.06018.99
5.4.340.0170.06018.99
5.4.320.0070.07019.09
5.4.310.0100.04019.13
5.4.300.0070.06719.07
5.4.290.0100.05319.09
5.4.280.0100.08019.07
5.4.270.0000.08319.09
5.4.260.0170.04719.15
5.4.250.0100.04319.08
5.4.240.0030.05019.14
5.4.230.0100.05019.08
5.4.220.0100.03719.05
5.4.210.0070.07319.07
5.4.200.0100.07019.08
5.4.190.0030.08719.11
5.4.180.0030.04319.12
5.4.170.0130.06018.87
5.4.160.0030.07019.04
5.4.150.0000.07318.91
5.4.140.0030.07016.50
5.4.130.0270.05016.32
5.4.120.0070.07316.51
5.4.110.0070.04716.54
5.4.100.0070.05716.51
5.4.90.0000.04716.56
5.4.80.0070.04716.40
5.4.70.0000.04016.41
5.4.60.0070.03316.35
5.4.50.0100.03016.43
5.4.40.0070.03716.53
5.4.30.0130.03016.38
5.4.20.0070.03316.52
5.4.10.0100.03316.52
5.4.00.0000.04015.98

preferences:
48.59 ms | 401 KiB | 5 Q