Exemple #1
0
 public static function timeout($func, $ms = null, $async = true, $forceEnv = false)
 {
     $closure = new Cana_SuperClosure($func);
     $encoded = base64_encode(serialize($closure));
     if ($ms) {
         $sleep = ' -s=' . $ms;
     }
     if ($forceEnv) {
         $env = ' -e=' . $forceEnv;
     } else {
         $env = ' -e=' . c::getEnv(false);
     }
     if (file_exists('/usr/local/bin/php')) {
         $v = 'local';
     } else {
         $v = 'auto';
     }
     $cmd = c::config()->dirs->root . 'cli/timeout-' . $v . '.php' . $sleep . ' -c=' . str_replace("'", '"', escapeshellarg($encoded)) . $env;
     if (getenv('TRAVIS')) {
         $async = false;
     }
     if ($async) {
         try {
             $log = getenv('TRAVIS') ? '/dev/null' : '/var/log/timeout';
             exec('nohup ' . $cmd . ' >> ' . $log . ' 2>&1 &');
         } catch (Exception $e) {
             Crunchbutton_Log::error(['type' => 'timeout exception', 'cmd' => $cmd]);
         }
     } else {
         exec($cmd . ' 2>&1 &', $o);
         if ($o) {
             print_r($o);
         }
     }
 }