task :environment

namespace :jobs do
  desc "Alias for qc:work"
  task :work  => "qc:work"
end

namespace :qc do
  desc "Start a new worker for the (default or $QUEUE) queue"
  task :work  => :environment do
    trap('INT') {exit}
    trap('TERM') {@worker.stop}
    @worker = QC::Worker.new
    @worker.start
  end

  desc "Returns the number of jobs in the (default or QUEUE) queue"
  task :count => :environment do
    puts QC::Worker.new.queue.count
  end

  desc "Setup queue_classic tables and functions in database"
  task :create => :environment do
    QC::Setup.create
  end

  desc "Remove queue_classic tables and functions from database."
  task :drop => :environment do
    QC::Setup.drop
  end
end

task :environment

namespace :jobs do
  desc "Alias for qc:work"
  task :work  => "qc:work"
end

namespace :qc do
  desc "Start a new worker for the (default or $QUEUE) queue"
  task :work  => :environment do
    @worker = QC::Worker.new

    trap('INT') do
      $stderr.puts("Received INT. Shutting down.")
      if !@worker.running
        $stderr.puts("Worker has stopped running. Exit.")
        exit(1)
      end
      @worker.stop
    end

    trap('TERM') do
      $stderr.puts("Received Term. Shutting down.")
      @worker.stop
    end

    @worker.start
  end

  desc "Returns the number of jobs in the (default or QUEUE) queue"
  task :count => :environment do
    puts QC::Worker.new.queue.count
  end

  desc "Setup queue_classic tables and functions in database"
  task :create => :environment do
    QC::Setup.create
  end

  desc "Remove queue_classic tables and functions from database."
  task :drop => :environment do
    QC::Setup.drop
  end
end

Correct sig handling defined in Rake task
Bugfix: Correct sig handling defined in Rake task

The queue_classic rake task will now respond to the following signals:

* INT - Instructs the worker to stop accepting new jobs.
* INT - If INT is sent twice (or the worker's @running ivar is false) exit(1).
* TERM - Set the worker's @running ivar to false.
