php-beanstalkd消息队列类分享

<?php
namespace Common\Business;
/**
 * beanstalk: A minimalistic PHP beanstalk client.
 *
 * Copyright (c) 2009-2015 David Persson
 *
 * Distributed under the terms of the MIT License.
 * Redistributions of files must retain the above copyright notice.
 */

use RuntimeException;

/**
 * An interface to the beanstalk queue service. Implements the beanstalk
 * protocol spec 1.9. Where appropriate the documentation from the protocol
 * has been added to the docblocks in this class.
 *
 * @link https://github.com/kr/beanstalkd/blob/master/doc/protocol.txt
 */
class BeanStalk {

	/**
	 * Minimum priority value which can be assigned to a job. The minimum
	 * priority value is also the _highest priority_ a job can have.
	 *
	 * @var integer
	 */
	const MIN_PRIORITY = 0;

	/**
	 * Maximum priority value which can be assigned to a job. The maximum
	 * priority value is also the _lowest priority_ a job can have.
	 *
	 * @var integer
	 */
	const MAX_PRIORITY = 4294967295;

	/**
	 * Holds a boolean indicating whether a connection to the server is
	 * currently established or not.
	 *
	 * @var boolean
	 */
	public $connected = false;

	/**
	 * Holds configuration values.
	 *
	 * @var array
	 */
	protected $_config = [];

	/**
	 * The current connection resource handle (if any).
	 *
	 * @var resource
	 */
	protected $_connection;

	/**
	 * Constructor.
	 *
	 * @param array $config An array of configuration values:
	 *        - `‘persistent‘`  Whether to make the connection persistent or
	 *                          not, defaults to `true` as the FAQ recommends
	 *                          persistent connections.
	 *        - `‘host‘`        The beanstalk server hostname or IP address to
	 *                          connect to, defaults to `127.0.0.1`.
	 *        - `‘port‘`        The port of the server to connect to, defaults
	 *                          to `11300`.
	 *        - `‘timeout‘`     Timeout in seconds when establishing the
	 *                          connection, defaults to `1`.
	 *        - `‘logger‘`      An instance of a PSR-3 compatible logger.
	 *
	 * @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
	 * @return void
	 */
	public function __construct(array $config = []) {
		$defaults = [
			‘persistent‘ => true,
			‘host‘ => ‘127.0.0.1‘,
			‘port‘ => 11300,
			‘timeout‘ => 1,
			‘logger‘ => null
		];
		$this->_config = $config + $defaults;
	}

	/**
	 * Destructor, disconnects from the server.
	 *
	 * @return void
	 */
	public function __destruct() {
		$this->disconnect();
	}

	/**
	 * Initiates a socket connection to the beanstalk server. The resulting
	 * stream will not have any timeout set on it. Which means it can wait
	 * an unlimited amount of time until a packet becomes available. This
	 * is required for doing blocking reads.
	 *
	 * @see \Beanstalk\Client::$_connection
	 * @see \Beanstalk\Client::reserve()
	 * @return boolean `true` if the connection was established, `false` otherwise.
	 */
	public function connect() {
		if (isset($this->_connection)) {
			$this->disconnect();
		}
        $errNum = ‘‘;
        $errStr = ‘‘;
		$function = $this->_config[‘persistent‘] ? ‘pfsockopen‘ : ‘fsockopen‘;
		$params = [$this->_config[‘host‘], $this->_config[‘port‘], &$errNum, &$errStr];

		if ($this->_config[‘timeout‘]) {
			$params[] = $this->_config[‘timeout‘];
		}
		$this->_connection = @call_user_func_array($function, $params);

		if (!empty($errNum) || !empty($errStr)) {
			$this->_error("{$errNum}: {$errStr}");
		}

		$this->connected = is_resource($this->_connection);

		if ($this->connected) {
			stream_set_timeout($this->_connection, -1);
		}
		return $this->connected;
	}

