Example #1
0
 public function test_amp_1()
 {
     echo "before run()\n";
     \Amp\run(function () {
         \Amp\repeat(function () {
             echo "tick\n";
         }, $msInterval = 1000);
         \Amp\once("\\Amp\\stop", $msDelay = 5000);
     });
     echo "after run()\n";
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     set_time_limit(10800);
     // 3 hours is the maximum for this command. Need more? You really screwed something, full suite for all Oblivion vanilla data takes 20 minutes. :)
     try {
         $target = $input->getArgument('target');
         $this->threadsNumber = $input->getArgument('threadsNumber');
         $buildTarget = BuildTargetFactory::get($target);
         if (count(array_slice(scandir($buildTarget->getWorkspacePath()), 2)) > 0 || count(array_slice(scandir($buildTarget->getTranspiledPath()), 2)) > 0 || count(array_slice(scandir($buildTarget->getArtifactsPath()), 2)) > 0) {
             $output->writeln("Target " . $target . " current build dir not clean, archive it manually.");
             return;
         }
         $output->writeln("Starting transpiling reactor using " . $this->threadsNumber . " threads...");
         $reactor = \Amp\reactor();
         $reactor->run(function () use($buildTarget, $output, $reactor) {
             $errorLog = fopen($buildTarget->getErrorLogPath(), "w+");
             $sourceFiles = array_slice(scandir($buildTarget->getSourcePath()), 2);
             $buildPlanBuilder = new TES5BuildPlanBuilder(unserialize(file_get_contents('app/graph')));
             $buildPlan = $buildPlanBuilder->createBuildPlan($sourceFiles, $this->threadsNumber);
             $totalSourceFiles = count($sourceFiles);
             $progressBar = new CliProgressBar($totalSourceFiles);
             $progressBar->display();
             $promises = [];
             foreach ($buildPlan as $threadBuildPlan) {
                 $task = new TranspileChunkJob($buildTarget->getTargetName(), $threadBuildPlan);
                 $deferred = new \Amp\Deferred();
                 \Amp\once(function () use($deferred, $task) {
                     $task->runTask($deferred);
                 }, 0);
                 $promise = $deferred->promise();
                 $promise->when(function (\Exception $e = null, $return = null) use($output, $errorLog) {
                     if ($e) {
                         $output->writeln('Exception ' . get_class($e) . ' occurred in one of the threads while transpiling, progress bar will not be accurate..');
                         fwrite($errorLog, get_class($e) . PHP_EOL . $e->getMessage() . PHP_EOL);
                     }
                 });
                 $promise->watch(function ($data) use($progressBar, $errorLog) {
                     $progressBar->progress(count($data['scripts']));
                     if (isset($data['exception'])) {
                         fwrite($errorLog, implode(', ', $data['scripts']) . PHP_EOL . $data['exception']);
                     }
                 });
                 $promises[] = $promise;
             }
             /**
              * @var Promise $transpilingPromise
              */
             $transpilingPromise = \Amp\any($promises);
             $transpilingPromise->when(function () use($reactor, $progressBar, $errorLog) {
                 $progressBar->end();
                 fclose($errorLog);
                 $reactor->stop();
             });
         });
         $output->writeln("Preparing build workspace...");
         /*
          *
          * @TODO - Create a factory that will provide a PrepareWorkspaceJob based on running system, so we can provide a
          * native implementation for Windows
          */
         $prepareCommand = new PrepareWorkspaceJob($buildTarget->getTargetName());
         $prepareCommand->run();
         $output->writeln("Workspace prepared...");
         $task = new CompileScriptJob($buildTarget->getTargetName());
         $task->run();
         $output->writeln("Build completed, archiving ...");
         /*
          *
          * @TODO - Create a factory that will provide a PrepareWorkspaceJob based on running system, so we can provide a
          * native implementation for Windows
          */
         $prepareCommand = new ArchiveBuildJob($buildTarget->getTargetName());
         $prepareCommand->run();
     } catch (\LogicException $e) {
         $output->writeln("Unknown target " . $target . ", exiting.");
         return;
     } catch (\Exception $e) {
         var_dump($e->getMessage());
         exit;
     }
 }
