Skip to content

Commit

Permalink
Support for scheduling jobs in the future
Browse files Browse the repository at this point in the history
  • Loading branch information
severin committed Oct 11, 2014
1 parent 0045b28 commit f4d8348
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/queue_classic/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ def enqueue(method, *args)
end
end

def enqueue_at(time, method, *args)
QC.log_yield(:measure => 'queue.enqueue') do
s="INSERT INTO #{TABLE_NAME} (q_name, method, args, created_at) VALUES ($1, $2, $3, $4)"
res = conn_adapter.execute(s, name, method, JSON.dump(args), time)
end
end

def lock
QC.log_yield(:measure => 'queue.lock') do
s = "SELECT * FROM lock_head($1, $2)"
Expand Down
4 changes: 4 additions & 0 deletions sql/ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ BEGIN
|| ' WHERE locked_at IS NULL'
|| ' AND q_name = '
|| quote_literal(q_name)
|| ' AND created_at <= '
|| quote_literal(now())
|| ' LIMIT '
|| quote_literal(top_boundary)
|| ') limited'
Expand All @@ -37,6 +39,8 @@ BEGIN
|| ' WHERE locked_at IS NULL'
|| ' AND q_name = '
|| quote_literal(q_name)
|| ' AND created_at <= '
|| quote_literal(now())
|| ' ORDER BY id ASC'
|| ' LIMIT 1'
|| ' OFFSET ' || quote_literal(relative_top)
Expand Down
10 changes: 10 additions & 0 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ def test_lock_when_empty
assert_nil(QC.lock)
end

def test_lock_with_future_job
future = Time.now + 3
QC.enqueue_at(future, "Klass.method")
assert_nil QC.lock
until Time.now >= future do sleep 1 end
job = QC.lock
assert_equal("Klass.method", job[:method])
assert_equal([], job[:args])
end

def test_count
QC.enqueue("Klass.method")
assert_equal(1, QC.count)
Expand Down

0 comments on commit f4d8348

Please sign in to comment.