protected function processQuery(\GearmanJob $job, Query $query) { $start = microtime(true); $job->sendStatus(0, 100); $tempfile = tempnam(sys_get_temp_dir(), 'transmute_image'); $tempdest = tempnam(sys_get_temp_dir(), 'transmute_image'); if (false === ($filecontent = @file_get_contents($query->getFile()))) { $this->logger->addInfo(sprintf('Unable to download file `%s`', $query->getFile())); return new Result($job->handle(), $query->getUuid(), $job->workload(), null, $this->workerName, $start, microtime(true), array(), array(sprintf('Unable to download file `%s`', $query->getFile()))); } $this->logger->addInfo(sprintf('file %s retrieved', $query->getFile())); $job->sendStatus(30, 100); file_put_contents($tempfile, $filecontent); unset($filecontent); $job->sendStatus(50, 100); $specification = new Image(); $width = $height = null; foreach ($query->getParameters() as $name => $value) { switch ($name) { case 'width': $width = $value; break; case 'height': $height = $value; break; case 'quality': $specification->setQuality($value); break; } } if (null !== $width && null !== $height) { $specification->setDimensions($width, $height); } try { $this->alchemyst->open($tempfile)->turnInto($tempdest, $specification)->close(); } catch (Exception $e) { $this->logger->addInfo(sprintf('A media-alchemyst exception occured %s', $e->getMessage())); return new Result($job->handle(), $query->getUuid(), $job->workload(), null, $this->workerName, $start, microtime(true), array(), array(sprintf('A media-alchemyst exception occured %s', $e->getMessage()))); } catch (\Exception $e) { $this->logger->addInfo(sprintf('An unexpected exception occured %s', $e->getMessage())); return new Result($job->handle(), $query->getUuid(), $job->workload(), null, $this->workerName, $start, microtime(true), array(), array(sprintf('An unexpected exception occured %s', $e->getMessage()))); } $result = new Result($job->handle(), $query->getUuid(), $job->workload(), file_get_contents($tempdest), $this->workerName, $start, microtime(true)); unlink($tempfile); unlink($tempdest); $this->logger->addInfo('Conversion successfull'); $job->sendStatus(100, 100); return $result; }
/** * Executes a [Request] object and returns the response * * // Create a request * $request = Request::factory('/users/load/1'); * * // Execute the request * $response = Request_Async_Gearman::execute_request($request); * * Used as a callback by Request_Async_Gearman to handle the request * processing. * * @param Kohana_Request request to process * @return Kohana_Response the response from the request */ public static function execute_request(GearmanJob $job) { // Unserialise the request $request = unserialize($job->workload()); // Send starting status $job->sendStatus(1, 2); // Encapsulate request execution try { // Get the response $response = $request->execute()->headers('X-Request-Uri', $request->uri()); } catch (Exception $e) { // Send the exception to Gearman $job->sendException($e->getMessage()); return; } // Send complete status $job->sendStatus(2, 2); // Send the response $job->sendData(serialize($response)); }
/** * Update Status * * @param int $numerator * @param int $denominator * @return bool */ public function updateStatus($numerator, $denominator) { return $this->job->sendStatus($numerator, $denominator); }