|
2 | 2 |
|
3 | 3 | namespace Netlogix\JobQueue\FastRabbit\SingletonPreloading; |
4 | 4 |
|
5 | | -use Neos\Flow\Core\Bootstrap; |
| 5 | +use Doctrine\DBAL\Connection; |
| 6 | +use Doctrine\ORM\EntityManagerInterface; |
| 7 | +use Neos\Cache\Frontend\VariableFrontend; |
6 | 8 | use Neos\Flow\ObjectManagement\Configuration\Configuration; |
7 | 9 | use Neos\Flow\ObjectManagement\ObjectManager; |
8 | 10 | use Neos\Flow\ObjectManagement\ObjectManagerInterface; |
9 | 11 | use Neos\Flow\Reflection\ReflectionService; |
10 | 12 | use Neos\Flow\Annotations as Flow; |
11 | 13 | use Throwable; |
12 | | - |
13 | 14 | use Traversable; |
14 | 15 |
|
15 | | -use function array_filter; |
| 16 | +use function class_exists; |
16 | 17 | use function is_a; |
17 | 18 |
|
18 | 19 | /** |
|
29 | 30 | */ |
30 | 31 | class AllSingletonsPreloader implements SingletonsPreloader |
31 | 32 | { |
| 33 | + public const string CACHE = 'Netlogix.JobQueue.FakeQueue:SingletonPreloaderCache'; |
| 34 | + |
32 | 35 | #[Flow\InjectConfiguration(path: 'AllSingletonsPreloader.ignoreClassNames', package: 'Netlogix.JobQueue.FastRabbit')] |
33 | 36 | protected array $ignoreClassNames = []; |
34 | 37 |
|
35 | | - public function __construct( |
36 | | - protected readonly ObjectManager $objectManager, |
37 | | - protected readonly ReflectionService $reflectionService |
38 | | - ) { |
39 | | - } |
| 38 | + #[Flow\Inject(name: AllSingletonsPreloader::CACHE, lazy: false)] |
| 39 | + protected VariableFrontend $cache; |
| 40 | + |
| 41 | + #[Flow\Inject(lazy: false)] |
| 42 | + protected ObjectManager $objectManager; |
| 43 | + |
| 44 | + #[Flow\Inject(lazy: false)] |
| 45 | + protected ReflectionService $reflectionService; |
40 | 46 |
|
41 | 47 | public function collect(): void |
42 | 48 | { |
43 | | - $objectManager = Bootstrap::$staticObjectManager; |
44 | | - foreach ($this->getSingletonClassNames($objectManager) as $className) { |
45 | | - try { |
46 | | - $objectManager->get($className); |
47 | | - } catch (Throwable $e) { |
48 | | - // ignore |
49 | | - } |
| 49 | + foreach ($this->getClassList() as $className => $buildInstance) { |
| 50 | + $this->preload(className: $className, buildInstance: $buildInstance); |
50 | 51 | } |
| 52 | + $this->pauseExpiringObjects(); |
51 | 53 | } |
52 | 54 |
|
53 | 55 | /** |
54 | | - * @return Traversable<string> |
| 56 | + * @return array<string, bool> |
55 | 57 | */ |
56 | | - public function getSingletonClassNames(ObjectManagerInterface $objectManager): Traversable |
| 58 | + protected function getClassList(): array |
57 | 59 | { |
58 | | - foreach (self::getSingletonClassNamesFromReflection($objectManager) as $className) { |
59 | | - foreach ($this->ignoreClassNames as $ignoredClassName) { |
60 | | - if (is_a($className, $ignoredClassName, true)) { |
61 | | - continue; |
62 | | - } |
| 60 | + if ($this->cache->has('classList')) { |
| 61 | + return $this->cache->get('classList'); |
| 62 | + } else { |
| 63 | + $list = [... $this->buildClassList()]; |
| 64 | + $this->cache->set('classList', $list); |
| 65 | + return $list; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + protected function preload(string $className, bool $buildInstance): void |
| 70 | + { |
| 71 | + try { |
| 72 | + $buildInstance |
| 73 | + ? $this->objectManager->get($className) |
| 74 | + : class_exists(class: $className, autoload: true); |
| 75 | + } catch (Throwable) { |
| 76 | + // ignore |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + protected function pauseExpiringObjects() |
| 81 | + { |
| 82 | + if ($this->objectManager->has(EntityManagerInterface::class)) { |
| 83 | + $this->objectManager |
| 84 | + ->get(EntityManagerInterface::class) |
| 85 | + ->getConnection() |
| 86 | + ->close(); |
| 87 | + } |
| 88 | + if ($this->objectManager->has(Connection::class)) { |
| 89 | + $this->objectManager |
| 90 | + ->get(Connection::class) |
| 91 | + ->close(); |
| 92 | + } |
| 93 | + // TODO: There are other objects that might expire, for example ´ |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @return Traversable<string, bool> |
| 98 | + */ |
| 99 | + protected function buildClassList(): Traversable |
| 100 | + { |
| 101 | + foreach (self::getSingletonClassNamesFromReflection($this->objectManager) as $className => $buildInstance) { |
| 102 | + yield $className => $buildInstance && !$this->ignoreClassName($className); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + protected function ignoreClassName(string $className): bool |
| 107 | + { |
| 108 | + foreach ($this->ignoreClassNames as $ignoredClassName) { |
| 109 | + if (is_a($className, $ignoredClassName, true)) { |
| 110 | + return true; |
63 | 111 | } |
64 | | - yield $className; |
65 | 112 | } |
| 113 | + return false; |
66 | 114 | } |
67 | 115 |
|
| 116 | + /** |
| 117 | + * @return array<string, bool> |
| 118 | + */ |
68 | 119 | #[Flow\CompileStatic] |
69 | | - public static function getSingletonClassNamesFromReflection(ObjectManagerInterface $objectManager): array |
| 120 | + final public static function getSingletonClassNamesFromReflection(ObjectManagerInterface $objectManager): array |
70 | 121 | { |
71 | | - return array_filter( |
72 | | - array: $objectManager->get(ReflectionService::class)->getAllClassNames(), |
73 | | - callback: static function ($className) use ($objectManager): bool { |
74 | | - try { |
75 | | - return $objectManager->getScope($className) === Configuration::SCOPE_SINGLETON; |
76 | | - } catch (\Exception $e) { |
77 | | - return false; |
| 122 | + $reflection = $objectManager->get(ReflectionService::class); |
| 123 | + assert($reflection instanceof ReflectionService); |
| 124 | + $classNames = []; |
| 125 | + foreach ($reflection->getAllClassNames() as $className) { |
| 126 | + try { |
| 127 | + if ($objectManager->getScope($className) !== Configuration::SCOPE_SINGLETON) { |
| 128 | + /** |
| 129 | + * Only preload singletons |
| 130 | + */ |
| 131 | + $classNames[$className] = false; |
| 132 | + continue; |
78 | 133 | } |
| 134 | + } catch (\Exception $e) { |
| 135 | + $classNames[$className] = false; |
| 136 | + continue; |
79 | 137 | } |
80 | | - ); |
| 138 | + |
| 139 | + $constructParameters = $reflection->getMethodParameters($className, '__construct'); |
| 140 | + if (count($constructParameters)) { |
| 141 | + /** |
| 142 | + * Skip preloading for classes with constructor arguments because they are |
| 143 | + * likely to depend on stateful objects that, in one way or other, expire, |
| 144 | + * like database connections. |
| 145 | + */ |
| 146 | + $classNames[$className] = false; |
| 147 | + } else { |
| 148 | + $classNames[$className] = true; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return $classNames; |
81 | 153 | } |
82 | 154 | } |
0 commit comments