Skip to content

Commit

Permalink
Merge pull request phalcon#11317 from sergeyklay/beanstalk/persistent
Browse files Browse the repository at this point in the history
Ability to persistent connection in Beanstalk::connect
  • Loading branch information
andresgutierrez committed Jan 17, 2016
2 parents b5841b6 + f5d9d6b commit 796d755
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 24 deletions.
17 changes: 9 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# [2.0.10](https://github.com/phalcon/cphalcon/releases/tag/phalcon-v2.0.10) (2015-XX-XX)
- ORM: Added support for DATE columns in Oracle
- Fixed wrong total_items and total_pages in Paginator when the query builder has set groupBy()
- Fixed `Phalcon\Acl\Memory::allow` bug[#11210] related to the inconsistent behavior with access specified as string and array
- Fixed wrong `total_items` and `total_pages` in `Paginator` when the query builder has set `groupBy()`
- Fixed `Phalcon\Acl\Memory::allow` bug[#11210](https://github.com/phalcon/cphalcon/issues/11210) related to the inconsistent behavior with access specified as string and array
- Added quoting column in `Phalcon\Db\Dialect\MySQL::addColumn` when define position of the column
- Added support to define position of the column in `Phalcon\Db\Dialect\MySQL::modifyColumn`
- Fixed `Phalcon\Mvc\Model\Query\Builder` bug[#11298] related to resetting limit to null
- Fixed `Phalcon\Tag::getTitle` bug[#11185]. Now a title will be automatically escaped.
- Fixed `Gettext::exists` bug[#11310] related to the wrong returned value (always true)
- Fixed `Gettext::setLocale` bug[#11311] related to the incorrect setting locale
- Fixed `Phalcon\Mvc\Model\Query\Builder` bug[#11298](https://github.com/phalcon/cphalcon/issues/11298) related to resetting limit to null
- Fixed `Phalcon\Tag::getTitle` bug[#11185](https://github.com/phalcon/cphalcon/issues/11185). Now a title will be automatically escaped.
- Fixed `Phalcon\Translate\Adapter\Gettext::exists` bug[#11310](https://github.com/phalcon/cphalcon/issues/11310) related to the wrong returned value (always true)
- Fixed `Phalcon\Translate\Adapter\Gettext::setLocale` bug[#11311](https://github.com/phalcon/cphalcon/issues/11311) related to the incorrect setting locale
- Added ability to persistent connection in `Phalcon\Queue\Beanstalk::connect`

# [2.0.9](https://github.com/phalcon/cphalcon/releases/tag/phalcon-v2.0.9) (2015-11-24)
- Fixed bug that double serializes data using Redis adapter
- Added `console:boot` event to allow the developer to perform initialization actions
- Added implementation options `allowEmpty` for `Phalcon\Mvc\Model\Validator\Ip`
- Fixed SQLite bug[#10997] related to setting of index type
- Fixed SQLite bug[#10997](https://github.com/phalcon/cphalcon/issues/10997) related to setting of index type
- Added `Phalcon\Db\Dialect\Sqlite::listIndexesSql` - to generate the SQL to get query list of indexes
- Fixed MySQL bug[#11036] related to setting of index type
- Fixed MySQL bug[#11036](https://github.com/phalcon/cphalcon/issues/11036) related to setting of index type
- Added missed `RouteInterface::setHostname`, `RouteInterface::getHostname`
- Added `strict` option for ExclusionIn validator
- Added `Phalcon\Text::underscore` - to make a phrase underscored instead of spaced
Expand Down
52 changes: 37 additions & 15 deletions phalcon/queue/beanstalk.zep
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,24 @@
namespace Phalcon\Queue;

use Phalcon\Queue\Beanstalk\Job;
use Phalcon\Queue\Beanstalk\Exception;

/**
* Phalcon\Queue\Beanstalk
*
* Class to access the beanstalk queue service.
* Partially implements the protocol version 1.2
*
* <code>
* use Phalcon\Queue\Beanstalk;
*
* $queue = new Beanstalk([
* 'host' => '127.0.0.1',
* 'port' => 11300,
* 'persistent' => true,
* ]);
* </code>
*
* @link http://www.igvita.com/2010/05/20/scalable-work-queues-with-beanstalk/
*/
class Beanstalk
Expand Down Expand Up @@ -66,7 +77,7 @@ class Beanstalk
*/
public function connect() -> resource
{
var connection, parameters;
var connection, parameters, persistent, $function;

let connection = this->_connection;
if typeof connection == "resource" {
Expand All @@ -75,9 +86,23 @@ class Beanstalk

let parameters = this->_parameters;

let connection = fsockopen(parameters["host"], parameters["port"], null, null);
/**
* Check if the connection must be persistent
*/
if fetch persistent, parameters["persistent"] {
if persistent {
let $function = "pfsockopen";
} else {
let $function = "fsockopen";
}
} else {
let $function = "fsockopen";
}

let connection = {$function}(parameters["host"], parameters["port"], null, null);

if typeof connection != "resource" {
throw new \Phalcon\Exception("Can't connect to Beanstalk server");
throw new Exception("Can't connect to Beanstalk server");
}

stream_set_timeout(connection, -1, null);
Expand All @@ -95,7 +120,7 @@ class Beanstalk
*/
public function put(var data, var options = null) -> string|boolean
{
var priority, delay, ttr, serialized, response, status, length;
var priority, delay, ttr, serialized, response, status, length;

/**
* Priority is 100 by default
Expand Down Expand Up @@ -139,7 +164,7 @@ class Beanstalk
*/
public function reserve(var timeout = null) -> boolean|<Job>
{
var command, response;
var command, response;

if typeof timeout != "null" {
let command = "reserve-with-timeout " . timeout;
Expand Down Expand Up @@ -168,7 +193,7 @@ class Beanstalk
*/
public function choose(string! tube) -> boolean|string
{
var response;
var response;

this->write("use " . tube);

Expand All @@ -185,7 +210,7 @@ class Beanstalk
*/
public function watch(string! tube) -> boolean|string
{
var response;
var response;

this->write("watch " . tube);

Expand Down Expand Up @@ -253,7 +278,7 @@ class Beanstalk
*/
public function peekReady() -> boolean|<Job>
{
var response;
var response;

this->write("peek-ready");

Expand All @@ -270,7 +295,7 @@ class Beanstalk
*/
public function peekBuried() -> boolean|<Job>
{
var response;
var response;

this->write("peek-buried");

Expand Down Expand Up @@ -328,13 +353,10 @@ class Beanstalk
/**
* Reads a packet from the socket. Prior to reading from the socket will
* check for availability of the connection.
*
* @param int length Number of bytes to read.
* @return string|boolean Data or `false` on error.
*/
public function read(int length = 0) -> boolean|string
{
var connection, data;
var connection, data;

let connection = this->_connection;
if typeof connection != "resource" {
Expand All @@ -352,7 +374,7 @@ class Beanstalk

let data = stream_get_line(connection, length + 2);
if stream_get_meta_data(connection)["timed_out"] {
throw new \Phalcon\Exception("Connection timed out");
throw new Exception("Connection timed out");
}

return rtrim(data, "\r\n");
Expand All @@ -366,7 +388,7 @@ class Beanstalk
*/
protected function write(string data) -> boolean|int
{
var connection, packet;
var connection, packet;

let connection = this->_connection;
if typeof connection != "resource" {
Expand Down
29 changes: 29 additions & 0 deletions phalcon/queue/beanstalk/exception.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| 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. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
+------------------------------------------------------------------------+
*/

namespace Phalcon\Queue\Beanstalk;

/**
* Phalcon\Queue\Beanstalk\Exception
*
* Exceptions thrown in Phalcon\Queue\Beanstalk will use this class
*/
class Exception extends \Phalcon\Exception
{
}
2 changes: 1 addition & 1 deletion unit-tests/BeanstalkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function testStats()

$tubeStats = $queue->statsTube('beanstalk-test');
$this->assertTrue(is_array($tubeStats));
$this->assertTrue($jobStats['name'] === 'beanstalk-test');
$this->assertTrue($tubeStats['name'] === 'beanstalk-test');

$this->assertTrue($queue->statsTube('beanstalk-test-does-not-exist') === false);

Expand Down

0 comments on commit 796d755

Please sign in to comment.