railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.88k stars 2.25k forks source link

Rails Admin connects to DB during asset precompile #3542

Closed coding-bunny closed 2 years ago

coding-bunny commented 2 years ago

Describe the bug

#20 [builder 8/8] RUN --mount=type=ssh     RAILS_ENV=production ASSETS_PRECOMPILE=1 SECRET_KEY_BASE=$(bin/rake secret)     bundle exec rake assets:precompile     && yarn cache clean     && rm -rf /app/tmp/*     && rm -rf /app/node_modules/.cache
#20 sha256:5bc57b6c3a9a14585665787b6177d5d89ac041dad6acca63856cf49ac8e89042
#20 5.331 [Warning] After upgrading RailsAdmin to 3.x you haven't set asset_source yet, using :sprockets as the default.
#20 5.331 To suppress this message, run 'rails rails_admin:install' to setup the asset delivery method suitable to you.
#20 5.964 Running via Spring preloader in process 26
#20 7.970 ** [NewRelic] FATAL : Failed ERB processing configuration file. This is typically caused by a Ruby error in <% %> templating blocks in your newrelic.yml file.
#20 7.970 ** [NewRelic] FATAL : key not found: "NEWRELIC_KEY"
#20 8.210 [RailsAdmin] Could not load model Deployment, assuming model is non existing. (Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2))
#20 8.210 [RailsAdmin] Could not load model Application, assuming model is non existing. (Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2))

Reproduction steps

Expected behavior

Additional context

Add any other context about the problem here.

codealchemy commented 2 years ago

@coding-bunny can you share a bit how your rails_admin initializer is set up (specifically, the model configs)? Have you tried the branch from https://github.com/railsadminteam/rails_admin/pull/3541 to see if that helps mitigate it?

coding-bunny commented 2 years ago
# frozen_string_literal: true

::RailsAdmin.config do |config|
  ### More at https://github.com/sferik/rails_admin/wiki/Base-configuration
  require ::Rails.root.join('lib', 'rails_admin', 'insights.rb')

  ::RailsAdmin::Config::Actions.register(::RailsAdmin::Config::Actions::Insights)

  # Model Configuration
  #
  # Configures the Models for Rails Admin, doing it here removes all the circular warnings.
  config.included_models = %w[Deployment Rollback Application]

  config.model('Deployment') do
    list do
      sort_by :deployed_at

      field :application_name do
        label { 'Application' }
      end
      field :environment
      field :host do
        label { 'Destination' }
        pretty_value do
          view = bindings[:view]
          is_k8s = bindings[:object].application&.k8s?
          environment = bindings[:object].environment
          url = ::RailsAdminHelper.argo_url(environment)
          raw_value = value || '-'
          is_k8s ? view.link_to(value, "#{url}/#{value}", target: '_blank', rel: 'noopener') : raw_value
        end
      end
      field :branch
      field :deployed_at
      field :current do
        filterable true
        label { 'Current?' }
        formatted_value { bindings[:object].current? }
        pretty_value { bindings[:object].current? || '-' }
        export_value { bindings[:object].current? }
      end
    end

    show do
      field :application_name do
        label { 'Application' }
      end
      field :environment
      field :host do
        label { 'Destination' }
        pretty_value do
          view = bindings[:view]
          is_k8s = bindings[:object].application&.k8s?
          environment = bindings[:object].environment
          url = ::RailsAdminHelper.argo_url(environment)
          raw_value = value || '-'
          is_k8s ? view.link_to(value, "#{url}/#{value}", target: '_blank', rel: 'noopener') : raw_value
        end
      end
      field :branch
      field :version do
        pretty_value do
          view = bindings[:view]
          url = bindings[:object].application&.repo_url
          url.blank? ? value : view.link_to(value, "#{url}/commit/#{value}", target: '_blank', rel: 'noopener')
        end
      end
      field :deployed_by
      field :deployed_at
    end
  end

  config.model('Application') do
    list do
      sort_by :name

      field :name
      field :deployment_type
      field :repo_url
    end

    show do
      field :name
      field :deployment_type
      field :repo_url
    end
  end

  config.actions do
    # mandatory
    dashboard
    # mandatory
    index
    # Our own insights plugin
    insights
    export
    # bulk_delete
    show
    # edit
    # delete
    # show_in_app

    ## With an audit adapter, you can add:
    # history_index
    # history_show
  end
end

That's our initializer, so we are using the String approach for configuring the models. Everything works though, it's just that error/warning keeps popping up during the asset precompile since we don't have database access at that point.

darrenterhune commented 2 years ago

We had this issue on heroku. Then had it after we migrated off heroku due to the fact that assets:precompile happens on a separate instance, which does not have access to the db. This is what I did to fix the problem:

# lib/tasks/assets.rake
namespace :assets do
  desc 'Set rails-admin loading env variable'
  task :set_precompiling_assets_env do
    ENV['PRECOMPILING_ASSETS'] = 'true'
  end
end

Rake::Task['assets:environment'].enhance(['assets:set_precompiling_assets_env'])
# config/initializers/rails_admin.rb
if ENV['PRECOMPILING_ASSETS'].blank?
  RailsAdmin.config do |config|
  end
end

I have no idea why pre 3.0 worked and 3.0 didn't but it seems to be class/object loading issues.

coding-bunny commented 2 years ago

Oh right, could just return at the top of the file if that variable is set and skip the initialization. That's actually a pattern we do more often for gems 😅 Will go with that approach for now

mshibuya commented 2 years ago

Closed by #3541.