davidalger / capistrano-magento2

Magento 2 specific tasks for Capistrano 3
https://rubygems.org/gems/capistrano-magento2
Open Software License 3.0
228 stars 75 forks source link

Issue with DI compilation happening in parallel #136

Closed erikhansen closed 4 years ago

erikhansen commented 4 years ago

Leaving this here in case it's helpful to anyone else who might run into this.

I'm deploying Magento 2.3.2 to multiple application servers. I was intermittently getting this error during the magento:setup:di:compile step:

      01 php -f bin/magento -- setup:di:compile --no-ansi
      01
      01 In AbstractFactory.php line 124:
      01
      01   Type Error occurred when creating object: Magento\Setup\Module\Di\Compiler\Log\Writer\Console

I've not run into this issue in other multi-app setups, so it's likely something specific to this environment (third-party extension, etc).

I added the contents below to a tools/cap/lib/capistrano/tasks/di_compile_in_sequence.rake file, and it worked around my issue:

# Run setup:di:compile in series (sequence) since running it in parallel continually resulted in this error:
#
#Type Error occurred when creating object: Magento\Setup\Module\Di\Compiler\Log\Writer\Console
#
# Running setup:di:compile in sequence results in the deployment taking about 1 additional
# minute for each server beyond 1.

Rake::Task["magento:setup:di:compile"].clear_actions
namespace :magento do
  namespace :setup do
    namespace :di do
      desc 'Runs dependency injection compilation routine'
      task :compile do
        # BEGIN EDIT
        on release_roles(:all), in: :sequence do
        # END EDIT
          within release_path do
            with mage_mode: :production do
              output = capture :magento, 'setup:di:compile --no-ansi', verbosity: Logger::INFO

              # 2.1.x doesn't return a non-zero exit code for certain errors (see davidalger/capistrano-magento2#41)
              if output.to_s.include? 'Errors during compilation'
                raise Exception, 'DI compilation command execution failed'
              end
            end
          end
        end
      end
    end
  end
end
davidalger commented 4 years ago

@erikhansen Sounds like you have it solved. Another resolution may be to dump themes, scopes, and i18n to config.php and then commit them allowing this gem to remove the app/etc/env.php file during the build phase preventing any "cross-talk" and/or attempted connections to db or cache backends.

erikhansen commented 4 years ago

@davidalger Ah, thanks for the tip. I had the thought in the back of my mind that configuring a "zero side effect deployment" might resolve this issue, and it sounds like I should have given that though credence.