	/**
	 * Closes the connection to the beanstalk server by first signaling
	 * that we want to quit then actually closing the socket connection.
	 *
	 * @return boolean `true` if diconnecting was successful.
	 */
	public function disconnect() {
		if (!is_resource($this->_connection)) {
			$this->connected = false;
		} else {
			$this->_write(‘quit‘);
			$this->connected = !fclose($this->_connection);

			if (!$this->connected) {
				$this->_connection = null;
			}
		}
		return !$this->connected;
	}

	/**
	 * Pushes an error message to the logger, when one is configured.
	 *
	 * @param string $message The error message.
	 * @return void
	 */
	protected function _error($message) {
		if ($this->_config[‘logger‘]) {
			$this->_config[‘logger‘]->error($message);
		}
	}

	public function errors()
	{
		return $this->_config[‘logger‘];
	}
	/**
	 * Writes a packet to the socket. Prior to writing to the socket will
	 * check for availability of the connection.
	 *
	 * @param string $data
	 * @return integer|boolean number of written bytes or `false` on error.
	 */
	protected function _write($data) {
		if (!$this->connected) {
			$message = ‘No connecting found while writing data to socket.‘;
			throw new RuntimeException($message);
		}

		$data .= "\r\n";
		return fwrite($this->_connection, $data, strlen($data));
	}

	/**
	 * Reads a packet from the socket. Prior to reading from the socket
	 * will check for availability of the connection.
	 *
	 * @param integer $length Number of bytes to read.
	 * @return string|boolean Data or `false` on error.
	 */
	protected function _read($length = null) {
		if (!$this->connected) {
			$message = ‘No connection found while reading data from socket.‘;
			throw new RuntimeException($message);
		}
		if ($length) {
			if (feof($this->_connection)) {
				return false;
			}
			$data = stream_get_contents($this->_connection, $length + 2);
			$meta = stream_get_meta_data($this->_connection);

			if ($meta[‘timed_out‘]) {
				$message = ‘Connection timed out while reading data from socket.‘;
				throw new RuntimeException($message);
			}
			$packet = rtrim($data, "\r\n");
		} else {
			$packet = stream_get_line($this->_connection, 16384, "\r\n");
		}
		return $packet;
	}

	/* Producer Commands */

	/**
	 * The `put` command is for any process that wants to insert a job into the queue.
	 *
	 * @param integer $pri 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.
	 * @param integer $delay Seconds to wait before putting the job in the
	 *        ready queue.  The job will be in the "delayed" state during this time.
	 * @param integer $ttr Time to run - Number of seconds to allow a worker to
	 *        run this job.  The minimum ttr is 1.
	 * @param string $data The job body.
	 * @return integer|boolean `false` on error otherwise an integer indicating
	 *         the job id.
	 */
	public function put($pri, $delay, $ttr, $data) {
		$this->_write(sprintf("put %d %d %d %d\r\n%s", $pri, $delay, $ttr, strlen($data), $data));
		$status = strtok($this->_read(), ‘ ‘);

		switch ($status) {
			case ‘INSERTED‘:
			case ‘BURIED‘:
				return (integer) strtok(‘ ‘); // job id
			case ‘EXPECTED_CRLF‘:
			case ‘JOB_TOO_BIG‘:
			default:
				$this->_error($status);
				return false;
		}
	}

	/**
	 * The `use` command is for producers. Subsequent put commands will put
	 * jobs into the tube specified by this command. If no use command has
	 * been issued, jobs will be put into the tube named `default`.
	 *
	 * @param string $tube A name at most 200 bytes. It specifies the tube to
	 *        use. If the tube does not exist, it will be created.
	 * @return string|boolean `false` on error otherwise the name of the tube.
	 */
	public function useTube($tube) {
		$this->_write(sprintf(‘use %s‘, $tube));
		$status = strtok($this->_read(), ‘ ‘);

		switch ($status) {
			case ‘USING‘:
				return strtok(‘ ‘);
			default:
				$this->_error($status);
				return false;
		}
	}

