Gem rails2.3 vendor/plugins/ 下的 gem 该如何加载到 rack application 里面?

algo31031 · 2014年06月30日 · 最后由 algo31031 回复于 2014年07月03日 · 1993 次阅读

前段时间用faye为系统添加了即时聊天的功能, faye server被打包进rack application, 通过/config/initializers/faye.rb, 使用thin 随rails一同启动.

之后客户追加新功能, 需要对未在线用户, 如果有新的聊天信息, 要对其进行提醒

因为要兼顾IE用户, 未使用faye websocket. 所以判断用户是否在线采取的策略是:

  1. 在rack程序里添加faye server端扩展, 遇到subscribe event则把对应user设为在线状态, 同时记录下使用的clientId
  2. 监听unsubscribe和disconnect事件, 通过clientId找到该user, 将其标记为离线

标记在线/离线时候, 使用到了ActiveRecord的model, 而model里又用到了位于vendor/plugins/下的gem, acts_as_paranoid. 于是在rack里面一顿require, 总算是在本地能跑起来(development 下的thin/webrick, production下apache+passenger) 但是部署到服务器之后, 发现thin启动不起来, 报错说找不到destroy_without_callbacks方法

又折腾了好半天没什么结果, 恳请大家帮我看下是哪儿出了问题, 非常感谢.

thin启动失败的报错信息

/home/college-dev/rails/vendor/plugins/acts_as_paranoid/lib/caboose/acts/paranoid.rb:56:in `alias_method': undefined method `destroy_without_callbacks' for class `UserChatTeam' (NameError)
    from /home/college-dev/rails/vendor/plugins/acts_as_paranoid/lib/caboose/acts/paranoid.rb:56:in `acts_as_paranoid'
    from /home/college-dev/rails/app/models/user_chat_team.rb:2
    from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `load_dependency'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:640:in `new_constants_in'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `load_dependency'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require'
    from faye.ru:14
    from /usr/lib/ruby/gems/1.8/gems/rack-1.3.6/lib/rack/builder.rb:51:in `instance_eval'
    from /usr/lib/ruby/gems/1.8/gems/rack-1.3.6/lib/rack/builder.rb:51:in `initialize'
    from faye.ru:1:in `new'
    from faye.ru:1

faye.ru的内容

require 'faye'
require File.expand_path('../config/initializers/faye_token.rb', __FILE__)
require 'active_record'
require 'mysql'
require 'yaml'

# using 'acts_as_paranoid' as a plugin in 'vendor/plugins'
RAILS_ENV = ENV['RACK_ENV'] 
load 'active_record/associations.rb' 
require File.expand_path('../vendor/plugins/acts_as_paranoid/lib/caboose/acts/paranoid.rb', __FILE__)
require File.expand_path('../vendor/plugins/acts_as_paranoid/lib/caboose/acts/belongs_to_with_deleted_association.rb', __FILE__)
require File.expand_path('../vendor/plugins/acts_as_paranoid/lib/caboose/acts/has_many_through_without_deleted_association.rb', __FILE__)
require File.expand_path('../vendor/plugins/acts_as_paranoid/init.rb', __FILE__)
require File.expand_path('../app/models/user_chat_team.rb', __FILE__)

environment = ENV['RACK_ENV'] || 'production'
dbconfig    = YAML.load(File.read('config/database.yml'))
ActiveRecord::Base.establish_connection(dbconfig[environment])

class ServerAuth
  def incoming(message, callback)

    if message['channel'] !~ %r{^/meta/}
      if message['ext']['auth_token'] != FAYE_TOKEN
        message['error'] = 'Invalid authentication token.'
      else
        message['ext'].delete('auth_token')
      end
    end

    callback.call(message)
  end
end

class  MarkOnline
  def incoming(message, callback)

    if message['channel'] == '/meta/subscribe'
      UserChatTeam.mark_online( message['data']['user_id'], 
                                message['data']['chat_team_id'],
                                message['clientId']) 
    end

    callback.call(message)
  end

end


faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
faye_server.add_extension(ServerAuth.new)
faye_server.add_extension(MarkOnline.new)

faye_server.on('unsubscribe') do |client_id, channel|
  UserChatTeam.mark_offline(client_id)  
end

faye_server.on('disconnect') do |client_id|
  UserChatTeam.mark_offline(client_id)   
end

run faye_server

faye.rb的内容

# 随rails一起启动faye server
Thread.new do
  system("thin -C #{RAILS_ROOT}/config/faye.ym l start")
end

require 'yaml'

# もしキーが存在しなかったら例外を投げる様にする。
FAYE_CONFIG = Hash.new{|hash, key| raise(IndexError, "no such key : [#{key}] !!")}
FAYE_CONFIG.merge!(YAML.load(File.open(RAILS_ROOT + '/config/faye.yml')))
FAYE_CONFIG.each do |k,v|
  if v.is_a?(Hash)
    v.replace(Hash.new{|hash,key| raise(IndexError, "no such a key in FAYE_CONFIG : #{key} !!")}.merge(v))
  end
end

# FAYE_CONFIGのキーが全てSymbolで指定してあるかチェック
# Symbol以外の値は不正なので、例外を出して起動を中止する。
def faye_config_valid?(hash)
  hash.each do |k,v|
    unless k.is_a?(Symbol)
      # Railsの仕様で、environment.rb で例外を出しても起動してしまうが、
      # gem の読み込みエラーのみ停止してくれるので、仕方なくそれを利用する。
      # 詳細は config/boot.rb を参照のこと。
      require '/config/faye.yml のキーは全てシンボルでなければいけません。'
    end

    case 
    when v.is_a?(Hash)   # Hashならば、再帰処理
      faye_config_valid?(v)
    when v.is_a?(String) # Stringならば、{RAILS_ROOT}を実際の値に書き換える.
      v.sub!(/\{RAILS_ROOT\}/, RAILS_ROOT)
    end
  end
end
faye_config_valid?(FAYE_CONFIG)

# Don't Comment out!!
FAYE_CONFIG.freeze
共收到 1 条回复

自己回来结贴吧, 趟不过去只好暂且绕过去

标记上下线的策略不变, 但是不再在faye.ru里直接调用ActiveRecord controller添加了2个action, rack里直接向rails发request了

uri = URI.parse("#{ ENV['RAILS_ENV'] == 'development' ? 'http://localhost:3000' : FAYE_HOST }/mypage/chat_team/mark_online")
http = Net::HTTP.new(uri.host, uri.port)
if ENV['RAILS_ENV'] != 'development' && FAYE_HOST =~ /^https/
  http.use_ssl = true 
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({ :user_id => message['data']['user_id'],
                        :chat_team_id => message['data']['chat_team_id'],
                        :client_id => message['clientId'],
                        :faye_foken => FAYE_TOKEN})
http.request(request)
2楼 已删除
3楼 已删除
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册