Commits

Mirko Chialastri  committed 9348953

Refactory Worker

  • Participants
  • Parent commits 0518d91

Comments (0)

Files changed (2)

File Libs/Job.php

 		$result 	= json_decode($this->response['body'], true, 500);
 
 		if ($status == true && !isset($result['error'])) {
-			Log::success(sprintf('API response ... <img src="http://graph.facebook.com/%d/picture" /> (http://graph.facebook.com/%d/picture)', $result['id'], $result['id']), array('prefix' => 'APIGraph.request.parse'));
+			Log::success(sprintf('Trovato avatar utente Facebook <img src="http://graph.facebook.com/%d/picture" /> (http://graph.facebook.com/%d/picture)', $result['id'], $result['id']), array('prefix' => 'APIGraph.request.parse'));
 			return true;
 		} else {
 			Log::error(sprintf('Impossibile trovare avatar per questo facebook user id', array('prefix' => 'APIGraph.request.parse')));

File Libs/Worker.php

 require_once 'Libs/Job.php';
 
 
-function convert($size) {
-    $unit=array('b','kb','mb','gb','tb','pb');
-    return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
- }
-
-
-
-
-
 Class Worker {
 	public $queue = null;
 
 				Log::code(print_r($job_encoded, true), array('timestamp' => false));
 			}
 
+
 			$JObject = new $job_encoded->class;
-			try{
-				$JObject->send();
-				$JObject->parse(); 		// Callback per parsare il risultato, ed eventualmente fare operazioni su database
-
-				$this->queue->delete($job);
-				$retry = false;
-
-				Log::success(sprintf('API REQUEST - %s', $this->response->headers['code']), array('prefix' => $job_encoded->class));
-				Log::out('Removed from queue', array('prefix'  => 'Job::delete'));
-			} catch(APIRequestTimeoutException $e) {
-				Log::error('CURL timeout error .... pauseTube (3 minutes) !', array('prefix' => $job_encoded->class));
-				$this->queue->release($job);
-				$this->queue->pauseTube($tube, 30);
-			} catch(APIRequestGenericException $e) {
-				Log::error(sprintf('CURL generic error "%s" (%d)', $e->getMessage(), $e->getCode()), array('prefix' => $job_encoded->class));
-				// TODO: RELEASE? DELETE?
-			}
+			for($retries = JOBS_RETRIES; $retries > 0; $retries--) {
+
+				// CHIAMATA API
+				try{
+					$JObject->send();
+					// Callback per parsare il risultato, ed eventualmente fare operazioni su database
+					if ($JObject->parse()) {
+						$this->queue->delete($job);
+						$retry = false;
+
+						$response = $JObject->getResponse();
+						Log::success(sprintf('API REQUEST - %s', $response['headers']['code']), array('prefix' => $job_encoded->class));
+						Log::out('Removed from queue', array('prefix'  => 'Job::delete'));
+						break;
+					} else {	// Chiamata API OK, ma il risultato non è quello sperato 
+						Log::info('Risultato non valido... tento di nuovo');
+						$this->queue->release($job);
+					}
+				} catch(APIRequestTimeoutException $e) {
+					Log::error('CURL timeout error .... pauseTube (3 minutes) !', array('prefix' => $job_encoded->class));
+					$this->queue->release($job);
+					$this->queue->pauseTube($tube, 30);
+				} catch(APIRequestGenericException $e) {
+					Log::error(sprintf('CURL generic error "%s" (%d)', $e->getMessage(), $e->getCode()), array('prefix' => $job_encoded->class));
+					// TODO: RELEASE? DELETE?
+				}
 
+				// CONTROLLO TENTATIVI
+				if ($retries <= 0) {
+					Log::error('Job FAIL ... Max re-tries, deleting job!!!');
+					$this->queue->delete($job);
+					break;
+				}
 
-			/*if ($retries > 0) {
-				Log::error(sprintf('Job FAIL ... %d/%d re-tries lefts', $retries, JOBS_RETRIES), true);
-				$this->queue->release($job);
-				$retries--;
-			} else {
-				Log::error('Job FAIL ... Max re-tries, deleting job!!!', true);
-				$this->queue->delete($job);
-				$retries = JOBS_RETRIES;
-			}*/
-		
-			
-
-
-			// PHP (god)
-			$memory = memory_get_usage(true);
-			Log::info(sprintf('Memoria utilizzata: %s di %s', convert($memory), ini_get('memory_limit')), array('prefix' => 'Memory::usage'));
-			if($memory > 1000000) {
-				Log::error('Worker::memory exiting run due to memory limit', array('prefix' => 'Memory::usage'));
-				exit;
+				Log::error(sprintf('Job FAIL ... %d/%d re-tries lefts', $retries, JOBS_RETRIES));
 			}
-			
+
+
+
+			$this->checkProcessMemoryLimit();
+		}
+	}
+
+	public function checkProcessMemoryLimit() {
+		// PHP (god)
+		$memory = memory_get_usage(true);
+		Log::info(sprintf('Memoria utilizzata: %s di %s', $this->_convert($memory), ini_get('memory_limit')), array('prefix' => 'Memory::usage'));
+		if($memory > 1000000) {
+			Log::error('Worker::memory exiting run due to memory limit', array('prefix' => 'Memory::usage'));
+			exit(1);
 		}
 	}
 
 
 		Log::out(sprintf('Tube: %s > %d jobs [ready=%d, buried=%d]', $stats['name'], $stats['total-jobs'], $stats['current-jobs-ready'], $stats['current-jobs-buried']), array('prefix' => 'beanstalkd.tube.stats'));
 	}
+
+
+
+	protected function _convert($size) {
+    	$unit=array('b','kb','mb','gb','tb','pb');
+    	return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
+   	}
+ 
+
 }