	/**
	 * Pause a tube delaying any new job in it being reserved for a given time.
	 *
	 * @param string $tube The name of the tube to pause.
	 * @param integer $delay Number of seconds to wait before reserving any more
	 *        jobs from the queue.
	 * @return boolean `false` on error otherwise `true`.
	 */
	public function pauseTube($tube, $delay) {
		$this->_write(sprintf(‘pause-tube %s %d‘, $tube, $delay));
		$status = strtok($this->_read(), ‘ ‘);

		switch ($status) {
			case ‘PAUSED‘:
				return true;
			case ‘NOT_FOUND‘:
			default:
				$this->_error($status);
				return false;
		}
	}

	/* Worker Commands */

	/**
	 * Reserve a job (with a timeout).
	 *
	 * @param integer $timeout If given specifies number of seconds to wait for
	 *        a job. `0` returns immediately.
	 * @return array|false `false` on error otherwise an array holding job id
	 *         and body.
	 */
	public function reserve($timeout = null) {
		if (isset($timeout)) {
			$this->_write(sprintf(‘reserve-with-timeout %d‘, $timeout));
		} else {
			$this->_write(‘reserve‘);
		}
		$status = strtok($this->_read(), ‘ ‘);

		switch ($status) {
			case ‘RESERVED‘:
				return [
					‘id‘ => (integer) strtok(‘ ‘),
					‘body‘ => $this->_read((integer) strtok(‘ ‘))
				];
			case ‘DEADLINE_SOON‘:
			case ‘TIMED_OUT‘:
			default:
				$this->_error($status);
				return false;
		}
	}

	/**
	 * Removes a job from the server entirely.
	 *
	 * @param integer $id The id of the job.
	 * @return boolean `false` on error, `true` on success.
	 */
	public function delete($id) {
		$this->_write(sprintf(‘delete %d‘, $id));
		$status = $this->_read();

		switch ($status) {
			case ‘DELETED‘:
				return true;
			case ‘NOT_FOUND‘:
			default:
				$this->_error($status);
				return false;
		}
	}

	/**
	 * Puts a reserved job back into the ready queue.
	 *
	 * @param integer $id The id of the job.
	 * @param integer $pri Priority to assign to the job.
	 * @param integer $delay Number of seconds to wait before putting the job in the ready queue.
	 * @return boolean `false` on error, `true` on success.
	 */
	public function release($id, $pri, $delay) {
		$this->_write(sprintf(‘release %d %d %d‘, $id, $pri, $delay));
		$status = $this->_read();

		switch ($status) {
			case ‘RELEASED‘:
			case ‘BURIED‘:
				return true;
			case ‘NOT_FOUND‘:
			default:
				$this->_error($status);
				return false;
		}
	}

	/**
	 * Puts a job into the `buried` state Buried jobs are put into a FIFO
	 * linked list and will not be touched until a client kicks them.
	 *
	 * @param integer $id The id of the job.
	 * @param integer $pri *New* priority to assign to the job.
	 * @return boolean `false` on error, `true` on success.
	 */
	public function bury($id, $pri) {
		$this->_write(sprintf(‘bury %d %d‘, $id, $pri));
		$status = $this->_read();

		switch ($status) {
			case ‘BURIED‘:
				return true;
			case ‘NOT_FOUND‘:
			default:
				$this->_error($status);
				return false;
		}
	}

	/**
	 * Allows a worker to request more time to work on a job.
	 *
	 * @param integer $id The id of the job.
	 * @return boolean `false` on error, `true` on success.
	 */
	public function touch($id) {
		$this->_write(sprintf(‘touch %d‘, $id));
		$status = $this->_read();

		switch ($status) {
			case ‘TOUCHED‘:
				return true;
			case ‘NOT_TOUCHED‘:
			default:
				$this->_error($status);
				return false;
		}
	}

	/**
	 * Adds the named tube to the watch list for the current connection.
	 *
	 * @param string $tube Name of tube to watch.
	 * @return integer|boolean `false` on error otherwise number of tubes in watch list.
	 */
	public function watch($tube) {
		$this->_write(sprintf(‘watch %s‘, $tube));
		$status = strtok($this->_read(), ‘ ‘);

		switch ($status) {
			case ‘WATCHING‘:
				return (integer) strtok(‘ ‘);
			default:
				$this->_error($status);
				return false;
		}
	}

