Beispiel #1
1
 function enqueue($fid, $prioridad = 10)
 {
     // Carga biblioteca
     $this->_inicializa_pheanstalk();
     // Guardar en BBDD
     $result = $this->store($fid, self::PENDING, '');
     if ($result === FALSE) {
         return FALSE;
     }
     // Encolar en beanstalkd
     $host = $this->config->item('beanstalkd_host');
     $port = $this->config->item('beanstalkd_port');
     $tube = $this->config->item('beanstalkd_tube');
     $exito = FALSE;
     try {
         $pheanstalk = new Pheanstalk($host, $port);
         $pheanstalk->useTube($tube)->put("SCAN " . $fid, $prioridad);
         $exito = TRUE;
     } catch (Exception $e) {
         // TODO : más detalle
         log_message('error', 'Error encolando trabajo en beanstalkd');
         $this->store($fid, self::ERROR, 'El fichero no se pudo encolar para su revisión');
         /* En este caso, no es mala idea que el fichero quede
          * en la base de datos marcado con ERROR, pero que la ejecución
          * siga
          */
     }
     if ($exito === TRUE) {
         log_message('info', 'Fichero ' . $fid . ' encolado para ser escaneado');
     }
 }
 public function indexAction()
 {
     // TBD
     // List all objects?
     $pheanstalkClassRoot = APPLICATION_PATH . '/../contrib/Pheanstalk';
     require_once $pheanstalkClassRoot . '/Pheanstalk/ClassLoader.php';
     Pheanstalk_ClassLoader::register($pheanstalkClassRoot);
     $pheanstalk = new Pheanstalk('nurgle.nl');
     $id = $pheanstalk->usetube('testtube')->put('stop');
     print $id;
     exit;
     /*
     while($job = $pheanstalk->watch('testtube')->ignore('default')->reserve()) {
     	echo " -- " . $job->getData() . "\n";
     	$pheanstalk->delete($job);
     }
     */
     exit;
     /*
     $this->getResponse()->setHttpResponseCode(400);
     
     $data = array(
     	'code'		=> self::ERRNO_NO_ID,
     	'message'	=> self::ERRMSG_NO_ID,
     );
     
     $this->_helper->json($data);
     */
 }
Beispiel #3
0
 /**
  * Provides backwards compatibility - older versions used this
  * class as the "facade" rather than the Pheanstalk class.
  *
  * @param string $name
  * @param array $arguments
  */
 public function __call($name, $arguments)
 {
     $methods = array('put', 'reserve', 'release', 'delete', 'bury', 'kick', 'getCurrentTube', 'useTube', 'getWatchedTubes', 'watchTube', 'ignoreTube');
     if (!in_array($name, $methods)) {
         throw new BadMethodCallException(__METHOD__ . ' not implemented');
     }
     trigger_error(sprintf('%s::%s() deprecated, use Pheanstalk::%s()', __CLASS__, $name, $name), E_USER_NOTICE);
     $pheanstalk = new Pheanstalk($this->_hostname, $this->_port);
     $pheanstalk->setConnection($this);
     return call_user_func_array(array($pheanstalk, $name), $arguments);
 }
 /**
  * When the timeout passed to ::processOne() is 0 and there are no jobs it should return quitely.
  * @throws Exception\WorkerException
  */
 public function testWorkerNoJobs()
 {
     $testWorkerRuns = $this;
     $tube = 'worker_tube_' . rand(53, 504);
     $data = 'worker_value_' . rand(95, 3000);
     $pheanstalk = new Pheanstalk(self::SERVER_HOST, self::SERVER_PORT);
     $worker = new Worker(self::SERVER_HOST, self::SERVER_PORT);
     $worker->register($tube, function (Job $job) use($testWorkerRuns, $data) {
         $testWorkerRuns->assertEquals($data, $job->getData());
     });
     $processedJob = $worker->processOne(0);
     $stats = $pheanstalk->statsTube($tube);
     $this->assertEquals($stats['total-jobs'], 0);
 }