Example #3
0
 private function initializeIdleTimeout(SocketPoolStruct $poolStruct)
 {
     if (isset($poolStruct->idleWatcher)) {
         \Amp\enable($poolStruct->idleWatcher);
     } else {
         $poolStruct->idleWatcher = \Amp\once(function () use($poolStruct) {
             $this->unloadSocket($poolStruct->uri, $poolStruct->id);
         }, $poolStruct->msIdleTimeout);
     }
 }
Example #4
0
 private function loadNewServer($uri)
 {
     if (!($socket = @\stream_socket_client($uri, $errno, $errstr, 0, STREAM_CLIENT_ASYNC_CONNECT))) {
         throw new ResolutionException(sprintf("Connection to %s failed: [Error #%d] %s", $uri, $errno, $errstr));
     }
     \stream_set_blocking($socket, false);
     $id = (int) $socket;
     $server = new \StdClass();
     $server->id = $id;
     $server->uri = $uri;
     $server->socket = $socket;
     $server->buffer = "";
     $server->length = INF;
     $server->pendingRequests = [];
     $server->watcherId = \Amp\onReadable($socket, $this->makePrivateCallable("onReadable"), ["enable" => true, "keep_alive" => true]);
     $this->serverIdMap[$id] = $server;
     $this->serverUriMap[$uri] = $server;
     if (substr($uri, 0, 6) == "tcp://") {
         $promisor = new Deferred();
         $server->connect = $promisor->promise();
         $watcher = \Amp\onWritable($server->socket, static function ($watcher) use($server, $promisor, &$timer) {
             \Amp\cancel($watcher);
             \Amp\cancel($timer);
             unset($server->connect);
             $promisor->succeed();
         });
         $timer = \Amp\once(function () use($id, $promisor, $watcher, $uri) {
             \Amp\cancel($watcher);
             $this->unloadServer($id);
             $promisor->fail(new TimeoutException("Name resolution timed out, could not connect to server at {$uri}"));
         }, 5000);
     }
     return $server;
 }
Example #5
0
 private function afterHeaderWrite(RequestCycle $cycle)
 {
     $body = $cycle->request->getBody();
     if ($body == '') {
         // We're finished if there's no body in the request.
         $cycle->futureResponse->update([Notify::REQUEST_SENT, $cycle->request]);
     } elseif ($this->requestExpects100Continue($cycle->request)) {
         $cycle->continueWatcher = \Amp\once(function () use($cycle) {
             $this->proceedFrom100ContinueState($cycle);
         }, $cycle->options[self::OP_MS_100_CONTINUE_TIMEOUT]);
     } else {
         $this->writeBody($cycle, $body);
     }
 }
Example #6
0
<?php

require __DIR__ . '/../vendor/autoload.php';
$i = 0;
\Amp\repeat(function () use(&$i) {
    echo "\n{$i} iterations";
    $i = 0;
}, 1000);
\Amp\once(function () {
    \Amp\stop();
}, 5000);
\Amp\once(function () {
    \Amp\stop();
}, 10000);
echo "##### SET OP/S";
\Amp\run(function () use(&$i) {
    $memcached = new \Edo\Memcached();
    $memcached->addServer('tcp://127.0.0.1', 11211);
    while (true) {
        $stats = (yield $memcached->set('key', 'value_key'));
        $i++;
    }
});
echo "\n##### GET OP/S";
\Amp\run(function () use(&$i) {
    $memcached = new \Edo\Memcached();
    $memcached->addServer('tcp://127.0.0.1', 11211);
    while (true) {
        $stats = (yield $memcached->get('key'));
        $i++;
    }
Example #7
0
 public function testTouchCommand()
 {
     \Amp\run(function () {
         $set = (yield $this->memcached->set('key_touch', 'value_touch', 0));
         $get = (yield $this->memcached->get('key_touch'));
         $this->assertEquals('value_touch', $get);
         $touch = (yield $this->memcached->touch('key_touch', 1));
         $this->assertInternalType('bool', $touch);
         $this->assertTrue($touch);
         $get = (yield $this->memcached->get('key_touch'));
         $this->assertEquals('value_touch', $get);
         \Amp\once(function () {
             $get = (yield $this->memcached->get('key_touch'));
             $this->assertFalse($get);
             \Amp\stop();
         }, 1000);
     });
 }