Skip to content

Commit

Permalink
remove logging
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlmoon committed Feb 3, 2017
1 parent cc4fba6 commit e3340ed
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 175 deletions.
44 changes: 16 additions & 28 deletions Net/Gearman/Client.php
Expand Up @@ -97,7 +97,7 @@ protected function getConnection($uniq=null, $servers=null)
$start = microtime(true);
$elapsed = 0;

if (is_null($servers)){
if (is_null($servers)) {
$servers = $this->servers;
}

Expand All @@ -108,11 +108,10 @@ protected function getConnection($uniq=null, $servers=null)
*/
$tried_servers = array();

while ($conn === null && count($servers)){

if (count($servers) === 1){
while ($conn === null && count($servers)) {
if (count($servers) === 1) {
$key = key($servers);
} elseif ($uniq === null){
} elseif ($uniq === null) {
$key = array_rand($servers);
} else {
$key = ord(substr(md5($uniq), -1)) % count($servers);
Expand All @@ -122,25 +121,14 @@ protected function getConnection($uniq=null, $servers=null)

$tried_servers[] = $server;

if (empty($this->conn[$server])){

if (empty($this->conn[$server])) {
$conn = null;

$start = microtime(true);
try{
try {
$conn = new Net_Gearman_Connection($server, $this->timeout);
} catch(Net_Gearman_Exception $e) {
} catch (Net_Gearman_Exception $e) {
$conn = null;
Logger::log("gearman_worker_error_log", array(
"level" => "warning",
"event" => "client_connect",
"server" => $server,
"timeout" => $this->timeout,
"elapsed" => microtime(true) - $start,
"error_message" => $e->getMessage(),
"error_file" => $e->getFile(),
"error_line" => $e->getLine()
), true);
}

if (!$conn || !$conn->isConnected()) {
Expand All @@ -161,9 +149,9 @@ protected function getConnection($uniq=null, $servers=null)

}

if (empty($conn)){
if (empty($conn)) {
$message = "Failed to connect to a Gearman server. Attempted to connect to ".implode(",", $tried_servers).".";
if (count($tried_servers) != count($try_servers)){
if (count($tried_servers) != count($try_servers)) {
$message.= " Not all servers were tried. Full server list is ".implode(",", $try_servers).".";
}
throw new Net_Gearman_Exception($message);
Expand Down Expand Up @@ -230,7 +218,7 @@ protected function submitTask(Net_Gearman_Task $task)

// if we don't have a scalar
// json encode the data
if (!is_scalar($task->arg)){
if (!is_scalar($task->arg)) {
$arg = @json_encode($task->arg);
} else {
$arg = $task->arg;
Expand All @@ -242,7 +230,7 @@ protected function submitTask(Net_Gearman_Task $task)
'arg' => $arg
);

if (!empty($task->servers)){
if (!empty($task->servers)) {
$servers = $task->servers;
} else {
$servers = null;
Expand All @@ -269,7 +257,7 @@ public function runSet(Net_Gearman_Set $set, $timeout = null)
$taskKeys = array_keys($set->tasks);
$t = 0;

if ($timeout !== null){
if ($timeout !== null) {
$socket_timeout = min(10, (int)$timeout);
} else {
$socket_timeout = 10;
Expand Down Expand Up @@ -312,14 +300,14 @@ public function runSet(Net_Gearman_Set $set, $timeout = null)
$write = null;
$except = null;
$read_cons = array();
foreach ($this->conn as $conn){
foreach ($this->conn as $conn) {
$read_conns[] = $conn->socket;
}
@socket_select($read_conns, $write, $except, $socket_timeout);
foreach ($this->conn as $conn) {
$err = socket_last_error($conn->socket);
// Error 11 is EAGAIN and is normal in non-blocking mode
if ($err && $err != 11){
if ($err && $err != 11) {
$msg = socket_strerror($err);
socket_getpeername($conn->socket, $remote_address, $remote_port);
socket_getsockname($conn->socket, $local_address, $local_port);
Expand Down Expand Up @@ -395,7 +383,7 @@ public function disconnect()
}

foreach ($this->conn as $conn) {
if (is_callable(array($conn, "close"))){
if (is_callable(array($conn, "close"))) {
$conn->close();
}
}
Expand Down Expand Up @@ -426,7 +414,7 @@ public static function getInstance($servers, $timeout = 1000) {

$key = md5(json_encode($servers));

if (!isset($instances[$key])){
if (!isset($instances[$key])) {
$instances[$key] = new Net_Gearman_Client($servers, $timeout);
}

Expand Down
32 changes: 11 additions & 21 deletions Net/Gearman/Connection.php
Expand Up @@ -108,7 +108,7 @@ class Net_Gearman_Connection
public $socket;

public function __construct($host=null, $timeout=250) {
if ($host){
if ($host) {
$this->connect($host, $timeout);
}
}
Expand Down Expand Up @@ -175,7 +175,7 @@ public function connect($host, $timeout = 250)
* trying.
*/
$socket_connected = false;
do{
do {
socket_clear_error($this->socket);
$socket_connected = @socket_connect($this->socket, $host, $port);
$err = @socket_last_error($this->socket);
Expand All @@ -191,16 +191,15 @@ public function connect($host, $timeout = 250)
$socket_connected = $socket_connected && $err === 0;


if ($socket_connected){

if ($socket_connected) {
socket_set_nonblock($this->socket);

socket_set_option($this->socket, SOL_SOCKET, SO_KEEPALIVE, 1);

/**
* set the real send/receive timeouts here now that we are connected
*/
if ($timeout >= 1000){
if ($timeout >= 1000) {
$ts_seconds = $timeout / 1000;
$tv_sec = floor($ts_seconds);
$tv_usec = ($ts_seconds - $tv_sec) * 1000000;
Expand All @@ -213,13 +212,8 @@ public function connect($host, $timeout = 250)

// socket_set_option($this->socket, SOL_TCP, SO_DEBUG, 1); // Debug

StatsD::timing("application.gearman.connect_time_per_server.".GetConfig::get("dealnews.location")."_to_".str_replace(".", "_", $host), $elapsed);

} else {

StatsD::timing("application.gearman.connect_time_per_server_failure.".GetConfig::get("dealnews.location")."_to_".str_replace(".", "_", $host), $elapsed);
StatsD::increment("application.gearman.connect_failed.".GetConfig::get("dealnews.location")."_to_".str_replace(".", "_", $host), 1);

$errno = @socket_last_error($this->socket);
$errstr = @socket_strerror($errno);

Expand All @@ -238,11 +232,11 @@ public function connect($host, $timeout = 250)

}

public function addWaitingTask($task){
public function addWaitingTask($task) {
$this->waiting[] = $task;
}

public function getWaitingTask(){
public function getWaitingTask() {
return array_shift($this->waiting);
}

Expand Down Expand Up @@ -276,8 +270,6 @@ public function send($command, array $params = array())

$d = implode("\x00", $data);

// use for debug: Logger::log("gearman_worker_error_log", getmypid().":send:$command");

$cmd = "\0REQ" . pack("NN",
$this->commands[$command][0],
$this->stringLength($d)) . $d;
Expand Down Expand Up @@ -373,8 +365,6 @@ public function read()
throw new Net_Gearman_Exception("({$return['err_code']}): {$return['err_text']}");
}

// use for debug: Logger::log("gearman_worker_error_log", getmypid().":read:".$this->magic[$resp['type']][0]);

return array('function' => $this->magic[$resp['type']][0],
'type' => $resp['type'],
'data' => $return);
Expand All @@ -392,7 +382,7 @@ public function blockingRead($timeout = 1000)
{
$cmds = array();

if ($timeout >= 1000){
if ($timeout >= 1000) {
$ts_seconds = $timeout / 1000;
$tv_sec = floor($ts_seconds);
$tv_usec = ($ts_seconds - $tv_sec) * 1000000;
Expand All @@ -408,19 +398,19 @@ public function blockingRead($timeout = 1000)
$start = microtime(true);
$success = @socket_select($read, $write, $except, $tv_sec, $tv_usec);
$et = (microtime(true) - $start) * 1000;
if ($success === false){
if ($success === false) {
$errno = @socket_last_error($this->socket);
if ($errno != 0){
if ($errno != 0) {
throw new Net_Gearman_Exception("Socket error: ($errno) ".socket_strerror($errno));
}
}

if ($success === 0){
if ($success === 0) {
$errno = @socket_last_error($this->socket);
throw new Net_Gearman_Exception("Socket timeout ($et): ($errno) ".socket_strerror($errno));
}

if (count($read)){
if (count($read)) {
foreach ($read as $s) {
$cmds[] = $this->read();
}
Expand Down

0 comments on commit e3340ed

Please sign in to comment.