5

benim veritabanı erişim izin vermeyecek. Böyle basit bir şey:Raylar 3.1 + Cinler mücevher benim Raylar için kurulumu için Unbuntu sunucuda çalışan 3.1 uygulamasını cini çalışıyorum

require File.expand_path('../../config/environment', __FILE__) 
require 'rubygems' 
require 'daemons' 

Daemons.run_proc('my_script') do 
    loop do 
    puts BlogPost.count 
    sleep(5) 
    end 
end 

Ama BlogPost.count varınca, aşağıdaki hatayı alıyorum: my komut MySQL bağlanamıyorum neden

/usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `query': Mysql2::Error: MySQL server has gone away: SHOW FIELDS FROM `blog_posts` (ActiveRecord::StatementInvalid) 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `execute' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `log' 
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `execute' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:473:in `columns' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `initialize' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:185:in `with_connection' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:92:in `initialize' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/base.rb:706:in `call' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/base.rb:706:in `default' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/base.rb:706:in `[]' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/base.rb:706:in `columns' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/base.rb:722:in `column_names' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/relation/calculations.rb:192:in `aggregate_column' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/relation/calculations.rb:213:in `execute_simple_calculation' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/relation/calculations.rb:187:in `perform_calculation' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/relation/calculations.rb:155:in `calculate' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/relation/calculations.rb:58:in `count' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/base.rb:445:in `__send__' 
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/base.rb:445:in `count' 
from script/background_job_processor_control.rb:22 

bir fikrin? BlogPost.count kodunu Daemons.run_proc'dan önce koyarsam, MySQL'e bağlanabilirim.

---------- DÜZENLEME: ÇÖZÜLDÜ! Benim cini stoping/başlatırken

  1. bundle exec ekleyin: ------------

    çözümüne merak herkes için

    , ben bu işe almak için üç şey yapmak zorunda : RAILS_ENV=production bundle exec myscript.rb start.

  2. Bu noktada, MySQL2 hatasını almayı kestim, ancak bir kayıt hatası almaya başladım. Çözüm aşağıdaki kodu blok: ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new('/path/to/log/file.log')
  3. ekleyecekti. Sonra üçüncü bir hata aldım ve çözüm, komut dosyasının daha önceki yerine run_proc bloğu içinde Rails ortamını gerektirmekti.

Benim son komut dosyası şöyle görünür:

require 'rubygems' 
require 'daemons' 

Daemons.run_proc('my_script') do 
    require File.expand_path('../../config/environment', __FILE__) 
    ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new('/path/to/log/file.log') 
    loop do 
    puts BlogPost.count 
    sleep(5) 
    end 
end 
+0

Eğer 'bohça exec my_script_ctl start' ile cinleri başlıyor? – forker

+0

Hmm, hayır '' RAILS_ENV = üretim ruby ​​script/myscript_control.rb start' yapıyordum. Sadece Senin yöntemini yapıyor çalıştı ve şimdi farklı bir hatayı alıyorum: /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.0/lib/active_support/buffered_logger.rb:109:in 'yazma ': kapalı akışı (IOError) – NudeCanalTroll

+0

Yani burada tavsiyesine uyarak (http://stackoverflow.com/questions/5809678/rails-3-daemons-gem-exception-when-querying-model) kurtulmak için günlüğe kaydetme hatası ve geliştirme veritabanımda bazı tablolar bulunmadığını söyleyen yeni bir hata aldım. Ama bunu üretim üzerinde çalıştırmaya çalışıyorum, bu yüzden 'bundle exec' önünde' RAILS_ENV = prodüksiyon' ekledim ve şimdi 'MySQL sunucusunun gitmiş olduğu orijinal hataya döndük'. :( – NudeCanalTroll

cevap

1

düzgün, komut dosyası için Raylar ortamı (bağımlılıklar) önyükleyebilir aşağıdaki gibi başlatmak için:

bundle exec my_script_ctl start 
1

neden hiçbir fikrim yok,

# app env 
ENV['RAILS_ENV'] = 'production' 
require File.expand_path(File.dirname(__FILE__) + "/../config/environment") 

# disconnect first 
ActiveRecord::Base.connection.disconnect! 

# Become a daemon 
Daemons.daemonize 

# connect again 
ActiveRecord::Base.establish_connection ENV['RAILS_ENV'] 

# ... do what you need 
: senaryoyu Daemonizing önce veritabanı bağlantısını kesmek zaman ama, mesela işleri

Bu yardımcı olur umarım.

İlgili konular