在 platform.sh 上使用 SncRedisBundle
using SncRedisBundle on platform.sh
我正在尝试让 platform.sh 托管我的应用程序,我的问题是我使用 SncRedisBundle,配置如下:
#config.yml
imports:
- { resource: platformSh/parameters_platform.php } #platform.sh cloud provider configuration's
snc_redis:
clients:
doctrine:
type: predis
alias: doctrine
dsn: "%redis_url%&database=4"
doctrine:
metadata_cache:
client: doctrine
entity_manager: default # the name of your entity_manager connection
document_manager: default # the name of your document_manager connection
result_cache:
client: doctrine
entity_manager: [default] # you may specify multiple entity_managers
query_cache:
client: doctrine
entity_manager: default
second_level_cache:
client: doctrine
entity_manager: default
#platformSh/parameters_platform.php
$relationships = getenv("PLATFORM_RELATIONSHIPS");
var_dump('$relationships : ', $relationships, @$_ENV['PLATFORM_RELATIONSHIPS']);
if (!$relationships) {
return;
}
$relationships = json_decode(base64_decode($relationships), true);
foreach ($relationships['database'] as $endpoint) {
if (empty($endpoint['query']['is_master'])) {
continue;
}
$container->setParameter('database_driver', 'pdo_' . $endpoint['scheme']);
$container->setParameter('database_host', $endpoint['host']);
$container->setParameter('database_port', $endpoint['port']);
$container->setParameter('database_name', $endpoint['path']);
$container->setParameter('database_user', $endpoint['username']);
$container->setParameter('database_password', $endpoint['password']);
$container->setParameter('database_path', '');
}
if (!empty($relationships['redis'])) {
$redisConstructedDsn = 'redis://'.$relationships['redis'][0]['host'].$relationships['redis'][0]['port'].'?password=';
$container->setParameter('redis_url', $redisConstructedDsn);
var_dump('$redisConstructedDsn : ', $redisConstructedDsn);
} else {
var_dump('$relationships :', $relationships);
}
# Store session into /tmp.
ini_set('session.save_path', '/tmp/sessions');
当我推送到 platform.sh 时,他们的构建过程包括对 composer update 的调用,其结束如下:
Generating optimized autoload files
> Incenteev\ParameterHandler\ScriptHandler::buildParameters
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache
Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache
handling the symfony-scripts event terminated with an exception
[RuntimeException]
An error occurred when executing the "'cache:clear --no-warmup'" command:
string(17) "$relationships : "
bool(false)
NULL
[Predis\Connection\ConnectionException]
php_network_getaddresses: getaddrinfo failed: Name or service not known [tcp://localhost&database=4:6379]
.
我联系了他们的支持团队,他们告诉我:
Hi,
May I know if you're trying to get PLATFORM_RELATIONSHIPS during the
build phase?
That's not available since the build is environment agnostic.
If you need to connect DB / Redis / other services, please do so in
deploy phase (i.e. Deploy Hook).
编辑:支持人员回答我问题出在这个 composer.json 部分:
"scripts": {
"symfony-scripts": [
"Incenteev\ParameterHandler\ScriptHandler::buildParameters",
"Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap",
"Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache",
"Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets",
"Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installRequirementsFile",
"Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::prepareDeploymentTarget"
],
"post-install-cmd": [
"@symfony-scripts"
],
"post-update-cmd": [
"@symfony-scripts"
],
"compile": [
"app/console assetic:dump"
]
},
例如,"Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache"
和 "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets"
都导致 Symfony 清除缓存,这太快了(在构建阶段 platform.sh 不提供env. 链接的 redis 和 MySQL 工作所必需的...)
我不知道如何纠正这个问题,platform.sh 想让我修改 composer.json 文件,但我觉得它很奇怪而且很危险。你会怎么做?
在您的 platform.app.yaml 中,设置
flavor: 'none'
然后用假值创建一个 parameters.dist.yml(在这个文件中添加 redis_url: xxx)。这些假值将在您的 parameters_platform.php
中被覆盖
这里是我的platform.app.yaml的钩子代码:
hooks:
# Build script; can modify the filesystem, but cannot access services
build: |
set -x -e
# remove the development front-controller if present
(>&2 rm web/app_dev.php || true)
SYMFONY_ENV=prod composer install --prefer-dist --optimize-autoloader --classmap-authoritative --no-progress --no-ansi --no-interaction --no-dev
(>&2 SYMFONY_ENV=prod app/console cache:clear --no-warmup)
(>&2 SYMFONY_ENV=prod app/console cache:warmup)
# Keep the cache in a persistant directory
# If your cache can be readonly, you can skip this step
(>&2 mkdir -p tmp/cache && mv app/cache/prod tmp/cache/ && mv app/bootstrap.php.cache tmp/bootstrap.php.cache)
yarn
gulp
# Deploy script, can access services, but the filesystem is read-only
deploy: |
set -x -e
# "install" cache
# If your cache can be readonly, you can skip these steps
rm -rf app/cache/prod
(>&2 SYMFONY_ENV=prod app/console --env=prod cache:clear)
(>&2 SYMFONY_ENV=prod app/console --env=prod doctrine:schema:update --dump-sql --force)
cp -Rp tmp/bootstrap.php.cache app/bootstrap.php.cache
cp -Rp tmp/cache/prod app/cache/
我们项目的platform.php:
if (empty($relationships['redis'])) {
return;
}
$redis = array_shift($relationships['redis']);
$container->setParameter('redis_host', $redis['host']);
$container->setParameter('redis_port', $redis['port']);
我正在尝试让 platform.sh 托管我的应用程序,我的问题是我使用 SncRedisBundle,配置如下:
#config.yml
imports:
- { resource: platformSh/parameters_platform.php } #platform.sh cloud provider configuration's
snc_redis:
clients:
doctrine:
type: predis
alias: doctrine
dsn: "%redis_url%&database=4"
doctrine:
metadata_cache:
client: doctrine
entity_manager: default # the name of your entity_manager connection
document_manager: default # the name of your document_manager connection
result_cache:
client: doctrine
entity_manager: [default] # you may specify multiple entity_managers
query_cache:
client: doctrine
entity_manager: default
second_level_cache:
client: doctrine
entity_manager: default
#platformSh/parameters_platform.php
$relationships = getenv("PLATFORM_RELATIONSHIPS");
var_dump('$relationships : ', $relationships, @$_ENV['PLATFORM_RELATIONSHIPS']);
if (!$relationships) {
return;
}
$relationships = json_decode(base64_decode($relationships), true);
foreach ($relationships['database'] as $endpoint) {
if (empty($endpoint['query']['is_master'])) {
continue;
}
$container->setParameter('database_driver', 'pdo_' . $endpoint['scheme']);
$container->setParameter('database_host', $endpoint['host']);
$container->setParameter('database_port', $endpoint['port']);
$container->setParameter('database_name', $endpoint['path']);
$container->setParameter('database_user', $endpoint['username']);
$container->setParameter('database_password', $endpoint['password']);
$container->setParameter('database_path', '');
}
if (!empty($relationships['redis'])) {
$redisConstructedDsn = 'redis://'.$relationships['redis'][0]['host'].$relationships['redis'][0]['port'].'?password=';
$container->setParameter('redis_url', $redisConstructedDsn);
var_dump('$redisConstructedDsn : ', $redisConstructedDsn);
} else {
var_dump('$relationships :', $relationships);
}
# Store session into /tmp.
ini_set('session.save_path', '/tmp/sessions');
当我推送到 platform.sh 时,他们的构建过程包括对 composer update 的调用,其结束如下:
Generating optimized autoload files > Incenteev\ParameterHandler\ScriptHandler::buildParameters > Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap > Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the symfony-scripts event terminated with an exception
[RuntimeException]
An error occurred when executing the "'cache:clear --no-warmup'" command:
string(17) "$relationships : "
bool(false)
NULL[Predis\Connection\ConnectionException]
php_network_getaddresses: getaddrinfo failed: Name or service not known [tcp://localhost&database=4:6379].
我联系了他们的支持团队,他们告诉我:
Hi,
May I know if you're trying to get PLATFORM_RELATIONSHIPS during the build phase?
That's not available since the build is environment agnostic.
If you need to connect DB / Redis / other services, please do so in deploy phase (i.e. Deploy Hook).
编辑:支持人员回答我问题出在这个 composer.json 部分:
"scripts": {
"symfony-scripts": [
"Incenteev\ParameterHandler\ScriptHandler::buildParameters",
"Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap",
"Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache",
"Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets",
"Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installRequirementsFile",
"Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::prepareDeploymentTarget"
],
"post-install-cmd": [
"@symfony-scripts"
],
"post-update-cmd": [
"@symfony-scripts"
],
"compile": [
"app/console assetic:dump"
]
},
例如,"Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache"
和 "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets"
都导致 Symfony 清除缓存,这太快了(在构建阶段 platform.sh 不提供env. 链接的 redis 和 MySQL 工作所必需的...)
我不知道如何纠正这个问题,platform.sh 想让我修改 composer.json 文件,但我觉得它很奇怪而且很危险。你会怎么做?
在您的 platform.app.yaml 中,设置
flavor: 'none'
然后用假值创建一个 parameters.dist.yml(在这个文件中添加 redis_url: xxx)。这些假值将在您的 parameters_platform.php
中被覆盖这里是我的platform.app.yaml的钩子代码:
hooks:
# Build script; can modify the filesystem, but cannot access services
build: |
set -x -e
# remove the development front-controller if present
(>&2 rm web/app_dev.php || true)
SYMFONY_ENV=prod composer install --prefer-dist --optimize-autoloader --classmap-authoritative --no-progress --no-ansi --no-interaction --no-dev
(>&2 SYMFONY_ENV=prod app/console cache:clear --no-warmup)
(>&2 SYMFONY_ENV=prod app/console cache:warmup)
# Keep the cache in a persistant directory
# If your cache can be readonly, you can skip this step
(>&2 mkdir -p tmp/cache && mv app/cache/prod tmp/cache/ && mv app/bootstrap.php.cache tmp/bootstrap.php.cache)
yarn
gulp
# Deploy script, can access services, but the filesystem is read-only
deploy: |
set -x -e
# "install" cache
# If your cache can be readonly, you can skip these steps
rm -rf app/cache/prod
(>&2 SYMFONY_ENV=prod app/console --env=prod cache:clear)
(>&2 SYMFONY_ENV=prod app/console --env=prod doctrine:schema:update --dump-sql --force)
cp -Rp tmp/bootstrap.php.cache app/bootstrap.php.cache
cp -Rp tmp/cache/prod app/cache/
我们项目的platform.php:
if (empty($relationships['redis'])) {
return;
}
$redis = array_shift($relationships['redis']);
$container->setParameter('redis_host', $redis['host']);
$container->setParameter('redis_port', $redis['port']);