Skip to content

Instantly share code, notes, and snippets.

@1v
Forked from andruby/deploy.rb
Last active April 4, 2016 05:07
Show Gist options
  • Save 1v/f438a9076dad84b8d950 to your computer and use it in GitHub Desktop.
Save 1v/f438a9076dad84b8d950 to your computer and use it in GitHub Desktop.

Revisions

  1. 1v revised this gist Apr 4, 2016. 1 changed file with 102 additions and 22 deletions.
    124 changes: 102 additions & 22 deletions resque.rake
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,65 @@
    require 'resque/tasks'
    require 'resque/scheduler/tasks'

    def get_pgid(file)
    pgid = true
    if File.file?(file)
    fpid = File.read(file).to_i
    pgid = `ps -o pgid --no-headers --pid #{fpid}`.strip!
    if pgid
    puts "Process group ID found: #{pgid}"
    pgid = pgid.to_i
    end
    end
    pgid
    end

    # Start a worker with proper env vars and output redirection
    def run_worker(queue, count = 1)
    puts "Starting #{count} worker(s) with QUEUE: #{queue}"
    ops = {:pgroup => true, :err => [(Rails.root + 'log/resque_err').to_s, 'a'],
    # one pid for first worker
    parent_pid_file = 'tmp/resque.pid'
    # getting process group ID
    ops = {:pgroup => get_pgid(parent_pid_file), :err => [(Rails.root + 'log/resque_err').to_s, 'a'],
    :out => [(Rails.root + 'log/resque_stdout').to_s, 'a']}
    ops_s = {:pgroup => true, :err => [(Rails.root + 'log/resque_scheduler_err').to_s, 'a'],
    :out => [(Rails.root + 'log/resque_scheduler_stdout').to_s, 'a']}
    env_vars = {'QUEUES' => queue.to_s,
    'BACKGROUND' => 'yes',
    'TERM_CHILD' => '1'}
    # 'BACKGROUND' => 'yes',
    'TERM_CHILD' => '1',
    # 'VVERBOSE' => '1',
    'REDISTOGO_URL' => 'redis://localhost:6379/'}
    # adding pid file if there is no yet
    env_vars['PIDFILE'] = parent_pid_file if !File.file?(parent_pid_file)
    count.times {
    ## Using Kernel.spawn and Process.detach because regular system() call would
    ## cause the processes to quit when capistrano finishes
    pid = spawn(env_vars, 'bundle exec rake resque:work', ops)
    Process.detach(pid)
    pid = spawn(env_vars, 'bundle exec rake resque:scheduler', ops_s)
    }
    end

    # Start a worker with proper env vars and output redirection
    def run_scheduler(queue, count = 1)
    puts "Starting #{count} scheduler(s) with QUEUE: #{queue}"

    parent_pid_file = 'tmp/resque_scheduler.pid'

    ops = {:pgroup => get_pgid(parent_pid_file), :err => [(Rails.root + 'log/resque_scheduler_err').to_s, 'a'],
    :out => [(Rails.root + 'log/resque_scheduler_stdout').to_s, 'a']}
    env_vars = {'QUEUES' => queue.to_s,
    # 'BACKGROUND' => 'yes',
    'TERM_CHILD' => '1',
    'REDISTOGO_URL' => 'redis://localhost:6379/'}
    env_vars['PIDFILE'] = parent_pid_file if !File.file?(parent_pid_file)
    count.times {
    ## Using Kernel.spawn and Process.detach because regular system() call would
    ## cause the processes to quit when capistrano finishes
    pid = spawn(env_vars, 'bundle exec rake resque:scheduler', ops)
    Process.detach(pid)
    }
    end

    namespace :resque do

    task :setup => :environment

    desc "Restart running workers"
    @@ -30,36 +68,78 @@ namespace :resque do
    Rake::Task['resque:start_workers'].invoke
    end

    task :restart_schedulers => :environment do
    Rake::Task['resque:stop_schedulers'].invoke
    Rake::Task['resque:start_schedulers'].invoke
    end

    desc "Quit running workers"
    task :stop_workers => :environment do
    pids = Array.new
    Resque.workers.each do |worker|
    pids = pids | worker.worker_pids[0...-1]
    fpid = File.read('tmp/resque.pid')
    File.delete('tmp/resque.pid')
    # getting process group ID
    pgid = `ps -o pgid --no-headers --pid #{fpid}`.strip!
    # killing all workers created by this app
    syscmd = "kill -QUIT -\"#{pgid}\""
    puts "Running syscmd: #{syscmd}"
    begin
    system(syscmd)
    rescue => e
    puts "Error: #{e}"
    end
    if pids.empty?
    puts "No workers to kill"
    else
    syscmd = "kill -s QUIT #{pids.join(' ')}"
    puts "Running syscmd: #{syscmd}"
    begin
    system(syscmd)
    rescue => e
    puts "Error: #{e}"
    end
    end

    desc "Quit running schedulers"
    task :stop_schedulers => :environment do
    fpid = File.read('tmp/resque_scheduler.pid')
    File.delete('tmp/resque_scheduler.pid')
    pgid = `ps -o pgid --no-headers --pid #{fpid}`.strip!
    syscmd = "kill -QUIT -\"#{pgid}\""
    puts "Running syscmd: #{syscmd}"
    begin
    system(syscmd)
    rescue => e
    puts "Error: #{e}"
    end
    end

    desc "Start workers"
    task :start_workers => :environment do
    run_worker("*")
    # run_worker("high")
    run_worker("drafts_checker")
    # there we need timeout because pid file need to be created
    # sleep(10)
    # run_worker("drafts_checker2")
    # run_worker("drafts_checker3")
    end

    desc "Start schedulers"
    task :start_schedulers => :environment do
    run_scheduler("drafts_checker")
    # sleep(10)
    # run_scheduler("drafts_checker2")
    end

    task :setup_schedule => :setup do
    require 'resque-scheduler'

    Resque.schedule = YAML.load_file('resque_schedule.yml')
    # If you want to be able to dynamically change the schedule,
    # uncomment this line. A dynamic schedule can be updated via the
    # Resque::Scheduler.set_schedule (and remove_schedule) methods.
    # When dynamic is set to true, the scheduler process looks for
    # schedule changes and applies them on the fly.
    # Note: This feature is only available in >=2.0.0.
    # Resque::Scheduler.dynamic = true

    # The schedule doesn't need to be stored in a YAML, it just needs to
    # be a hash. YAML is usually the easiest.
    Resque.schedule = YAML.load_file('config/resque_schedule.yml')

    # If your schedule already has +queue+ set for each job, you don't
    # need to require your jobs. This can be an advantage since it's
    # less code that resque-scheduler needs to know about. But in a small
    # project, it's usually easier to just include you job classes here.
    # So, something like this:
    # require 'jobs'
    end

    task :scheduler => :setup_schedule
  2. 1v revised this gist Nov 24, 2015. 1 changed file with 28 additions and 8 deletions.
    36 changes: 28 additions & 8 deletions resque.rake
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,22 @@
    require 'resque/tasks'
    require 'resque/scheduler/tasks'

    # Start a worker with proper env vars and output redirection
    def run_worker(queue, count = 1)
    puts "Starting #{count} worker(s) with QUEUE: #{queue}"
    ops = {:pgroup => true, :err => [(Rails.root + "log/resque_err").to_s, "a"],
    :out => [(Rails.root + "log/resque_stdout").to_s, "a"]}
    ops = {:pgroup => true, :err => [(Rails.root + 'log/resque_err').to_s, 'a'],
    :out => [(Rails.root + 'log/resque_stdout').to_s, 'a']}
    ops_s = {:pgroup => true, :err => [(Rails.root + 'log/resque_scheduler_err').to_s, 'a'],
    :out => [(Rails.root + 'log/resque_scheduler_stdout').to_s, 'a']}
    env_vars = {'QUEUES' => queue.to_s,
    'BACKGROUND' => 'yes',
    'TERM_CHILD' => '1'}
    count.times {
    ## Using Kernel.spawn and Process.detach because regular system() call would
    ## cause the processes to quit when capistrano finishes
    pid = spawn(env_vars, "rake resque:work", ops)
    pid = spawn(env_vars, 'bundle exec rake resque:work', ops)
    Process.detach(pid)
    pid = spawn(env_vars, 'bundle exec rake resque:scheduler', ops_s)
    Process.detach(pid)
    }
    end
    @@ -22,7 +29,7 @@ namespace :resque do
    Rake::Task['resque:stop_workers'].invoke
    Rake::Task['resque:start_workers'].invoke
    end

    desc "Quit running workers"
    task :stop_workers => :environment do
    pids = Array.new
    @@ -34,13 +41,26 @@ namespace :resque do
    else
    syscmd = "kill -s QUIT #{pids.join(' ')}"
    puts "Running syscmd: #{syscmd}"
    system(syscmd)
    begin
    system(syscmd)
    rescue => e
    puts "Error: #{e}"
    end
    end
    end

    desc "Start workers"
    task :start_workers => :environment do
    run_worker("*", 2)
    run_worker("high", 1)
    run_worker("*")
    # run_worker("high")
    end

    task :setup_schedule => :setup do
    require 'resque-scheduler'

    Resque.schedule = YAML.load_file('resque_schedule.yml')

    end

    task :scheduler => :setup_schedule
    end
  3. 1v revised this gist Nov 21, 2015. 2 changed files with 4 additions and 23 deletions.
    21 changes: 0 additions & 21 deletions deploy.rb
    Original file line number Diff line number Diff line change
    @@ -1,21 +0,0 @@
    after "deploy:symlink", "deploy:restart_workers"

    ##
    # Rake helper task.
    # http://pastie.org/255489
    # http://geminstallthat.wordpress.com/2008/01/27/rake-tasks-through-capistrano/
    # http://ananelson.com/said/on/2007/12/30/remote-rake-tasks-with-capistrano/
    def run_remote_rake(rake_cmd)
    rake_args = ENV['RAKE_ARGS'].to_s.split(',')
    cmd = "cd #{fetch(:latest_release)} && #{fetch(:rake, "rake")} RAILS_ENV=#{fetch(:rails_env, "production")} #{rake_cmd}"
    cmd += "['#{rake_args.join("','")}']" unless rake_args.empty?
    run cmd
    set :rakefile, nil if exists?(:rakefile)
    end

    namespace :deploy do
    desc "Restart Resque Workers"
    task :restart_workers, :roles => :db do
    run_remote_rake "resque:restart_workers"
    end
    end
    6 changes: 4 additions & 2 deletions resque.rake
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,9 @@ def run_worker(queue, count = 1)
    puts "Starting #{count} worker(s) with QUEUE: #{queue}"
    ops = {:pgroup => true, :err => [(Rails.root + "log/resque_err").to_s, "a"],
    :out => [(Rails.root + "log/resque_stdout").to_s, "a"]}
    env_vars = {"QUEUE" => queue.to_s}
    env_vars = {'QUEUES' => queue.to_s,
    'BACKGROUND' => 'yes',
    'TERM_CHILD' => '1'}
    count.times {
    ## Using Kernel.spawn and Process.detach because regular system() call would
    ## cause the processes to quit when capistrano finishes
    @@ -25,7 +27,7 @@ namespace :resque do
    task :stop_workers => :environment do
    pids = Array.new
    Resque.workers.each do |worker|
    pids.concat(worker.worker_pids)
    pids = pids | worker.worker_pids[0...-1]
    end
    if pids.empty?
    puts "No workers to kill"
  4. @andruby andruby created this gist Jan 26, 2011.
    21 changes: 21 additions & 0 deletions deploy.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    after "deploy:symlink", "deploy:restart_workers"

    ##
    # Rake helper task.
    # http://pastie.org/255489
    # http://geminstallthat.wordpress.com/2008/01/27/rake-tasks-through-capistrano/
    # http://ananelson.com/said/on/2007/12/30/remote-rake-tasks-with-capistrano/
    def run_remote_rake(rake_cmd)
    rake_args = ENV['RAKE_ARGS'].to_s.split(',')
    cmd = "cd #{fetch(:latest_release)} && #{fetch(:rake, "rake")} RAILS_ENV=#{fetch(:rails_env, "production")} #{rake_cmd}"
    cmd += "['#{rake_args.join("','")}']" unless rake_args.empty?
    run cmd
    set :rakefile, nil if exists?(:rakefile)
    end

    namespace :deploy do
    desc "Restart Resque Workers"
    task :restart_workers, :roles => :db do
    run_remote_rake "resque:restart_workers"
    end
    end
    44 changes: 44 additions & 0 deletions resque.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    # Start a worker with proper env vars and output redirection
    def run_worker(queue, count = 1)
    puts "Starting #{count} worker(s) with QUEUE: #{queue}"
    ops = {:pgroup => true, :err => [(Rails.root + "log/resque_err").to_s, "a"],
    :out => [(Rails.root + "log/resque_stdout").to_s, "a"]}
    env_vars = {"QUEUE" => queue.to_s}
    count.times {
    ## Using Kernel.spawn and Process.detach because regular system() call would
    ## cause the processes to quit when capistrano finishes
    pid = spawn(env_vars, "rake resque:work", ops)
    Process.detach(pid)
    }
    end

    namespace :resque do
    task :setup => :environment

    desc "Restart running workers"
    task :restart_workers => :environment do
    Rake::Task['resque:stop_workers'].invoke
    Rake::Task['resque:start_workers'].invoke
    end

    desc "Quit running workers"
    task :stop_workers => :environment do
    pids = Array.new
    Resque.workers.each do |worker|
    pids.concat(worker.worker_pids)
    end
    if pids.empty?
    puts "No workers to kill"
    else
    syscmd = "kill -s QUIT #{pids.join(' ')}"
    puts "Running syscmd: #{syscmd}"
    system(syscmd)
    end
    end

    desc "Start workers"
    task :start_workers => :environment do
    run_worker("*", 2)
    run_worker("high", 1)
    end
    end