我可以使用具有不同RAM量的服务器在MongoDB 1.8中设置副本集吗?
如果是,有什么优点和缺点?
可以不打电话Thread#join吗?在这种情况下,我不在乎线程是否爆炸 - 我只是想让Unicorn继续处理.
class MyMiddleware
def initialize(app)
@app = app
end
def call(env)
t = Thread.new { sleep 1 }
t.join # is it ok if I skip this?
@app.call env
end
end
Run Code Online (Sandbox Code Playgroud)
我会得到"僵尸线程"或类似的东西吗?
我试图获得在Unicorn下运行的Rails应用程序连接到受密码保护的Redis服务器时出现意外和重大问题.
使用bundle exec rails c production命令行中,我可以通过Resque.redis发出命令.但是,当它在Unicorn下分叉时,我的配置似乎正在丢失.
使用非密码保护的Redis服务器Just Works.但是,我打算在其他服务器上运行工作程序,而不是Redis服务器所在的服务器,因此我需要密码保护.
我也成功地使用了密码保护(使用相同的技术)但使用Passenger而不是Unicorn.
我有以下设置:
# config/resque.yml
development: localhost:6379
test: localhost:6379
production: redis://user:PASSWORD@oak.isc.org:6379
Run Code Online (Sandbox Code Playgroud)
.
# config/initializers/redis.rb
rails_root = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/../..'
rails_env = ENV['RAILS_ENV'] || 'development'
$resque_config = YAML.load_file(rails_root + '/config/resque.yml')
uri = URI.parse($resque_config[rails_env])
Resque.redis = Redis.new(host: uri.host, port: uri.port, password: uri.password)
Run Code Online (Sandbox Code Playgroud)
.
# unicorn.rb bootup file
preload_app true
before_fork do |server, worker|
Redis.current.quit
end
after_fork do |server, worker|
Redis.current.quit
end
Run Code Online (Sandbox Code Playgroud)
.
我有包含字段xyz的文档
{ term: "puppies", page: { skip: 1, per_page: 20 } } // not useful as a composite key...
{ page: { skip: 1, per_page: 20 }, term: "puppies" } // different order, same contents
Run Code Online (Sandbox Code Playgroud)
为了确定xyz中的"顶部"值,我想将它们全部映射到类似的东西
emit('term="puppies",page={ skip: 1, per_page: 20 }', 1); // composite key
Run Code Online (Sandbox Code Playgroud)
但是我无法将嵌入的对象变成有意义的字符串:
emit('term="puppies",page=[object bson_object]', 1); // not useful
Run Code Online (Sandbox Code Playgroud)
有关使用函数的任何建议而不是toString()?
# return the top <num> values of <field> based on a query <selector>
#
# example: top(10, :xyz, {}, {})
def top(num, …Run Code Online (Sandbox Code Playgroud) 我想从模型中调度(路由和渲染).(我只关心GET请求,我忽略了Accept:标题,所以我只看PATH_INFO.)
# app/models/response.rb
class Response < ActiveRecord::Base
# col :path_info
# col :app_version
# col :body, :type => :text
def set_body
params = Rails.application.routes.recognize_path(path_info, :method => :get)
controller = "#{params[:controller].camelcase}Controller".constantize.new
controller.action_name = params[:action]
controller.request = ActionDispatch::Request.new('rack.input' => [])
controller.request.path_parameters = params.with_indifferent_access
controller.request.format = params[:format] || 'html'
controller.response = ActionDispatch::Response.new
controller.send params[:action]
self.body = controller.response.body
end
end
Run Code Online (Sandbox Code Playgroud)
上面的代码有效,但感觉很笨拙.什么是正确的方法呢?我想象Yehuda Katz会告诉我类似的事情:
def set_body
# [...]
app = "#{params[:controller].camelcase}Controller".constantize.action(params[:action])
app.process params
self.body = app.response.body …Run Code Online (Sandbox Code Playgroud) 假设我有一个普通的CSV
# helloworld.csv
hello,world,,,"please don't replace quoted stuff like ,,",,
Run Code Online (Sandbox Code Playgroud)
如果我想mysqlimport了解其中的一些领域NULL,那么我需要:
# helloworld.mysql.csv
hello,world,\N,\N,"please don't replace quoted stuff like ,,",\N,\N
Run Code Online (Sandbox Code Playgroud)
我从另一个问题得到了一些帮助 - 为什么sed不能取代重叠模式 - 但请注意问题:
$ perl -pe 'while (s#,,#,\\N,#) {}' -pe 's/,$/,\\N/g' helloworld.csv
hello,world,\N,\N,"please don't replace quoted stuff like ,\N,",\N,\N
^^
Run Code Online (Sandbox Code Playgroud)
如何编写正则表达式,以便,,它们在引号之间不会被替换?
最终的回答
这是我使用的最终perl,感谢下面接受的答案:
perl -pe 's/^,/\\N,/; while (s/,(?=,)(?=(?:[^"]*"[^"]*")*[^"]*$)/,\\N/g) {}; s/,$/,\\N/' helloworld.csv
Run Code Online (Sandbox Code Playgroud)
它处理前导,尾随和不带引号的空字符串.
直接从手册,这是PostgreSQL中merge_db的规范示例:
CREATE TABLE db (a INT PRIMARY KEY, b TEXT);
CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS
$$
BEGIN
LOOP
-- first try to update the key
UPDATE db SET b = data WHERE a = key;
IF found THEN
RETURN;
END IF;
-- not there, so try to insert the key
-- if someone else inserts the same key concurrently,
-- we could get a unique-key failure
BEGIN
INSERT INTO db(a,b) VALUES (key, …Run Code Online (Sandbox Code Playgroud)