	/**
	 * Remove the named tube from the watch list.
	 *
	 * @param string $tube Name of tube to ignore.
	 * @return integer|boolean `false` on error otherwise number of tubes in watch list.
	 */
	public function ignore($tube) {
		$this->_write(sprintf(‘ignore %s‘, $tube));
		$status = strtok($this->_read(), ‘ ‘);

		switch ($status) {
			case ‘WATCHING‘:
				return (integer) strtok(‘ ‘);
			case ‘NOT_IGNORED‘:
			default:
				$this->_error($status);
				return false;
		}
	}

	/* Other Commands */

	/**
	 * Inspect a job by its id.
	 *
	 * @param integer $id The id of the job.
	 * @return string|boolean `false` on error otherwise the body of the job.
	 */
	public function peek($id) {
		$this->_write(sprintf(‘peek %d‘, $id));
		return $this->_peekRead();
	}

	/**
	 * Inspect the next ready job.
	 *
	 * @return string|boolean `false` on error otherwise the body of the job.
	 */
	public function peekReady() {
		$this->_write(‘peek-ready‘);
		return $this->_peekRead();
	}

	/**
	 * Inspect the job with the shortest delay left.
	 *
	 * @return string|boolean `false` on error otherwise the body of the job.
	 */
	public function peekDelayed() {
		$this->_write(‘peek-delayed‘);
		return $this->_peekRead();
	}

	/**
	 * Inspect the next job in the list of buried jobs.
	 *
	 * @return string|boolean `false` on error otherwise the body of the job.
	 */
	public function peekBuried() {
		$this->_write(‘peek-buried‘);
		return $this->_peekRead();
	}

	/**
	 * Handles response for all peek methods.
	 *
	 * @return string|boolean `false` on error otherwise the body of the job.
	 */
	protected function _peekRead() {
		$status = strtok($this->_read(), ‘ ‘);

		switch ($status) {
			case ‘FOUND‘:
				return [
					‘id‘ => (integer) strtok(‘ ‘),
					‘body‘ => $this->_read((integer) strtok(‘ ‘))
				];
			case ‘NOT_FOUND‘:
			default:
				$this->_error($status);
				return false;
		}
	}

	/**
	 * Moves jobs into the ready queue (applies to the current tube).
	 *
	 * If there are buried jobs those get kicked only otherwise delayed
	 * jobs get kicked.
	 *
	 * @param integer $bound Upper bound on the number of jobs to kick.
	 * @return integer|boolean False on error otherwise number of jobs kicked.
	 */
	public function kick($bound) {
		$this->_write(sprintf(‘kick %d‘, $bound));
		$status = strtok($this->_read(), ‘ ‘);

		switch ($status) {
			case ‘KICKED‘:
				return (integer) strtok(‘ ‘);
			default:
				$this->_error($status);
				return false;
		}
	}

	/**
	 * This is a variant of the kick command 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 integer $id The job id.
	 * @return boolean `false` on error `true` otherwise.
	 */
	public function kickJob($id) {
		$this->_write(sprintf(‘kick-job %d‘, $id));
		$status = strtok($this->_read(), ‘ ‘);

		switch ($status) {
			case ‘KICKED‘:
				return true;
			case ‘NOT_FOUND‘:
			default:
				$this->_error($status);
				return false;
		}
	}

	/* Stats Commands */

	/**
	 * Gives statistical information about the specified job if it exists.
	 *
	 * @param integer $id The job id.
	 * @return string|boolean `false` on error otherwise a string with a yaml formatted dictionary.
	 */
	public function statsJob($id) {
		$this->_write(sprintf(‘stats-job %d‘, $id));
		return $this->_statsRead();
	}

	/**
	 * Gives statistical information about the specified tube if it exists.
	 *
	 * @param string $tube Name of the tube.
	 * @return string|boolean `false` on error otherwise a string with a yaml formatted dictionary.
	 */
	public function statsTube($tube) {
		$this->_write(sprintf(‘stats-tube %s‘, $tube));
		return $this->_statsRead();
	}

	/**
	 * Gives statistical information about the system as a whole.
	 *
	 * @return string|boolean `false` on error otherwise a string with a yaml formatted dictionary.
	 */
	public function stats() {
		$this->_write(‘stats‘);
		return $this->_statsRead();
	}

