queue_classic-later 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +55 -0
- data/Rakefile +1 -0
- data/lib/queue_classic-later.rb +1 -0
- data/lib/queue_classic/later.rb +63 -0
- data/lib/queue_classic/later/version.rb +5 -0
- data/queue_classic-later.gemspec +17 -0
- metadata +60 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Dan Peterson
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# QC::Later
|
2
|
+
|
3
|
+
Do things later with [queue_classic](https://github.com/ryandotsmith/queue_classic).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem "queue_classic-later"
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install queue_classic-later
|
18
|
+
|
19
|
+
## Setup
|
20
|
+
|
21
|
+
First, follow the queue_classic setup directions.
|
22
|
+
|
23
|
+
QC::Later has database setup to do, much like queue_classic itself. Use the same process suggested by the queue_classic
|
24
|
+
directions to run `QC::Later::Setup.create` in a database migration or similar.
|
25
|
+
|
26
|
+
You'll need to periodically call `QC::Later.tick`, such as via a
|
27
|
+
[clock process](https://devcenter.heroku.com/articles/scheduled-jobs-custom-clock-processes). Calling it every 5-10
|
28
|
+
seconds is probably sufficient.
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
QC::Later adds two methods to `QC::Queue`:
|
33
|
+
|
34
|
+
* `enqueue_in(seconds, method, *args)`: Like `enqueue`, but wait `seconds` before enqueueing the work
|
35
|
+
* `enqueue_at(time, method, *args)`: Like `enqueue_in`, but wait until `time` before enqueueing the work
|
36
|
+
|
37
|
+
Here's an example:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
# Enqueue this in 30 seconds
|
41
|
+
QC.enqueue_in(30, "Kernel.puts", "hello world")
|
42
|
+
|
43
|
+
# Enqueue this at a specific time
|
44
|
+
QC.enqueue_at(Time.new(2012, 10, 13, 12, 34, 56), "Kernel.puts", "hello world")
|
45
|
+
```
|
46
|
+
|
47
|
+
`QC::Later.tick` will enqueue these jobs to be worked by your worker(s) at the appropriate time.
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
1. Fork it
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
53
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
55
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1 @@
|
|
1
|
+
require "queue_classic/later/version"
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require "queue_classic"
|
2
|
+
|
3
|
+
module QC
|
4
|
+
module Later
|
5
|
+
|
6
|
+
TABLE_NAME = "queue_classic_later_jobs"
|
7
|
+
|
8
|
+
module Setup
|
9
|
+
extend self
|
10
|
+
|
11
|
+
def create
|
12
|
+
QC::Conn.transaction do
|
13
|
+
QC::Conn.execute("CREATE TABLE #{QC::Later::TABLE_NAME} (q_name varchar(255), method varchar(255), args text, not_before timestamptz)")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def drop
|
18
|
+
QC::Conn.transaction do
|
19
|
+
QC::Conn.execute("DROP TABLE IF EXISTS #{QC::Later::TABLE_NAME}")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module Queries
|
25
|
+
extend self
|
26
|
+
|
27
|
+
def insert(q_name, not_before, method, args)
|
28
|
+
QC.log_yield(:action => "insert_later_job") do
|
29
|
+
s = "INSERT INTO #{QC::Later::TABLE_NAME} (q_name, not_before, method, args) VALUES ($1, $2, $3, $4)"
|
30
|
+
QC::Conn.execute(s, q_name, not_before, method, QC::OkJson.encode(args))
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def delete_and_capture(not_before)
|
35
|
+
s = "DELETE FROM #{QC::Later::TABLE_NAME} WHERE not_before <= $1 RETURNING *"
|
36
|
+
# need to ensure we return an Array even if Conn.execute returns a single item
|
37
|
+
[QC::Conn.execute(s, not_before)].compact.flatten
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module QueueExtensions
|
42
|
+
def enqueue_in(seconds, method, *args)
|
43
|
+
enqueue_at(Time.now + seconds, method, *args)
|
44
|
+
end
|
45
|
+
|
46
|
+
def enqueue_at(not_before, method, *args)
|
47
|
+
QC::Later::Queries.insert(name, not_before, method, args)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
extend self
|
52
|
+
|
53
|
+
# run QC::Later.tick as often as necessary via your clock process
|
54
|
+
def tick
|
55
|
+
QC::Later::Queries.delete_and_capture(Time.now).each do |job|
|
56
|
+
queue = QC::Queue.new(job["q_name"])
|
57
|
+
queue.enqueue(job["method"], *QC::OkJson.decode(job["args"]))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
QC::Queue.send :include, QC::Later::QueueExtensions
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'queue_classic/later/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "queue_classic-later"
|
8
|
+
gem.version = QC::Later::VERSION
|
9
|
+
gem.authors = ["Dan Peterson"]
|
10
|
+
gem.email = ["dpiddy@gmail.com"]
|
11
|
+
gem.description = %q{Do things later with queue_classic}
|
12
|
+
gem.summary = %q{Do things later with queue_classic}
|
13
|
+
gem.homepage = "https://github.com/dpiddy/queue_classic-later"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: queue_classic-later
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dan Peterson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-28 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Do things later with queue_classic
|
15
|
+
email:
|
16
|
+
- dpiddy@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/queue_classic-later.rb
|
27
|
+
- lib/queue_classic/later.rb
|
28
|
+
- lib/queue_classic/later/version.rb
|
29
|
+
- queue_classic-later.gemspec
|
30
|
+
homepage: https://github.com/dpiddy/queue_classic-later
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
hash: 2878470040603052802
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
hash: 2878470040603052802
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.8.23
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Do things later with queue_classic
|
60
|
+
test_files: []
|