Skip to content

Instantly share code, notes, and snippets.

@simon-engledew
Created February 16, 2010 12:59

Revisions

  1. simon-engledew revised this gist Mar 10, 2010. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions run script for resque w. monit
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    #!/usr/bin/env ruby

    if GC.respond_to?(:copy_on_write_friendly=); GC.copy_on_write_friendly = true; end

    def pidfile
    @pidfile ||= File.expand_path(File.join('..', '..', 'tmp', 'pids', 'consumer.pid'), __FILE__)
    end
  2. simon-engledew created this gist Feb 16, 2010.
    53 changes: 53 additions & 0 deletions run script for resque w. monit
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    #!/usr/bin/env ruby

    def pidfile
    @pidfile ||= File.expand_path(File.join('..', '..', 'tmp', 'pids', 'consumer.pid'), __FILE__)
    end

    case ARGV.first
    when 'start' then
    $stdout.puts('starting consumer')

    require 'rubygems'
    require 'rake'

    ENV['QUEUES'] ||= 'critical,source'
    ENV['COUNT'] ||= '4'
    ENV['RAILS_ENV'] ||= 'production'

    load(File.expand_path(File.join('..', '..', 'Rakefile'), __FILE__))

    Process.detach(consumer = Process.fork do
    begin
    r, w = IO.pipe

    ENV['COUNT'].to_i.times do
    Process.fork do
    begin
    w.close

    Thread.new { r.read; exit }

    Rake::Task['resque:work'].invoke
    rescue Exception => e
    $stdout.puts([e, *e.backtrace].join($/))
    end
    end
    end

    Process.waitall
    rescue Exception => e
    $stdout.puts([e, *e.backtrace].join($/))
    end
    end)

    %x(echo "#{consumer}" > #{pidfile})

    when 'stop' then
    %x(kill `cat #{pidfile}`)
    %x(rm #{pidfile})

    else

    puts 'usage: script/consumer start|stop'
    end