/** * Constructor. * @return void */ public function init() { if (!isset($this->attrs->server['FR_PATH'])) { $this->status(404); $this->finish(); return; } $job = new \PHPDaemon\Core\ComplexJob(function ($job) { $this->wakeup(); }); $this->job = $job; $this->sleep(5, true); $this->attrs->server['FR_PATH'] = \PHPDaemon\FS\FileSystem::sanitizePath($this->attrs->server['FR_PATH']); $job('stat', function ($name, $job) { /** @var \PHPDaemon\Core\ComplexJob $job */ \PHPDaemon\FS\FileSystem::stat($this->attrs->server['FR_PATH'], function ($path, $stat) use($job) { if ($stat === -1) { $this->fileNotFound(); $job->setResult('stat', false); return; } if ($stat['type'] === 'd') { if (!\PHPDaemon\FS\FileSystem::$supported) { $this->file(rtrim($path, '/') . '/index.html'); } else { $job('readdir', function ($name, $job) use($path) { /** @var \PHPDaemon\Core\ComplexJob $job */ \PHPDaemon\FS\FileSystem::readdir(rtrim($path, '/'), function ($path, $dir) use($job) { $found = false; if (is_array($dir)) { foreach ($dir['dents'] as $file) { if ($file['type'] === \EIO_DT_REG) { // is file if (in_array($file['name'], $this->appInstance->indexFiles)) { $this->file($path . '/' . $file['name']); $found = true; break; } } } } if (!$found) { if (isset($this->attrs->server['FR_AUTOINDEX']) && $this->attrs->server['FR_AUTOINDEX']) { $this->autoindex($path, $dir); } else { $this->fileNotFound(); } } $job->setResult('readdir'); }, \EIO_READDIR_STAT_ORDER | \EIO_READDIR_DENTS); }); } } elseif ($stat['type'] === 'f') { $this->file($path); } $job->setResult('stat', $stat); }); }); $job(); }
/** * Called when the worker is ready to go * * @return void */ public function onReady() { // Adding listener // ComplexJob - STATE_WAITING $job = new \PHPDaemon\Core\ComplexJob(function ($job) { // ComplexJob - STATE_DONE /*array ( 'bar' => array ( 'job' => 'bar', 'success' => false, 'line' => 63, ), 'foo' => array ( 'job' => 'foo', 'success' => true, 'line' => 84, 'arg' => array ( 'param' => 'value', ), ), 'baz' => array ( 'job' => 'baz', 'success' => false, 'line' => 94, ), )*/ \PHPDaemon\Core\Daemon::log($job->results); }); // Adding listener // ComplexJob - STATE_WAITING $job->addListener(function ($job) { // ComplexJob - STATE_DONE }); // Adding async job foo $job('foo', $this->foo(['param' => 'value'])); // Adding with 1 sec delay \PHPDaemon\Core\Timer::add(function ($event) use($job) { // Adding async job bar $job('bar', function ($jobname, $job) { \PHPDaemon\Core\Timer::add(function ($event) use($jobname, $job) { // Job done $job->setResult($jobname, ['job' => 'bar', 'success' => false, 'line' => __LINE__]); $event->finish(); }, 1000.0 * 50); }); // Adding async job baz. Equal $job('baz', $this->baz()); $job->addJob('baz', $this->baz()); // Run jobs. All listeners will be called when the jobs done // ComplexJob - STATE_RUNNING $job(); $event->finish(); }, 1000000.0 * 1); }
public function updateServer($server) { if (!isset($server['address'])) { return; } $server['address'] = trim($server['address']); $app = $this; if (isset($app->jobMap[$server['address']])) { //\PHPDaemon\Daemon::log('already doing: '.$server['address']); return; } $job = new \PHPDaemon\Core\ComplexJob(function ($job) use($app, $server) { unset($app->jobMap[$server['address']]); //\PHPDaemon\Daemon::log('Removed job for '.$server['address']. ' ('.sizeof($app->jobMap).')'); $set = $job->results['info']; $set['address'] = $server['address']; $set['players'] = $job->results['players']; $set['latency'] = $job->results['latency']; $set['atime'] = time(); if (0) { \PHPDaemon\Core\Daemon::log('Updated server (' . round(memory_get_usage(true) / 1024 / 1024, 5) . '): ' . $server['address'] . ' latency = ' . round($set['latency'] * 1000, 2) . ' ==== ' . (isset($server['atime']) ? round($set['atime'] - $server['atime']) . ' secs. from last update.' : ' =---= ' . json_encode($server))); } try { $app->servers->upsert(['_id' => $server['_id']], ['$set' => $set]); } catch (\MongoException $e) { \PHPDaemon\Core\Daemon::uncaughtExceptionHandler($e); $app->servers->upsert(['_id' => $server['_id']], ['$set' => ['atime' => time()]]); } }); $app->jobMap[$server['address']] = $job; //\PHPDaemon\Daemon::log('Added job for '.$server['address']); $job('info', function ($jobname, $job) use($app, $server) { $app->client->requestInfo($server['address'], function ($conn, $result) use($app, $server, $jobname, $job) { $job('players', function ($jobname, $job) use($app, $server, $conn) { $conn->requestPlayers(function ($conn, $result) use($app, $jobname, $job) { $job->setResult($jobname, $result); $conn->finish(); }); }); $job->setResult($jobname, $result); }); }); $job('latency', function ($jobname, $job) use($app, $server) { $app->client->ping($server['address'], function ($conn, $result) use($app, $jobname, $job) { $job->setResult($jobname, $result); $conn->finish(); }); }); $job(); }