/** * Called when the worker is ready to go * * @return void */ public function onReady() { // Adding listener // ComplexJob - STATE_WAITING $job = new 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, ), )*/ Daemon::log($job->results); }); // Adding listener // ComplexJob - STATE_WAITING $job->addListener(function ($job) { // ComplexJob - STATE_DONE }); // Incapsulate some property in job $job->appInstance = $this; // Adding async job foo $job('foo', $this->foo(array('param' => 'value'))); // Adding with 1 sec delay Timer::add(function ($event) use($job) { // Adding async job bar $job('bar', function ($jobname, $job) { Timer::add(function ($event) use($jobname, $job) { // Job done $job->setResult($jobname, array('job' => 'bar', 'success' => false, 'line' => __LINE__)); $event->finish(); }, 1000.0 * 50); }); // Adding async job baz. Equal $job('baz', $job->appInstance->baz()); $job->addJob('baz', $job->appInstance->baz()); // Run jobs. All listeners will be called when the jobs done // ComplexJob - STATE_RUNNING $job(); $event->finish(); }, 1000000.0 * 1); }
/** * Constructor. * @return void */ public function init() { if (!isset($this->attrs->server['FR_PATH'])) { $this->status(404); $this->finish(); return; } $req = $this; $job = new ComplexJob(function ($job) use($req) { $req->wakeup(); }); $this->job = $job; $this->sleep(1, true); $req->attrs->server['FR_PATH'] = FS::sanitizePath($req->attrs->server['FR_PATH']); $job('stat', function ($name, $job) use($req) { FS::stat($req->attrs->server['FR_PATH'], function ($path, $stat) use($job, $req) { if ($stat === -1) { $req->fileNotFound(); $job->setResult('stat', false); return; } if ($stat['type'] === 'd') { $job('readdir', function ($name, $job) use($path, $req) { FS::readdir(rtrim($path, '/'), function ($path, $dir) use($job, $req) { $found = false; if (is_array($dir)) { foreach ($dir['dents'] as $file) { if ($file['type'] === EIO_DT_REG) { // is file if (in_array($file['name'], $req->appInstance->indexFiles)) { $req->file($path . '/' . $file['name']); $found = true; break; } } } } if (!$found) { if (isset($this->attrs->server['FR_AUTOINDEX']) && $this->attrs->server['FR_AUTOINDEX']) { $this->autoindex($dir); } else { $req->fileNotFound(); } } $job->setResult('readdir'); }, EIO_READDIR_STAT_ORDER | EIO_READDIR_DENTS); }); } elseif ($stat['type'] == 'f') { $this->file($path); } $job->setResult('stat', $stat); }); }); $job(); }