Beispiel #5
0
 public function testConnectionResetIfSocketExceptionIsThrown()
 {
     $pheanstalk = new Pheanstalk(self::SERVER_HOST, self::SERVER_PORT, self::CONNECT_TIMEOUT);
     Mock::generate('Pheanstalk_Connection');
     $connection = new MockPheanstalk_Connection('');
     $connection->returns('getHost', self::SERVER_HOST);
     $connection->returns('getPort', self::SERVER_PORT);
     $connection->returns('getConnectTimeout', self::CONNECT_TIMEOUT);
     $connection->throwOn('dispatchCommand', new Pheanstalk_Exception_SocketException('socket error simulated'));
     $pheanstalk->putInTube('testconnectionreset', __METHOD__);
     $pheanstalk->watchOnly('testconnectionreset');
     $pheanstalk->setConnection($connection);
     $connection->expectOnce('dispatchCommand');
     $job = $pheanstalk->reserve();
     $this->assertEqual(__METHOD__, $job->getData());
 }
 public function __construct($host, $port = self::DEFAULT_PORT, $connectTimeout = null)
 {
     if (strpos($host, ':') !== false) {
         list($host, $port) = explode(':', $host, 2);
     }
     parent::__construct($host, $port = self::DEFAULT_PORT, $connectTimeout = null);
     $this->hostinfo = $host . ':' . $port;
 }
 private function _createPheanstalk()
 {
     $pheanstalk = new Pheanstalk(self::SERVER_HOST);
     $tube = preg_replace('#[^a-z]#', '', strtolower(__CLASS__));
     $pheanstalk->useTube($tube)->watch($tube)->ignore('default');
     try {
         while ($pheanstalk->delete($pheanstalk->peekDelayed())) {
         }
     } catch (Pheanstalk_Exception_ServerException $e) {
     }
     try {
         while ($pheanstalk->delete($pheanstalk->peekReady())) {
         }
     } catch (Pheanstalk_Exception_ServerException $e) {
     }
     return $pheanstalk;
 }
