How can I start the Thinking Sphinx delayed delta rake task from a script deployment?

I have Thinking Sphinx setup and working, but I had a problem running rake Delayed Job tasks during deployment.

I have the following task in deploy.rb, which seems to be running, however, pending tasks are not processed - they add up until I run rake ts: dd from the server command line:

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

How can I run pending jobs from a deployment script?

thanks

Simon

+3
source share
3 answers

Link Florian provided Amit Solanka code that works!

, 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

crontab, 30 sphinx


  • script/delayed_delta daemon_generator, job_runner script. rake thinking_sphinx:delayed_deltas , .

  • , job_runner rake thinking_sphinx:delayed_deltas

  • Capistrano Sphinx (rake ts: rebuild), script/delayed_delta. , sphinx delayed_deltas .

+2

delayed_job script cron / (, monit). script , (killall job_runner). script :

#!/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 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 exit silently...
      exit (0)
    end
  rescue
    # looks like nothing is running, so let carry on
  end
end

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

Delayed :: Worker.new.start
+1
source

Source: https://habr.com/ru/post/1725239/


All Articles