postgresql을 사용하여 puma에서 Ruby 2.3.3으로 Rails 3를 실행 중입니다. 트위터에 대한 스트리밍 API로 부팅시 스레드를 시작하는 initializer/twitter.rb 파일이 있습니다. rails server
을 사용하여 응용 프로그램을 시작하면 트위터 스트리밍이 작동하고 정상적인 방식으로 웹 사이트에 연결할 수 있습니다. (스트리밍을 다른 스레드에 배치하지 않으면 스트리밍은 작동하지만 스레드는 트위터 스트림에 의해 차단되므로 브라우저에서 응용 프로그램을 볼 수 없습니다. 그러나 puma -C config/puma.rb
을 사용하여 응용 프로그램을 시작하면 다음과 같은 메시지가 표시됩니다.이 메시지는 내 스레드가 시작시 발견되어 잠자기 상태가되었음을 알려줍니다. 시작할 때이 스레드를 백그라운드에서 실행할 수 있도록 퓨마에게 어떻게 알릴 수 있습니까?/twitter.rbPuma는 레일 응용 프로그램을 부팅 할 때 중요한 스레드를 잠입니다.
### START TWITTER THREAD ### if production
if Rails.env.production?
puts 'Starting Twitter Stream...'
Thread.start {
twitter_stream.user do |object|
case object
when Twitter::Tweet
handle_tweet(object)
when Twitter::DirectMessage
handle_direct_message(object)
when Twitter::Streaming::Event
puts "Received Event: #{object.to_yaml}"
when Twitter::Streaming::FriendList
puts "Received FriendList: #{object.to_yaml}"
when Twitter::Streaming::DeletedTweet
puts "Deleted Tweet: #{object.to_yaml}"
when Twitter::Streaming::StallWarning
puts "Stall Warning: #{object.to_yaml}"
else
puts "It's something else: #{object.to_yaml}"
end
end
}
end
설정
초기화/시작시 puma.rb
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
on_worker_boot do
# Valid on Rails up to 4.1 the initializer method of setting `pool` size
ActiveSupport.on_load(:active_record) do
config = ActiveRecord::Base.configurations[Rails.env] ||
Rails.application.config.database_configuration[Rails.env]
config['pool'] = ENV['RAILS_MAX_THREADS'] || 5
ActiveRecord::Base.establish_connection(config)
end
end
메시지 도움에 미리
2017-04-19T23:52:47.076636+00:00 app[web.1]: Connecting to database specified by DATABASE_URL
2017-04-19T23:52:47.115595+00:00 app[web.1]: Starting Twitter Stream...
2017-04-19T23:52:47.229203+00:00 app[web.1]: Received FriendList: --- !ruby/array:Twitter::Streaming::FriendList []
2017-04-19T23:52:47.865735+00:00 app[web.1]: [4] * Listening on tcp://0.0.0.0:13734
2017-04-19T23:52:47.865830+00:00 app[web.1]: [4] ! WARNING: Detected 1 Thread(s) started in app boot:
2017-04-19T23:52:47.865870+00:00 app[web.1]: [4] ! #<Thread:[email protected]/app/config/initializers/twitter.rb:135 sleep> - /app/vendor/ruby-2.3.3/lib/ruby/2.3.0/openssl/buffering.rb:125:in `sysread'
2017-04-19T23:52:47.875056+00:00 app[web.1]: [4] - Worker 0 (pid: 7) booted, phase: 0
2017-04-19T23:52:47.865919+00:00 app[web.1]: [4] Use Ctrl-C to stop
2017-04-19T23:52:47.882759+00:00 app[web.1]: [4] - Worker 1 (pid: 11) booted, phase: 0
2017-04-19T23:52:48.148831+00:00 heroku[web.1]: State changed from starting to up
감사합니다. WARNING: Detected 1 Thread(s) started in app boot
을 언급 한 다른 여러 게시물을 살펴 봤지만 스레드가 중요하지 않은 경우 경고를 무시하는 해답이 있습니다. 내 경우에는 쓰레드가 매우 중요하며이 쓰레드는 잠들지 않아야한다.
업데이트 : 수면 스레드가 실제로 무엇인지를 배웠으며 문제가되지 않습니다. 그러나 나는 내 트위터 스트림 스레드를 시작할 위치가 확실하지 않습니다. 이니셜 라이저에 남아 있어야합니까? 별도의 프로세스 (백그라운드 앱)를 사용해야합니까? 모든 퓨마 작업자에게 트위터 스트림을 시작해야합니까? –