Beispiel #8
0
        /**
         * dispatchWork
         * 
         * @param string $function
         * @param array $parameters
         * @param object $socket
         */
        protected function dispatchWork($f,$p) {
                $this->Log->debug(__METHOD__);
                
                // If this work is to be done locally set it up right here
                if('local'==$this->work['method']) {
                        
                        // dispatch the API call
                        include_once($_SERVER['SCRIPT_FILENAME']);
                        $this->Log->debug('include_once('.$_SERVER['SCRIPT_FILENAME'].')');

                        // The expected class name
                        $class_name = ANYAPI_CALL_CLASS;

                        if(!class_exists($class_name)) {
                                $this->Log->error("Code 531: Unable to locate the expected class named '".$class_name."' in ".$_SERVER['SCRIPT_FILENAME']);
                                $response['error'] = array('code'=>'531','message'=>'Internal system error');
                                $this->out($response); // terminates
                        }
                        
                        // Try and instantiate the class
                        try {
                                $anyapi = new $class_name;
                        } catch (Exception $e) {
                                $this->Log->error("Code 540: Unable instantiate class named '".$class_name."' got exception ".$e);
                                $response['error'] = array('code'=>'540','message'=>'Internal system error');
                                $this->out($response); // terminates
                        }

                        // There must be a better way to do this - uck!!
                        if(0==count($p))     { return $anyapi->$f(); }
                        elseif(1==count($p)) { return $anyapi->$f($p[0]); }
                        elseif(2==count($p)) { return $anyapi->$f($p[0],$p[1]); }
                        elseif(3==count($p)) { return $anyapi->$f($p[0],$p[1],$p[2]); }
                        elseif(4==count($p)) { return $anyapi->$f($p[0],$p[1],$p[2],$p[3]); }
                        elseif(5==count($p)) { return $anyapi->$f($p[0],$p[1],$p[2],$p[3],$p[4]); }
                        elseif(6==count($p)) { return $anyapi->$f($p[0],$p[1],$p[2],$p[3],$p[4],$p[5]); }
                        elseif(7==count($p)) { return $anyapi->$f($p[0],$p[1],$p[2],$p[3],$p[4],$p[5],$p[6]); }
                        elseif(8==count($p)) { return $anyapi->$f($p[0],$p[1],$p[2],$p[3],$p[4],$p[5],$p[6],$p[7]); }
                        elseif(9==count($p)) { return $anyapi->$f($p[0],$p[1],$p[2],$p[3],$p[4],$p[5],$p[6],$p[7],$p[8]); }
                        elseif(10==count($p)){ return $anyapi->$f($p[0],$p[1],$p[2],$p[3],$p[4],$p[5],$p[6],$p[7],$p[8],$p[9]); }
                        elseif(11==count($p)){ return $anyapi->$f($p[0],$p[1],$p[2],$p[3],$p[4],$p[5],$p[6],$p[7],$p[8],$p[9],$p[10]); }
                        elseif(12==count($p)){ return $anyapi->$f($p[0],$p[1],$p[2],$p[3],$p[4],$p[5],$p[6],$p[7],$p[8],$p[9],$p[10],$p[11]); }
                        elseif(13==count($p)){ return $anyapi->$f($p[0],$p[1],$p[2],$p[3],$p[4],$p[5],$p[6],$p[7],$p[8],$p[9],$p[10],$p[11],$p[12]); }
                        elseif(14==count($p)){ return $anyapi->$f($p[0],$p[1],$p[2],$p[3],$p[4],$p[5],$p[6],$p[7],$p[8],$p[9],$p[10],$p[11],$p[12],$p[13]); }
                        elseif(15==count($p)){ return $anyapi->$f($p[0],$p[1],$p[2],$p[3],$p[4],$p[5],$p[6],$p[7],$p[8],$p[9],$p[10],$p[11],$p[12],$p[13],$p[14]); }

                        $this->Log->error("Code 563: AnyApi only supports 15x input parameters for an action - got an elegant fix?");
                        $response['error'] = array('code'=>'563','message'=>'Internal system error');
                        
                        return $response;
                }
                
                // Check we are setup for beanstalkd since it's the only type supported at the moment
                if('beanstalkd' != $this->work['queue']['type']) {
                        $this->Log->error("Code 565: AnyApi only supports work[queue][type] = 'beanstalkd'");
                        $response['error'] = array('code'=>'565','message'=>'Internal system error');
                        return $response;
                }
                
                $socket = null;

                // Only create a return data socket if the worker will not be doing an external callback
                if(!isset($this->request['return_options']['callback']['url']) && !isset($this->request['return_options']['callback']['email'])) {

                        $this->Log->debug("Creating an internet tcp stream socket for work return data");
                        $socket = socket_create(AF_INET,SOCK_STREAM,getprotobyname('tcp')); 

                        // Make sure we created the socket
                        if(!$socket) {
                                $this->Log->error("Code 490: Unable to create work queue return data socket!");
                                $response['error'] = array('code'=>'490','message'=>'Internal system error');
                                $this->out($response); // terminates
                        }
                        
                        // Bind the socket to something we can use
                        $return_token = false;
                        while(!$return_token) {

                                $port_delta = $this->work['queue']['callback_socket']['max_port'] - $this->work['queue']['callback_socket']['min_port'];
                                $port = $this->work['queue']['callback_socket']['min_port'] + rand(0,$port_delta);

                                if(@socket_bind($socket,$this->work['queue']['callback_socket']['address'],$port)) {
                                        $this->Log->debug('Bound socket to '.$this->work['queue']['callback_socket']['address'].':'.$port.' ready for work queue return data');
                                        $return_token = md5(mt_rand(0,10000000).microtime(true));
                                }
                        }
                        
                        $this->request['return_options']['callback']['socket'] = array(
                            'address' =>$this->work['queue']['callback_socket']['address'],
                            'port' => $port,
                            'token' => $return_token,
                            'timeout' => $this->work['queue']['callback_socket']['timeout']
                        );
                }
                
                // Beanstalk!
                include_once(dirname(__FILE__).'/pheanstalk/pheanstalk_init.php');
                $Pheanstalk = new Pheanstalk($this->work['queue']['host'],$this->work['queue']['port'],$this->work['queue']['connect_timeout']);
                
                $job_id = null;
                try {
                        $job_id = $Pheanstalk
                                ->useTube(ANYAPI_CALL_CLASS) // use the class name for the tube name
                                ->put(gzcompress(serialize(array($f,$p,$this->request['return_options'])),9));
                        
                } catch (Exception $e) {
                        $this->Log->error("Code 589: Unable to place work onto beanstalkd queue at ".$this->work['queue']['host'].':'.$this->work['queue']['port']);
                        $response['error'] = array('code'=>'589','message'=>'Internal system error');
                        return $response;
                }
                $this->Log->debug("Work placed on beanstalkd queue with job_id:".$job_id);
                
                // Wait for callback response from the worker into the socket here
                socket_listen($socket);
                $this->Log->debug("Socket listening for response data for job_id:".$job_id);
                
                socket_set_nonblock($socket);
                $this->Log->debug("Socket set to non-blocking mode for job_id:".$job_id);
                
                // Wait until timeout for the callback socket response
                $timeout_timestamp = time() + $this->work['queue']['callback_socket']['timeout'];
                $usleep_interval = 100; // 1/10000th of a second
                
                $connection = false;
                while(!$connection) {
                        
                        $connection = @socket_accept($socket);
                        
                        usleep($usleep_interval);
                        $usleep_interval = $usleep_interval*1.1; // gradual scaling back of $usleep_interval
                        
                        if(time() > $timeout_timestamp) {
                                $this->Log->error('Code 456: Timed out after while for response data from job_id: '.$job_id);
                                $response['error'] = array('code'=>'456','message'=>'Internal system error');
                                return $response;
                        }
                }
                
                // Slurp up the data in a buffered manner
                $data = null;
                do {
                        @socket_recv($connection, $buffer,1024,MSG_DONTWAIT);
                        
                        // Protect against resource temporarily unavailable problems
                        if(11 == socket_last_error($connection)) {
                                socket_clear_error($connection);
                                $buffer = 'CONTINUE';
                        } else {
                                $data .= $buffer;
                        }
                        
                } while (!empty($buffer));
                
                // Uncompress the data returned through the socket
                $data = @gzuncompress($data);
                
                // Strip apart data and token componets
                $token = substr($data,(strlen($data)-strlen($this->request['return_options']['callback']['socket']['token'])),strlen($this->request['return_options']['callback']['socket']['token']));
                $data = substr($data,0,strlen($data)-strlen($this->request['return_options']['callback']['socket']['token'])-1 );
                
                // Make sure the token matches the expected value
                if($token != $this->request['return_options']['callback']['socket']['token']) {
                        $this->Log->error('Code 472: Invalid return data token for job_id: '.$job_id);
                        $response['error'] = array('code'=>'472','message'=>'Internal system error');
                        return $response;
                }
                
                // Detect if this is raw binary data or not as indicated by a BIN\0 at the front of the string
                if("BIN\0" == substr($data,0,4)) {
                        return substr($data,4);
                }
                return json_decode($data,true);
        }
