예제 #1
0
 /**
  * Handle work
  *
  * @param object $job Gearman job
  * @return mixed Return value from job
  */
 public function doWork(GearmanJob $job)
 {
     $this->process['working'] = true;
     try {
         $workload = $job->workload();
         if (empty($workload)) {
             throw new RuntimeException("No workload");
         }
         $params = @json_decode($workload, true);
         if (!$params || !is_array($params)) {
             throw new RuntimeException("Invalid workload: {$workload}");
         }
         $this->log('Handling job ' . (!empty($params['id']) ? "#{$params['id']} " : '') . $params['task'] . ' with arguments ' . json_encode($params['args']));
         $result = Gearman::execute($params['configName'], $params['task'], $params['args'], !empty($params['env']) ? $params['env'] : [], $params);
     } catch (Exception $e) {
         $this->log('ERROR: ' . $e->getMessage(), LOG_ERR);
     }
     $this->process['working'] = false;
     if (!empty($this->settings['memoryLimit'])) {
         $usedMemory = memory_get_usage(true);
         $this->log('Current Memory: ' . ceil($usedMemory / (1024 * 1024)) . ' MB' . '. Max memory set to ' . ceil($this->settings['memoryLimit'] / (1024 * 1024)) . ' MB');
         if ($usedMemory >= $this->settings['memoryLimit']) {
             $this->log('Maximum allowed memory reached. Quitting.');
             $this->process['run'] = false;
         }
     }
     return isset($result) ? $result : null;
 }