Skip to content

Commit

Permalink
Fix proxy cache warmup (#248)
Browse files Browse the repository at this point in the history
* fix symfony proxy cache warmup

* register framework related services in framework

* test proxy cache generation

---------

Co-authored-by: jlabedo <jean@needelp.com>
  • Loading branch information
jlabedo and jlabedo authored Oct 25, 2023
1 parent 3c66232 commit 368fe50
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 17 deletions.
86 changes: 86 additions & 0 deletions Monorepo/CrossModuleTests/Tests/ProxyCacheGenerationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

use Ecotone\Messaging\Config\ConfiguredMessagingSystem;
use Illuminate\Foundation\Http\Kernel as LaravelKernel;
use Monorepo\Benchmark\FullAppBenchmarkCase;
use Monorepo\ExampleApp\ExampleAppCaseTrait;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpKernel\Kernel as SymfonyKernel;

class ProxyCacheGenerationTest extends FullAppBenchmarkCase
{
use ExampleAppCaseTrait;

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_symfony_prod()
{
self::clearSymfonyCache();
$this->bench_symfony_prod();
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_laravel_prod(): void
{
self::clearLaravelCache();
$this->bench_laravel_prod();
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_lite_application_prod()
{
self::clearLiteApplicationCache();
$this->bench_lite_application_prod();
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_lite_prod()
{
self::clearLiteCache();
$this->bench_lite_prod();
}

public function executeForSymfony(ContainerInterface $container, SymfonyKernel $kernel): void
{
$this->execute($container->get(ConfiguredMessagingSystem::class));
}

public function executeForLaravel(ContainerInterface $container, LaravelKernel $kernel): void
{
$this->execute($container->get(ConfiguredMessagingSystem::class));
}

public function executeForLiteApplication(ContainerInterface $container): void
{
$this->execute($container->get(ConfiguredMessagingSystem::class));
}

public function executeForLite(ConfiguredMessagingSystem $messagingSystem): void
{
$this->execute($messagingSystem);
}

private function execute(ConfiguredMessagingSystem $messagingSystem)
{
$commandBusProxy = $messagingSystem->getCommandBus();
$reflection = new ReflectionClass($commandBusProxy);
$filename = $reflection->getFileName();
self::assertFalse(self::isEvalCode($filename), 'Proxy class should not be generated as eval code');
}

private static function isEvalCode(string $fileName): bool
{
return strpos($fileName, "eval()'d code") !== false;
}
}
3 changes: 1 addition & 2 deletions packages/Ecotone/src/Lite/EcotoneLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ private static function prepareConfiguration(ContainerInterface|array $container
$configurationVariableService = InMemoryConfigurationVariableService::create($configurationVariables);
$definitionHolder = null;

$proxyFactory = new ProxyFactory($serviceCacheConfiguration);
$messagingSystemCachePath = $serviceCacheConfiguration->getPath() . DIRECTORY_SEPARATOR . 'messaging_system';
if ($serviceCacheConfiguration->shouldUseCache() && file_exists($messagingSystemCachePath)) {
/** It may fail on deserialization, then return `false` and we can build new one */
Expand All @@ -196,7 +195,7 @@ private static function prepareConfiguration(ContainerInterface|array $container
}

$container = new LazyInMemoryContainer($definitionHolder->getDefinitions(), $externalContainer);
$container->set(ProxyFactory::class, $proxyFactory);
$container->set(ServiceCacheConfiguration::class, $serviceCacheConfiguration);
$container->set(ConfigurationVariableService::REFERENCE_NAME, $configurationVariableService);

$messagingSystem = $container->get(ConfiguredMessagingSystem::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Ecotone\Messaging\Handler\Enricher\PropertyEditorAccessor;
use Ecotone\Messaging\Handler\Enricher\PropertyReaderAccessor;
use Ecotone\Messaging\Handler\ExpressionEvaluationService;
use Ecotone\Messaging\Handler\Gateway\ProxyFactory;
use Ecotone\Messaging\Handler\ReferenceSearchService;
use Ecotone\Messaging\Handler\SymfonyExpressionEvaluationAdapter;
use Ecotone\Messaging\InMemoryConfigurationVariableService;
Expand All @@ -34,11 +35,10 @@ public function process(ContainerBuilder $builder): void
$this->registerDefault($builder, ChannelResolver::class, new Definition(ChannelResolverWithContainer::class, [new Reference(ContainerInterface::class)]));
$this->registerDefault($builder, ReferenceSearchService::class, new Definition(ReferenceSearchServiceWithContainer::class, [new Reference(ContainerInterface::class)]));
$this->registerDefault($builder, ExpressionEvaluationService::REFERENCE, new Definition(SymfonyExpressionEvaluationAdapter::class, [new Reference(ReferenceSearchService::class)], 'create'));
$this->registerDefault($builder, ServiceCacheConfiguration::class, new Definition(ServiceCacheConfiguration::class, factory: 'noCache'));
$this->registerDefault($builder, ProxyFactory::class, new Definition(ProxyFactory::class, [new Reference(ServiceCacheConfiguration::REFERENCE_NAME)]));
$this->registerDefault($builder, PropertyEditorAccessor::class, new Definition(PropertyEditorAccessor::class, [new Reference(ExpressionEvaluationService::REFERENCE)], 'create'));
$this->registerDefault($builder, PropertyReaderAccessor::class, new Definition(PropertyReaderAccessor::class));
$this->registerDefault($builder, ConfiguredMessagingSystem::class, new Definition(MessagingSystemContainer::class, [new Reference(ContainerInterface::class), [], []]));
$this->registerDefault($builder, ConfigurationVariableService::class, new Definition(InMemoryConfigurationVariableService::class, [], 'createEmpty'));
}

private function registerDefault(ContainerBuilder $builder, string $id, object|array|string $definition): void
Expand Down
6 changes: 4 additions & 2 deletions packages/Ecotone/src/Test/ComponentTestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
use Ecotone\Messaging\Config\Container\ProxyBuilder;
use Ecotone\Messaging\Config\Container\Reference;
use Ecotone\Messaging\Config\ServiceCacheConfiguration;
use Ecotone\Messaging\ConfigurationVariableService;
use Ecotone\Messaging\Conversion\AutoCollectionConversionService;
use Ecotone\Messaging\Conversion\ConversionService;
use Ecotone\Messaging\Endpoint\ChannelAdapterConsumerBuilder;
use Ecotone\Messaging\Endpoint\EndpointRunner;
use Ecotone\Messaging\Endpoint\ExecutionPollingMetadata;
use Ecotone\Messaging\Endpoint\MessageHandlerConsumerBuilder;
use Ecotone\Messaging\Endpoint\PollingMetadata;
use Ecotone\Messaging\Handler\Gateway\ProxyFactory;
use Ecotone\Messaging\Handler\MessageHandlerBuilder;

use Ecotone\Messaging\InMemoryConfigurationVariableService;
use function get_class;

use Ramsey\Uuid\Uuid;
Expand All @@ -42,7 +43,8 @@ private function __construct(private InMemoryPSRContainer $container, private Co
public static function create(): self
{
$container = InMemoryPSRContainer::createFromAssociativeArray([
ProxyFactory::class => new ProxyFactory(ServiceCacheConfiguration::noCache()),
ServiceCacheConfiguration::class => ServiceCacheConfiguration::noCache(),
ConfigurationVariableService::REFERENCE_NAME => InMemoryConfigurationVariableService::createEmpty(),
]);
$containerBuilder = new ContainerBuilder();
$containerBuilder->addCompilerPass(new RegisterSingletonMessagingServices());
Expand Down
5 changes: 0 additions & 5 deletions packages/Laravel/src/EcotoneProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,6 @@ function () {
fn () => $serviceCacheConfiguration
);

$this->app->singleton(ProxyFactory::class, function (Application $app) {
$cacheConfiguration = $app->get(ServiceCacheConfiguration::REFERENCE_NAME);
return new ProxyFactory($cacheConfiguration);
});

if ($this->app->runningInConsole()) {
foreach ($definitionHolder->getRegisteredCommands() as $oneTimeCommandConfiguration) {
$commandName = $oneTimeCommandConfiguration->getName();
Expand Down
3 changes: 1 addition & 2 deletions packages/LiteApplication/src/EcotoneLiteApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public static function bootstrap(array $objectsToRegister = [], array $configura
$serviceConfiguration->getCacheDirectoryPath(),
$cacheConfiguration
);
$proxyFactory = new ProxyFactory($serviceCacheConfiguration);
$file = $serviceCacheConfiguration->getPath() . '/CompiledContainer.php';
if ($serviceCacheConfiguration->shouldUseCache() && file_exists($file)) {
$container = require $file;
Expand Down Expand Up @@ -73,7 +72,7 @@ public static function bootstrap(array $objectsToRegister = [], array $configura
$container = $builder->build();
}

$container->set(ProxyFactory::class, $proxyFactory);
$container->set(ServiceCacheConfiguration::class, $serviceCacheConfiguration);

$configurationVariableService = InMemoryConfigurationVariableService::create($configurationVariables);
$container->set(ConfigurationVariableService::REFERENCE_NAME, $configurationVariableService);
Expand Down
9 changes: 5 additions & 4 deletions packages/Symfony/DepedencyInjection/EcotoneExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public function load(array $configs, ContainerBuilder $container)
->withLoadCatalog($config['loadSrcNamespaces'] ? 'src' : '')
->withNamespaces($config['namespaces'])
->withSkippedModulePackageNames($skippedModules)
->withCacheDirectoryPath($container->getParameter('kernel.cache_dir'))
;

if (isset($config['serviceName'])) {
Expand Down Expand Up @@ -70,9 +69,11 @@ public function load(array $configs, ContainerBuilder $container)

$configurationVariableService = new SymfonyConfigurationVariableService($container);

$container->register(ProxyFactory::class)
->setPublic(true)
->setArguments([new Reference(ServiceCacheConfiguration::REFERENCE_NAME)]);
$container->register(ServiceCacheConfiguration::REFERENCE_NAME, ServiceCacheConfiguration::class)
->setArguments([
'%kernel.cache_dir%',
true
]);

$container->register(CacheWarmer::class)->setAutowired(true)->addTag('kernel.cache_warmer');

Expand Down

0 comments on commit 368fe50

Please sign in to comment.