	/**
	 * Returns a list of all existing tubes.
	 *
	 * @return string|boolean `false` on error otherwise a string with a yaml formatted list.
	 */
	public function listTubes() {
		$this->_write(‘list-tubes‘);
		return $this->_statsRead();
	}

	/**
	 * Returns the tube currently being used by the producer.
	 *
	 * @return string|boolean `false` on error otherwise a string with the name of the tube.
	 */
	public function listTubeUsed() {
		$this->_write(‘list-tube-used‘);
		$status = strtok($this->_read(), ‘ ‘);

		switch ($status) {
			case ‘USING‘:
				return strtok(‘ ‘);
			default:
				$this->_error($status);
				return false;
		}
	}

	/**
	 * Returns a list of tubes currently being watched by the worker.
	 *
	 * @return string|boolean `false` on error otherwise a string with a yaml formatted list.
	 */
	public function listTubesWatched() {
		$this->_write(‘list-tubes-watched‘);
		return $this->_statsRead();
	}

	/**
	 * Handles responses for all stat methods.
	 *
	 * @param boolean $decode Whether to decode data before returning it or not. Default is `true`.
	 * @return array|string|boolean `false` on error otherwise statistical data.
	 */
	protected function _statsRead($decode = true) {
		$status = strtok($this->_read(), ‘ ‘);

		switch ($status) {
			case ‘OK‘:
				$data = $this->_read((integer) strtok(‘ ‘));
				return $decode ? $this->_decode($data) : $data;
			default:
				$this->_error($status);
				return false;
		}
	}

	/**
	 * Decodes YAML data. This is a super naive decoder which just works on
	 * a subset of YAML which is commonly returned by beanstalk.
	 *
	 * @param string $data The data in YAML format, can be either a list or a dictionary.
	 * @return array An (associative) array of the converted data.
	 */
	protected function _decode($data) {
		$data = array_slice(explode("\n", $data), 1);
		$result = [];

		foreach ($data as $key => $value) {
			if ($value[0] === ‘-‘) {
				$value = ltrim($value, ‘- ‘);
			} elseif (strpos($value, ‘:‘) !== false) {
				list($key, $value) = explode(‘:‘, $value);
				$value = ltrim($value, ‘ ‘);
			}
			if (is_numeric($value)) {
				$value = (integer) $value == $value ? (integer) $value : (float) $value;
			}
			$result[$key] = $value;
		}
		return $result;
	}
}

?>

  

时间: 2024-10-05 03:55:24

php-beanstalkd消息队列类分享的相关文章

PHP memcache实现消息队列实例

现在,memcache于server缓存广泛应用.下面我来介绍一下memcache消息队列中等待的样本实现,有需要了解的朋友可以参考. memche消息队列原则key上做文章.后消息或者日志. 然后通过定时程序将内容落地到文件或者数据库. php实现消息队列的用处比方在做发送邮件时发送大量邮件非常费时间的问题.那么能够採取队列. 方便实现队列的轻量级队列server是: starling支持memcache协议的轻量级持久化server https://github.com/starling/s

php memcache或是memcached 来实现消息队列

现在memcache在服务器缓存应用比较广泛,下面我来介绍memcache实现消息队列等待的一个例子,有需要了解的朋友可参考. memche消息队列的原理就是在key上做文章,用以做一个连续的数字加上前缀记录序列化以后消息或者日志.然后通过定时程序将内容落地到文件或者数据库. php实现消息队列的用处比如在做发送邮件时发送大量邮件很费时间的问题,那么可以采取队列.方便实现队列的轻量级队列服务器是:starling支持memcache协议的轻量级持久化服务器https://github.com/s

用STL设计消息队列、优先级消息队列、资源分配管理器

