我正在尝试用Goliath框架创建异步api。服务应该写入mysql,将消息添加到RabbitMQ,并接收返回的响应。还应该有一个独立的管理应用程序与Rails构建。我对此有几个问题:
有没有办法在Rails和Goliath之间有效地共享模型?在使用Activerecord或其他orm时有什么问题吗?是否有任何最佳实践、配置(连接池大小、驱动程序)或其他相关选项?我必须使用什么来接收来自AMQP的消息?是构建一个单独的eventmachine守护进程更好,还是我可以使用Goliath的守护进程来做这些事情?谢谢你的预支。
发布于 2012-06-25 22:28:25
这里有一个在歌利亚中使用ActiveRecord模型的快速技巧。通过这种方法,您可以在不使用require的情况下使用模型,但是您在模型级别上没有关系。要获得has_many和belongs_to关系(在此方法中),我将加载模型文件,并在下面的类定义循环中包含包含此类单词的行。
require 'goliath'
require 'active_record'
require 'active_support'
# The location of the Rails app to integrate
RAILS_APP ||= ENV['HOME']+"/dev/qtrack"
# Load the ActiveRecord database configuration, development settings
configpath = File.join(RAILS_APP, "config", "database.yml")
config = YAML::load_file(configpath)
ActiveRecord::Base.establish_connection config["development"]
# Set the names of all Rails models to a constant
MODELS ||= []
models_dir = File.join(RAILS_APP, "app", "models")
model_names = Dir[models_dir+"/*.rb"]
# Loop over each file name, define a class for each
model_names.each do |fname|
mname = File.basename(fname, '.rb').titleize.sub(/ /, '')
eval %Q{
class ::#{mname} < ActiveRecord::Base
end
}
m = mname.constantize
MODELS << m unless MODELS.include?(m)
end
class Hello < Goliath::API
# default to JSON output, allow Yaml as secondary
use Goliath::Rack::Render, ['json', 'yaml']
def response(env)
# Create a Hash with each model name and the object count
models = MODELS.inject({}) {|hsh,model| hsh[model] = model.count; hsh }
[200, {}, models.to_json ]
end
end这是一个基于你的反馈的黑客攻击。
https://stackoverflow.com/questions/11137849
复制相似问题