Skip to content
Browse files

Adding index to table. Making setup/removal easy.

Setup is now: rake qc:create
Teardown is now: rake qc:drop
Adding index per #60 and cleaning up #59.
  • Loading branch information...
1 parent b26172e commit 1d03c69e2d2f9b82fc8fc2774c37e2fe6a7adad0 Ryan Smith (ace hacker) committed
View
3 lib/queue_classic.rb
@@ -9,6 +9,7 @@
require "queue_classic/queries"
require "queue_classic/queue"
require "queue_classic/worker"
+require "queue_classic/setup"
module QC
# ENV["LOG_LEVEL"] is used in Scrolls
@@ -17,7 +18,7 @@ module QC
Root = File.expand_path("..", File.dirname(__FILE__))
SqlFunctions = File.join(QC::Root, "/sql/ddl.sql")
DropSqlFunctions = File.join(QC::Root, "/sql/drop_ddl.sql")
- CreateWorkerTable = File.join(QC::Root, "/sql/create_worker_table.sql")
+ CreateTable = File.join(QC::Root, "/sql/create_table.sql")
# You can use the APP_NAME to query for
# postgres related process information in the
View
34 lib/queue_classic/queries.rb
@@ -2,29 +2,6 @@ module QC
module Queries
extend self
- def load_qc
- self.create_worker_table
- self.load_functions
- end
-
- def unload_qc
- self.drop_functions
- self.drop_worker_table
- end
-
- def create_worker_table
- Conn.transaction do
- Conn.execute(File.read(CreateWorkerTable))
- end
- end
-
- def drop_worker_table
- s = 'DROP TABLE IF EXISTS queue_classic_jobs'
- Conn.transaction do
- Conn.execute(s)
- end
- end
-
def insert(q_name, method, args, chan=nil)
QC.log_yield(:action => "insert_job") do
s = "INSERT INTO #{TABLE_NAME} (q_name, method, args) VALUES ($1, $2, $3)"
@@ -61,16 +38,5 @@ def delete_all(q_name=nil)
Conn.execute(*[s, q_name].compact)
end
- def load_functions
- Conn.transaction do
- Conn.execute(File.read(SqlFunctions))
- end
- end
-
- def drop_functions
- Conn.transaction do
- Conn.execute(File.read(DropSqlFunctions))
- end
- end
end
end
View
40 lib/queue_classic/setup.rb
@@ -0,0 +1,40 @@
+module QC
+ module Setup
+ extend self
+
+ def create
+ create_table
+ create_functions
+ end
+
+ def drop
+ drop_functions
+ drop_table
+ end
+
+ def create_table
+ Conn.transaction do
+ Conn.execute(File.read(CreateTable))
+ end
+ end
+
+ def drop_table
+ Conn.transaction do
+ Conn.execute("DROP TABLE IF EXISTS queue_classic_jobs")
+ end
+ end
+
+ def create_functions
+ Conn.transaction do
+ Conn.execute(File.read(SqlFunctions))
+ end
+ end
+
+ def drop_functions
+ Conn.transaction do
+ Conn.execute(File.read(DropSqlFunctions))
+ end
+ end
+
+ end
+end
View
30 lib/queue_classic/tasks.rb
@@ -6,33 +6,21 @@
namespace :qc do
desc "Start a new worker for the (default or $QUEUE) queue"
task :work => :environment do
- QC::Worker.new(
- QC::QUEUE,
- QC::TOP_BOUND,
- QC::FORK_WORKER,
- QC::LISTENING_WORKER,
- QC::MAX_LOCK_ATTEMPTS
- ).start
+ QC::Worker.new.start
end
desc "Returns the number of jobs in the (default or QUEUE) queue"
- task :length => :environment do
- puts QC::Worker.new(
- QC::QUEUE,
- QC::TOP_BOUND,
- QC::FORK_WORKER,
- QC::LISTENING_WORKER,
- QC::MAX_LOCK_ATTEMPTS
- ).length
+ task :count => :environment do
+ QC::Worker.new.queue.count
end
- desc "Ensure the database has the necessary functions for QC"
- task :load_functions => :environment do
- QC::Queries.load_functions
+ desc "Setup queue_classic tables and funtions in database"
+ task :create => :environment do
+ QC::Setup.create
end
- desc "Remove queue_classic functions from database."
- task :drop_functions => :environment do
- QC::Queries.drop_functions
+ desc "Remove queue_classic tables and functions from database."
+ task :drop => :environment do
+ QC::Setup.drop
end
end
View
2 lib/queue_classic/worker.rb
@@ -1,6 +1,8 @@
module QC
class Worker
+ attr_reader :queue
+
def initialize(*args)
if args.length == 5
q_name, top_bound, fork_worker, listening_worker, max_attempts = *args
View
2 queue_classic.gemspec
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|
s.name = "queue_classic"
s.email = "ryan@heroku.com"
- s.version = "2.0.0rc12"
+ s.version = "2.0.0rc13"
s.date = "2012-02-29"
s.description = "queue_classic is a queueing library for Ruby apps. (Rails, Sinatra, Etc...) queue_classic features asynchronous job polling, database maintained locks and no ridiculous dependencies. As a matter of fact, queue_classic only requires pg."
s.summary = "postgres backed queue"
View
82 readme.md
@@ -1,6 +1,6 @@
# queue_classic
-v2.0.0rc12
+v2.0.0rc13
queue_classic is a PostgreSQL-backed queueing library that is focused on
concurrent job locking, minimizing database load & providing a simple &
@@ -10,7 +10,7 @@ queue_classic features:
* Support for multiple queues with heterogeneous workers
* Utilization of Postgres' PUB/SUB
-* JSON encoding for jobs
+* JSON encoding
* Forking workers
* Postgres' rock-solid locking mechanism
* Fuzzy-FIFO support [academic paper](http://www.cs.tau.ac.il/~shanir/nir-pubs-web/Papers/Lock_Free.pdf)
@@ -19,7 +19,7 @@ queue_classic features:
## Proven
-Queue_classic was designed out of necessity. I needed a message queue that was
+queue_classic was designed out of necessity. I needed a message queue that was
fast, reliable, and low maintenance. It was built upon PostgreSQL out of a motivation
of not wanting to add a redis or 0MQ service to my network of services. It boasts
a small API and very few features. It was designed to be simple. Thus, if you need
@@ -58,9 +58,9 @@ database migration.
$ gem install queue_classic
$ createdb queue_classic_test
$ export QC_DATABASE_URL="postgres://username:password@localhost/queue_classic_test"
-$ ruby -r queue_classic -e "QC::Queries.load_qc"
+$ ruby -r queue_classic -e "QC::Setup.create"
$ ruby -r queue_classic -e "QC.enqueue('Kernel.puts', 'hello world')"
-$ ruby -r queue_classic -e "QC::Worker.new.start"
+$ ruby -r queue_classic -e "QC::Worker.new.work"
```
### Ruby on Rails Setup
@@ -94,37 +94,11 @@ require 'queue_classic'
class AddQueueClassic < ActiveRecord::Migration
def self.up
- QC::Queries.load_qc
+ QC::Setup.create
end
def self.down
- QC::Queries.unload_qc
- end
-
-end
-```
-
-The old way:
-
-```ruby
-class AddQueueClassic < ActiveRecord::Migration
-
- def self.up
- create_table :queue_classic_jobs do |t|
- t.string :q_name
- t.string :method
- t.text :args
- t.timestamp :locked_at
- end
- add_index :queue_classic_jobs, :id
- require "queue_classic"
- QC::Queries.load_functions
- end
-
- def self.down
- drop_table :queue_classic_jobs
- require "queue_classic"
- QC::Queries.drop_functions
+ QC::Setup.drop
end
end
@@ -138,36 +112,8 @@ end
require 'queue_classic'
Sequel.migration do
- up do
- QC::Queries.load_qc
- end
-
- down do
- QC::Queries.unload_qc
- end
-end
-```
-
-The old way:
-
-```ruby
-Sequel.migration do
- up do
- create_table :queue_classic_jobs do
- primary_key :id
- String :q_name
- String :details
- Time :locked_at
- end
- require "queue_classic"
- QC::Queries.load_functions
- end
-
- down do
- drop_table :queue_classic_jobs
- require "queue_classic"
- QC::Queries.drop_functions
- end
+ up {QC::Setup.create}
+ down {QC::Setup.down}
end
```
@@ -211,7 +157,7 @@ QC.enqueue("Kernel.printf", "hello %s", "world")
# This method has a hash argument.
QC.enqueue("Kernel.puts", {"hello" => "world"})
-# This method has a array argument.
+# This method has an array argument.
QC.enqueue("Kernel.puts", ["hello", "world"])
```
@@ -252,7 +198,7 @@ p_queue.enqueue("Kernel.printf", "hello %s", "world")
# This method has a hash argument.
p_queue.enqueue("Kernel.puts", {"hello" => "world"})
-# This method has a array argument.
+# This method has an array argument.
p_queue.enqueue("Kernel.puts", ["hello", "world"])
```
@@ -298,8 +244,7 @@ trap('TERM') {exit}
require "your_app"
require "queue_classic"
-worker = QC::Worker.new(q_name, top_bound, fork_worker, listening_worker, max_attempts)
-worker.start
+QC::Worker.new.start
```
#### Sublcass QC::Worker
@@ -345,8 +290,7 @@ require "your_app"
require "queue_classic"
require "my_worker"
-worker = MyWorker.new(q_name, top_bound, fork_worker, listening_worker, max_attempts)
-worker.start
+MyWorker.new.start
```
#### QC::Worker Details
View
7 sql/create_worker_table.sql
@@ -1,7 +0,0 @@
-CREATE TABLE queue_classic_jobs (
- id serial,
- q_name varchar(255),
- method varchar(255),
- args text,
- locked_at timestamp
-);
View
5 test/helper.rb
@@ -19,9 +19,8 @@ def teardown
def init_db(table_name="queue_classic_jobs")
QC::Conn.execute("SET client_min_messages TO 'warning'")
- QC::Conn.execute("DROP TABLE IF EXISTS #{table_name} CASCADE")
- QC::Conn.execute("CREATE TABLE #{table_name} (id serial, q_name varchar(255), method varchar(255), args text, locked_at timestamptz)")
- QC::Queries.load_functions
+ QC::Setup.drop
+ QC::Setup.create
QC::Conn.disconnect
end

0 comments on commit 1d03c69

Please sign in to comment.
Something went wrong with that request. Please try again.