Exemplo n.º 1
0
 function sendMessage($queue, $data, $delay = 0)
 {
     $data['randomize'] = md5(rand(0, 1000000000) . rand(0, 1000000000) . rand(0, 1000000000));
     $score = date("U") + $delay;
     Redis::zadd('cinnamon-queue-' . $queue, $score, json_encode($data));
     if (Config::get('queue.auto_run', false) && defined('__APP__')) {
         $base_path = __APP__;
         if (!file_exists($base_path . "/commands/subscribe.php")) {
             $base_path = __APP__ . "/vendor/cinnamonlab/queue";
         }
         $ip = $_SERVER['SERVER_ADDR'];
         $process = Redis::get('cinnamon-process-' . $ip);
         if ($process != null) {
             if ($process > date('U') - 600) {
                 Redis::publish('cinnamon-process', $queue);
                 return $this;
             }
             ob_start();
             system("ps ax|grep commands/subscribe.php| grep -v grep");
             $process = trim(ob_get_clean());
             if (strlen($process) > 0) {
                 Redis::set('cinnamon-process-' . $ip, date('U'));
                 Redis::expire('cinnamon-process-' . $ip, 1200);
                 Redis::publish('cinnamon-process', $queue);
                 return $this;
             } else {
                 Redis::del('cinnamon-process-' . $ip);
             }
             $process = null;
         }
         $cmd = "nohup " . Config::get('queue.php_path', '/usr/bin/php') . " " . $base_path . "/commands/subscribe.php {$ip} > /dev/null &";
         exec($cmd);
         Redis::publish('cinnamon-process', $queue);
     }
     return $this;
 }
Exemplo n.º 2
0
use Framework\Redis\Redis;
use Framework\Config;
use Framework\Queue\Driver\RedisDriver;
use Framework\Exception\FrameworkException;
use Framework\Queue\QueueProcessor;
use Framework\Input;
use Framework\Route;
use Framework\Queue\Driver\Driver;
$r = Redis::getInstance();
if (!isset($argv[1])) {
    return;
}
$ip = $argv[1];
$process = $r->get('cinnamon-process-' . $ip);
if ($process == null) {
    Redis::set('cinnamon-process-' . $ip, date('U'));
    Redis::expire('cinnamon-process-' . $ip, 1200);
    try {
        if (!$rs instanceof Driver) {
            throw FrameworkException::internalError('Queue Driver Not Set');
        }
        QueueProcessor::getInstance()->setDriver($rs)->setAsReceiver();
        $message = $rs->receiveMessage('route');
        if (!$message) {
            continue;
        }
        Input::bind($message);
        Route::reset();
        Route::setSkipMain();
        include __APP__ . "/route.php";
        $r->subscribe('cinnamon-process', function ($message, $channel) use($r) {