【问题标题】:rails 4 deep_clone different in development/staging and productionrails 4 deep_clone 在开发/登台和生产方面不同
【发布时间】:2020-10-15 23:06:46
【问题描述】:

我正在运行一个 rails 4 应用程序。到目前为止一切正常,但现在来自 gem deep_clonable 的 deep_clone 发生了一个奇怪的错误。我做了一个控制器方法来复制一个条目(交互模型/控制器)。

interactions_controller.rb:

def duplicate
  @interaction = Interaction.find(params[:id]).deep_clone include: [ :interaction_solvents, :interaction_additives], skip_missing_associations: true
  @interaction.user = current_user

respond_to do |format|
    if @interaction.save
      format.html { redirect_to @interaction, notice: 'Item was successfully cloned.' }
    else
      format.html {  redirect_to Interaction.find(params[:id]), notice: 'ERROR: Item can\'t be cloned.'}
    end
  end
end

我使用 docker 进行开发并使用 capistrano 将代码部署到登台和生产服务器。登台和生产服务器是相同的,我部署在 ENV=production 中。对于部署,我使用了两个文件,它们仅在 IP 上有所不同,并且缺少用于登台的部署备份。

config/deploy/production.rb:

user = 'prod'

        before 'deploy:migrate', 'deploy:backup'
    
    server 'XXX.XX.146.242', user: user, roles: %w{app web db}
    puts %w(publickey)
    set :ssh_options, {
      forward_agent: true,
      auth_methods: %w(publickey)
    }
    #set :pty, false
    
    set :linked_files, fetch(:linked_files, []).push(
      '.ruby-version',
      '.ruby-gemset'
    )...

config/deploy/staging.rb:

    user = 'prod'
    
    #before 'deploy:migrate', 'deploy:backup'
    
    server 'XXX.XX.146.243', user: user, roles: %w{app web db}
    puts %w(publickey)
    set :ssh_options, {
      forward_agent: true,
      auth_methods: %w(publickey)
    }
    #set :pty, false
    
    set :linked_files, fetch(:linked_files, []).push(
      '.ruby-version',
      '.ruby-gemset'
    )
    
    #set :tmp_dir, "/home/#{user}/tmp"
    set :user, user

现在它来了。调用重复的所有内容在开发和分期中都可以正常工作。在生产中,复制加倍并生成 2 个新副本。我更新了两台服务器并检查了版本。以下是一些规格:

  • Ubuntu 18.04.4 LTS(GNU/Linux 4.15.0-36-generic x86_64)
  • nginx 版本:nginx/1.14.0 (Ubuntu)
  • Rails 4.2.10
  • postgres (PostgreSQL) 10.12 (Ubuntu 10.12-0ubuntu0.18.04.1)

部分生产日志:

I, [2020-06-25T15:02:10.184115 #32495]  INFO -- : Started GET "/interactions/7336/duplicate" for 127.0.0.1 at 2020-06-25 15:02:10 +0200
I, [2020-06-25T15:02:10.186316 #32495]  INFO -- : Processing by InteractionsController#duplicate as HTML
I, [2020-06-25T15:02:10.186455 #32495]  INFO -- :   Parameters: {"id"=>"7336"}
...
I, [2020-06-25T15:02:11.514185 #32495]  INFO -- : Redirected to http://suprabank.org/interactions/7338
I, [2020-06-25T15:02:11.514595 #32495]  INFO -- : Completed 302 Found in 1328ms (ActiveRecord: 32.0ms)
I, [2020-06-25T15:02:11.590073 #32495]  INFO -- : Started GET "/interactions/7336/duplicate" for 127.0.0.1 at 2020-06-25 15:02:11 +0200
I, [2020-06-25T15:02:11.592145 #32495]  INFO -- : Processing by InteractionsController#duplicate as HTML
I, [2020-06-25T15:02:11.592555 #32495]  INFO -- :   Parameters: {"id"=>"7336"}
...
I, [2020-06-25T15:02:12.705493 #32495]  INFO -- : Redirected to http://suprabank.org/interactions/7339
I, [2020-06-25T15:02:12.705750 #32495]  INFO -- : Completed 302 Found in 1113ms (ActiveRecord: 23.1ms)
I, [2020-06-25T15:02:12.789812 #32495]  INFO -- : Started GET "/interactions/7339" for 127.0.0.1 at 2020-06-25 15:02:12 +0200
I, [2020-06-25T15:02:12.791400 #32495]  INFO -- : Processing by InteractionsController#show as HTML
I, [2020-06-25T15:02:12.791546 #32495]  INFO -- :   Parameters: {"id"=>"7339"}

从日志看来,条目 7336 的第一次复制工作正常,但随后路线走错路,并调用了 7336/duplicate 上的另一个 GET 请求。这不应该发生,也不会在分期中发生。在那个小绕道服务器正确加载(第二个)重复条目 7339 的显示操作之后。

我更新了两台服务器并重新启动了 nginx,但始终相同的登台和生产服务器之间的操作不匹配?

知道错误是什么吗?

提前致谢, 斯蒂芬

【问题讨论】:

  • 我编辑了标签以包含 ruby​​-on-rails 标签,它比 ruby​​-on-rails-x 标签有更多的关注者。
  • 你在使用deep_cloneable gem吗,因为我在official rails repo的任何地方都没有找到deep_clone方法
  • 抱歉忘了提到 gem: deep_cloneable (2.4.0)

标签: ruby-on-rails ruby nginx ruby-on-rails-4 postgresql-10


【解决方案1】:

感谢创建 gem deep_clonable 的 moiristo!他用他的经验帮助了我(我在 GitHub repo 上创建了一个问题来联系他),gem 没有任何错误!

我将操作重新路由为 POST 方法而不是 GET。

routes.rb:

post 'interactions/:id/duplicate', to: 'interactions#duplicate'

index.html.rb:

<%= link_to "", {controller: "interactions", action: 'duplicate', id: interaction.id}, class:'fa fa-clone', :method=>:post%>

在生产服务器上解决了。

感谢您的帮助,尤其是 moiristo。 问候, 斯蒂芬

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-20
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 2019-09-24
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多