如何从部署脚本开始思考Sphinx延迟delta rake任务?

时间:2009-12-10 01:04:28

标签: ruby-on-rails capistrano thinking-sphinx

我有思考Sphinx设置和工作但是我在部署期间启动延迟作业rake任务时遇到问题。

我在deploy.rb中有以下任务似乎执行,但是没有处理延迟的作业 - 它们堆叠起来直到我从服务器命令行运行rake ts:dd:

namespace :thinkingsphinx do
  desc 'Start Delayed Job Sphinx delta indexing'
  task :dd do
    run "cd #{current_path} && rake ts:dd RAILS_ENV=#{rails_env} &" 
  end
end

如何让延迟的作业从部署脚本开始运行?

由于

西蒙

3 个答案:

答案 0 :(得分:2)

此链接对于需要从部署脚本重新启动delayed_delta rake任务的任何人都很有用:

http://amitsolanki.com/2010/04/running-delayed-delta-daemon-in-background-for-thinking-sphinx/#comment-5802

答案 1 :(得分:2)

link Florian提供的代码由Amit Solanki编写,有效!

以下是我与Capistrano合作的方法:

安装宝石


使用以下内容创建名为script/delayed_delta的文件:

#!/usr/bin/env ruby
require 'rubygems'
require 'daemons'
dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))

daemon_options = {
  :multiple => false,
  :dir_mode => :normal,
  :dir => File.join(dir, 'tmp', 'pids'),
  :backtrace => true
}
puts 'delayed_delta'

Daemons.run_proc('job_runner', daemon_options) do
  if ARGV.include?('--')
    ARGV.slice! 0..ARGV.index('--')
  else
    ARGV.clear
  end

  Dir.chdir dir
  RAILS_ENV = ARGV.first || ENV['RAILS_ENV'] || 'development'
  require File.join('config', 'environment')

  Delayed::Worker.new(
    :min_priority => ENV['MIN_PRIORITY'],
    :max_priority => ENV['MAX_PRIORITY']
  ).start
end


配置Capistrano

Capistrano需要开始 Sphinx job_runner (使用我们的script/delayed_delta)。

将这样的内容添加到deploy.rb

deploy.task :restart, :roles => :app do
  run "export RAILS_ENV=production && cd #{deploy_to}/current && /usr/bin/rake ts:rebuild"  
  run "export RAILS_ENV=production && cd #{current_path} && /usr/bin/ruby script/delayed_delta start"
end


配置whenever gem

config/schedule.rb添加行中更新Sphinx的索引并启动job_runner(如果尚未运行)

every 30.minutes do
  command "export RAILS_ENV=production && cd /path/to/rails/production && /usr/bin/rake ts:index && /usr/bin/ruby script/delayed_delta start"
end

这会转换为每30分钟运行一次的 crontab 来更新sphinx


最后的笔记和经验教训

  • script/delayed_delta使用 daemon_generator gem启动 job_runner 后台工作程序脚本。这相当于在控制台上运行rake thinking_sphinx:delayed_deltas,但是持久。

  • 确保一次只运行一个 job_runner rake thinking_sphinx:delayed_deltas进程

  • 让Capistrano开始两个Sphinx(rake ts:rebuild)和script/delayed_delta。当我从不同的用户或不同的环境中启动sphinx和delayed_deltas时,我遇到了问题

答案 2 :(得分:1)

我会将您的delayed_job任务放在一个单独的脚本中,然后从cron运行它,或者让它由您选择的监控工具启动/监控(例如monit)。您的部署脚本可以将其终止以确保每次都重新启动它(killall job_runner)。这是我使用的脚本:

#!/usr/bin/env ruby
## this script is for making sure and delayed_jobs get run
##   it is used by thinking sphinx
require File.dirname(__FILE__) + '/../config/environment'

# you can also put the definition of this in config/environments/*.rb so it's different for test, production and development
JobRunnerPidFile = "#{RAILS_ROOT}/tmp/pids/job_runner.pid" 

if File.exists?(JobRunnerPidFile)
  old_pid = File.read(JobRunnerPidFile).to_i
  begin
    if Process.getpgid(old_pid) > 0
      # still running, let's exit silently...
      exit(0)
    end
  rescue
    # looks like nothing is running, so let's carry on
  end
end

File.open(JobRunnerPidFile, "w") {|f| f.write "#{$$}\n" }

Delayed::Worker.new.start