2016-12-29 19:16:04 -06:00
|
|
|
<?php
|
2015-08-21 22:41:38 -05:00
|
|
|
|
2016-08-23 16:47:40 -05:00
|
|
|
namespace Amp\Parallel\Worker;
|
2016-08-18 11:04:48 -05:00
|
|
|
|
2017-11-10 09:59:47 -06:00
|
|
|
use Amp\Parallel\Thread\Thread;
|
2017-05-18 09:51:31 +02:00
|
|
|
use Amp\Promise;
|
2015-08-21 22:41:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A worker thread that executes task objects.
|
|
|
|
*/
|
2016-08-18 11:04:48 -05:00
|
|
|
class WorkerThread extends AbstractWorker {
|
2017-07-26 01:02:34 -05:00
|
|
|
/**
|
|
|
|
* @param string $envClassName Name of class implementing \Amp\Parallel\Worker\Environment to instigate.
|
|
|
|
* Defaults to \Amp\Parallel\Worker\BasicEnvironment.
|
|
|
|
*/
|
|
|
|
public function __construct(string $envClassName = BasicEnvironment::class) {
|
|
|
|
parent::__construct(new Thread(function (string $className): Promise {
|
|
|
|
try {
|
|
|
|
$reflection = new \ReflectionClass($className);
|
|
|
|
} catch (\ReflectionException $e) {
|
|
|
|
throw new \Error(\sprintf("Invalid class name '%s'", $className));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$reflection->isInstantiable()) {
|
|
|
|
throw new \Error(\sprintf("'%s' is not instatiable class", $className));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$reflection->implementsInterface(Environment::class)) {
|
|
|
|
throw new \Error(\sprintf("The class '%s' does not implement '%s'", $className, Environment::class));
|
|
|
|
}
|
|
|
|
|
|
|
|
$environment = $reflection->newInstance();
|
|
|
|
|
|
|
|
if (!\defined("AMP_WORKER")) {
|
|
|
|
\define("AMP_WORKER", "amp-worker");
|
|
|
|
}
|
|
|
|
|
|
|
|
$runner = new TaskRunner($this, $environment);
|
2016-08-18 11:04:48 -05:00
|
|
|
return $runner->run();
|
2017-07-26 01:02:34 -05:00
|
|
|
}, $envClassName));
|
2015-08-21 22:41:38 -05:00
|
|
|
}
|
|
|
|
}
|