Skip to content
This repository has been archived by the owner on Oct 27, 2019. It is now read-only.

Commit

Permalink
#1 Double port error on connect fail with custom port
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Nov 18, 2012
1 parent 731aab8 commit 4ed210d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
3 changes: 2 additions & 1 deletion application/controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}

}
}
33 changes: 18 additions & 15 deletions application/controllers/JobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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 {
Expand All @@ -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 = "";
Expand All @@ -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 = "";
Expand All @@ -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 {
Expand Down Expand Up @@ -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 = "";
Expand All @@ -195,4 +187,15 @@ public function kickAction() {
$this->jsonHelper->getResponse()->sendResponse();
}

}
/**
* @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);
}

}

0 comments on commit 4ed210d

Please sign in to comment.