STL库老早已经成为C++的一部分,在使用C++开发项目的过程中,很多人还在犹豫要不要使用STL库,觉得STL库很难,其实不然.我工作的项目中现在大量使用STL库,STL使用调试简单,高效,可以减少很多重复的代码. 本文的主要目的是使用STL的queue 和 priority queue来阐述下项目中经常使用的消息队列以及资源分配模式.本文的例子主要如下: 消息队列 带优先级的消息队列 资源分配管理器 STL容器 我们将使用下面的容器来实现本文的例子: queue 队列容器支持添加一个元素,并且

微信后台异步消息队列的优化升级实践分享

1.引言 MQ 异步消息队列是微信后台自研的重要组件,广泛应用在各种业务场景中,为业务提供解耦.缓冲.异步化等能力.本文分享了该组件2.0版本的功能特点及优化实践,希望能为类似业务(比如移动端IM系统等)的消息队列设计提供一定的参考. 2.关于分享者 廖文鑫,2013年加入腾讯,从事微信后台基础功能及架构的开发和运营,先后参与了消息通知推送系统.任务队列组件.春晚摇红包活动等项目,在海量分布式高性能系统方面有丰富的经验. 3.背景介绍 微信后台给件 MQ 1.0 发布之初,基本满足了一般业务场景

消息队列_Beanstalkd-0001.Beanstalkd之轻量级分布式内存队列部署?

简单介绍: 说明: Beantalkd是一个高性能,轻量级的分布式消息队列,最初设计目的是想通过后台异步执行耗时任务降低WEB应用页面访问延迟,支持过1000万用户的应用,被豆瓣内部广泛使用. 几大特性: 1. 支持持久化,默认使用内存,但可启动时-b指定持久化目录,将任务写入Binlog,以相同参数启动会自动恢复Binlog中内容 2. 支持优先级0~2^32,任务优先级越小表示优先级越高,默认优先级为1024 3. 支持超时重发,预设过期时间或TTR时间内如果没有发送delete/relea

RabbitMqHelper 消息队列帮助类

using Newtonsoft.Json;using RabbitMQ.Client;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace RabbitMQ_Send{ class ConfigModel { } public enum ExchangeTypeEnum { /// <summary> /

系统学习消息队列分享(一) 怎样系统学习消息队列?

从系统之间有通信需求开始呢,就产生了消息队列,它也是最古老的中间件之一.它的应用场景非常广泛,分布式系统中的很多进程间通信问题,都可以用消息队列来解决.可以说消息队列是所有后端程序员的必备技能.但是,想要系统.深入地学习消息队列,却并不容易. 要了解消息队列的完整知识体系,想深度进阶为消息队列达人,从理论到实践,从基础到进阶,从深度到广度,全方位吃透消息队列. 哪些人适合学消息队列? 后端开发者:消息队列几乎是每个后端程序员都会用到的中间件,无论你是开发微服务,实时计算,还是机器学习程序,都需要

【阿里云产品公测】消息队列服务MQS使用分享

作者:阿里云用户 wiwi 消息队列MQS,顾名思义,是用于发送接收消息用的.废话不说,直接进入主题. 使用场景:服务添加了一个新功能,主要用于生成图片,本人用的开发语言是PHP,生成图片比较耗服务器性能,如果使用一台服务器生成的话,生成大量图片时速度就会很慢. 由此决定使用3台服务器同时处理,将任务分布到3台服务器中.另外有一台服务器用于提交任务. mqs支持多个生产者.多个消费者并发访问一个队列(本例是一个生产者,3个消费者). 简单的服务器部署情况如下图: 具体的代码实现这里就不再说明了,

系统学习消息队列分享(二) 为什么需要消息队列?

消息队列是最古老的中间件之一,从系统之间有通信需求开始,就自然产生了消息队列.但是给消息队列下一个准确的定义却不太容易.我们知道,消息队列的主要功能就是收发消息,但是它的作用不仅仅只是解决应用之间的通信问题这么简单. 我们举个例子说明一下消息队列的作用.话说小袁是一家巧克力作坊的老板,生产出美味的巧克力需要三道工序:首先将可可豆磨成可可粉,然后将可可粉加热并加入糖变成巧克力浆,最后将巧克力浆灌入模具,撒上坚果碎,冷却后就是成品巧克力了. 最开始的时候,每次研磨出一桶可可粉后,工人就会把这桶可可粉