Skip to content

Instantly share code, notes, and snippets.

@vladdancer
Last active October 21, 2023 19:19
Show Gist options
  • Save vladdancer/4c8ea46d2b63eb4fd89d5e51315e931a to your computer and use it in GitHub Desktop.
Save vladdancer/4c8ea46d2b63eb4fd89d5e51315e931a to your computer and use it in GitHub Desktop.
Solution for error while running any drush command on Acquia Cloud Platform within shell script invoked by form submit function
[warning] include_once(/mnt/www/html/myproject/docroot/modules/contrib/acquia_connector/acquia_connector.module): failed to open stream: No such file or directory Extension.php:148
[warning] include_once(): Failed opening '/mnt/www/html/myproject/docroot/modules/contrib/block_style_plugins/block_style_plugins.module' for inclusion (include_path='/mnt/www/html/myprojectstg/vendor/pear/archive_tar:/mnt/www/html/myprojectstg/vendor/pear/console_getopt:/mnt/www/html/myprojectstg/vendor/pear/pear-core-minimal/src:/mnt/www/html/myprojectstg/vendor/pear/pear_exception:.:/usr/local/php7.4/lib/php') Extension.php:148
TypeError: Argument 3 passed to Drush\Drupal\Commands\config\ConfigImportCommands::__construct() must implement interface Drupal\Core\Cache\CacheBackendInterface, instance of Drupal\config_filter\Config\FilteredStorage given, called in /mnt/www/html/myprojectstg/docroot/core/lib/Drupal/Component/DependencyInjection/Container.php on line 259 in Drush\Drupal\Commands\config\ConfigImportCommands->__construct() (line 172 of /mnt/www/html/myprojectstg/vendor/drush/drush/src/Drupal/Commands/config/ConfigImportCommands.php).

Acquia environment issue

Problem: spawning drush command from a shell script that is running in background mode by form submit custom module's function caused errors described in acquia_drush_error.log

If you try to run drush command from shell script in background mode via exec() from form submit (php-fpm) you'll recieve lot's of include_once warnings and fatal errors for service dependencies.

[warning] include_once(/mnt/www/html/proj/docroot/modules/contrib/acquia_connector/acquia_connector.module)
TypeError: Argument 3 passed to ConfigImportCommands::__construct() must implement interface CacheBackendInterface ...

An example command: exec('/path/to/custom/script.sh > /tmp/process.log 2>&1 &'); An example script (script.sh):

#!/bin/bash

cd /drupal/root
drush st

The problem is when exec spawns shell to execute there are no environment acquia variables AH_SITE_GROUP, AH_SITE_GROUP that are used to include correctly acquia settings.php file. And also you need to specify full path to drupal, drush, site root, and HOME env var for drush. Minimal drupal code and script contents:

<php
$acquiaSiteGroup = getenv('AH_SITE_GROUP');
$acquiaEnv = getenv('AH_SITE_ENVIRONMENT');

if (!empty($acquiaEnv) && !empty($acquiaSiteGroup)) {
  $projRoot = "/var/www/html/$acquiaSiteGroup.$acquiaEnv";
  $docRoot = $projRoot . '/docroot';
}
else {
  $docRoot = \Drupal::root();
  $projRoot = trim(shell_exec("cd $root; cd ..; pwd"));
}

$drush = $projRoot . '/vendor/bin/drush';

$cmd = "/path/to/script.sh $projRoot $docRoot $acquiaSiteGroup $acquiaEnv > /tmp/process.log 2>&1 &";
exec($cmd);

script.sh:

#!/bin/bash

proj_root=$1
docroot=$2
ah_site_group=$3
ah_site_env=$4

export HOME=$proj_root

if [[ -z "${AH_SITE_GROUP}" ]]; then
    export AH_SITE_GROUP=$ah_site_group
fi

if [[ -z "${AH_SITE_ENVIRONMENT}" ]]; then
    export AH_SITE_ENVIRONMENT=$ah_site_env
fi

dr="$proj_root/vendor/bin/drush -vv"
$dr --root=$docroot st

Related links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment