phpsymfonyrediscomposer-phpcloud-hosting

using SncRedisBundle on platform.sh


I'm trying to get my app hosted by platform.sh, my problem is that I use SncRedisBundle, with this config:


#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');

When I push to platform.sh, their build process include a call to composer update, which end like this:

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]

        .

I contacted their support team, which told me this:

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).


edit : the support has answered to me that the problem lie on this composer.json section:

"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" 
] 
},

for example, the "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache" but also the "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets" are causing Symfony to clear the cache which is do too soon (during the build phase where platform.sh don't provide the env. necessary for the linked redis and MySQL to wok...)


I don't know how to correct this, platform.sh want me to alter the composer.json file, but I find it strange & dangerous. How would you do?


Solution

  • in your platform.app.yaml, set

    flavor: 'none'
    

    and just make a parameters.dist.yml with fake values (add redis_url: xxx in this file). Those fake value will be overridden in your parameters_platform.php

    here is the code of the hook of my 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/
    

    The platform.php of our project:

    if (empty($relationships['redis'])) {
        return;
    }
    
    $redis = array_shift($relationships['redis']);
    
    $container->setParameter('redis_host', $redis['host']);
    $container->setParameter('redis_port', $redis['port']);