From 4ed210d2792f23f1d8dac9c70478b8a3c1e26971 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Sun, 18 Nov 2012 20:03:52 +0100 Subject: [PATCH] #1 Double port error on connect fail with custom port --- application/controllers/IndexController.php | 3 +- application/controllers/JobController.php | 33 +++++++++++---------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/application/controllers/IndexController.php b/application/controllers/IndexController.php index 00ca8c5..e487019 100644 --- a/application/controllers/IndexController.php +++ b/application/controllers/IndexController.php @@ -4,10 +4,11 @@ class IndexController extends Zend_Controller_Action { public function indexAction() { + // Default server URL $this->view->server = $this->_getParam("server", "localhost:11300"); } public function aboutAction() { } -} \ No newline at end of file +} diff --git a/application/controllers/JobController.php b/application/controllers/JobController.php index 5d72a3b..2839d7b 100644 --- a/application/controllers/JobController.php +++ b/application/controllers/JobController.php @@ -20,14 +20,10 @@ public function init() { * Returns the list of the jobs in queue for the server */ public function getListAction() { - $server = $this->_getParam("server"); - $exp=explode(":", $server); - $server = (isset($exp[0]))?$exp[0]:"localhost"; - $port = (isset($exp[1]))?$exp[1]:"11300"; $data = array (); try { // Connect to the server and get job list - $messageQueue = new Pheanstalk_Pheanstalk($server,$port); + $messageQueue = $this->getServer(); $tubes = $messageQueue->listTubes(); foreach ($tubes as $tube) { $tubeArray = array(); @@ -59,7 +55,7 @@ public function getListAction() { } } catch (Pheanstalk_Exception_ConnectionException $e) { $this->getResponse()->setHttpResponseCode(400); - $data = "Unable to connect to '$server'"; + $data = "Unable to connect to the Beanstalkd server"; } catch (Exception $e) { $this->getResponse()->setHttpResponseCode(500); $data = $e->getMessage(); @@ -73,7 +69,6 @@ public function getListAction() { * Add a job in the server queue */ public function addAction() { - $server = $this->_getParam("server"); $data = $this->_getParam("data"); $tube = $this->_getParam("tube"); try { @@ -84,7 +79,7 @@ public function addAction() { throw new Exception("The tube field must not be empty"); } // Connect to the server - $messageQueue = new Pheanstalk_Pheanstalk($server); + $messageQueue = $this->getServer(); $messageQueue->useTube($tube); $messageQueue->put($data); $response = ""; @@ -101,11 +96,10 @@ public function addAction() { * Delete a job */ public function deleteAction() { - $server = $this->_getParam("server"); $jobId = $this->_getParam("id"); try { // Connect to the server - $messageQueue = new Pheanstalk_Pheanstalk($server); + $messageQueue = $this->getServer(); $job = $messageQueue->peek($jobId); $messageQueue->delete($job); $response = ""; @@ -122,12 +116,11 @@ public function deleteAction() { * Bury a job */ public function buryAction() { - $server = $this->_getParam("server"); $tube = $this->_getParam("tube"); $jobId = $this->_getParam("id"); try { // Connect to the server - $messageQueue = new Pheanstalk_Pheanstalk($server); + $messageQueue = $this->getServer(); // Check if the next job in the queue is still the same job $stillExists = false; try { @@ -177,12 +170,11 @@ public function buryAction() { * - count : number of jobs to kick */ public function kickAction() { - $server = $this->_getParam("server"); $tube = $this->_getParam("tube"); $count = $this->_getParam("count", 1); try { // Connect to the server - $messageQueue = new Pheanstalk_Pheanstalk($server); + $messageQueue = $this->getServer(); $messageQueue->useTube($tube); $messageQueue->kick($count); $response = ""; @@ -195,4 +187,15 @@ public function kickAction() { $this->jsonHelper->getResponse()->sendResponse(); } -} \ No newline at end of file + /** + * @return Pheanstalk_Pheanstalk + */ + private function getServer() { + $server = $this->_getParam("server"); + $exp = explode(":", $server); + $server = isset($exp[0]) ? $exp[0] : "localhost"; + $port = isset($exp[1])?$exp[1]:"11300"; + return new Pheanstalk_Pheanstalk($server, $port); + } + +}