Beispiel #9
0
define('BASEPATH', $ruta);
$config = array();
require $ruta . "/config/config.php";
if (!$config['activar_antivirus']) {
    echo "El antivirus no está activo\n";
    exit(1);
}
// Biblioteca pheanstalk
require_once $ruta . 'app/libraries/pheanstalk/pheanstalk_init.php';
// Analizador
require_once $ruta . 'app/libraries/Avengine.php';
require_once $ruta . 'app/libraries/avmodules/' . $config['avmodule'] . '.php';
$avconfig = $config['avconfig'];
$av = new $config['avmodule']($avconfig);
try {
    $pheanstalk = new Pheanstalk($config['beanstalkd_host'], $config['beanstalkd_port']);
    while (1) {
        $job = $pheanstalk->watch($config['beanstalkd_tube'])->ignore('default')->reserve();
        if ($job === FALSE) {
            debug("Reintentando en unos segundos...");
            sleep(5);
        } else {
            $exito = FALSE;
            debug($job->getData());
            $trozos = preg_split('/ /', $job->getData());
            if ($trozos[0] == 'SCAN') {
                $fid = $trozos[1];
                // Procesado con el antivirus
                $path = $config['directorio_ficheros'] . '/' . $fid;
                debug('Escaneando ' . $fid);
                $resav = $av->scan($path);
 /**
  * Put the error on a beanstalk queue so a separate process can
  * send it off to Airbrake via HTTP. Requires the pheanstalk client.
  * Config:
  * $BEANSTALK_SERVERS[] = array(
  *    'host' => 'example.com',
  *    'port' => '11300'
  * );
  * @author Aaron Parecki
  */
 public function beanstalkRequest($url, $headers, $body)
 {
     global $BEANSTALK_SERVERS;
     require_once 'pheanstalk/pheanstalk_init.php';
     $k = array_rand($BEANSTALK_SERVERS);
     $pheanstalk = new Pheanstalk($BEANSTALK_SERVERS[$k]['host'], $BEANSTALK_SERVERS[$k]['port']);
     $pheanstalk->useTube('airbrake');
     $pheanstalk->put(json_encode(array('url' => $url, 'headers' => $headers, 'body' => $body)));
 }
Beispiel #11
0
#!/usr/bin/php
<?php 
date_default_timezone_set('Europe/Amsterdam');
echo "----------------------------------\n";
echo "MiniThor flow queue system started\n";
echo date('Y-m-d H:i:s') . "\n";
echo "----------------------------------\n";
$pheanstalkClassRoot = '../contrib/Pheanstalk';
require_once $pheanstalkClassRoot . '/Pheanstalk/ClassLoader.php';
Pheanstalk_ClassLoader::register($pheanstalkClassRoot);
$pheanstalk = new Pheanstalk('nurgle.nl');
$continue = true;
$restart = false;
do {
    $job = $pheanstalk->watch('testtube')->ignore('default')->reserve();
    $jobData = $job->getData();
    echo '> ' . date('H:i:s') . ' - ' . $jobData . "\n";
    if ($jobData == 'restart') {
        $continue = false;
        $restart = true;
    } else {
        if ($jobData == 'stop') {
            $continue = false;
        }
    }
    $pheanstalk->delete($job);
} while ($continue);
if ($restart) {
    $exitCode = 98;
    // Planned restart
} else {
<?php

require_once 'php-airbrake-notifier/Services/Airbrake.php';
require_once 'pheanstalk/pheanstalk_init.php';
$airbrake = new Services_Airbrake(HOPTOAD_API_KEY);
$k = array_rand($BEANSTALK_SERVERS);
$pheanstalk = new Pheanstalk($BEANSTALK_SERVERS[$k]['host'], $BEANSTALK_SERVERS[$k]['port']);
$pheanstalk->watch('airbrake')->ignore('default');
while ($job = $pheanstalk->reserve()) {
    $data = json_decode($job->getData());
    echo date('Y-m-d H:i:s') . ' Processing Error: ' . $data->url . "\n";
    $airbrake->curlRequest($data->url, $data->headers, $data->body);
    $pheanstalk->delete($job);
}