afterparty 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -2
- data/README.md +7 -1
- data/afterparty_test.sqlite3 +0 -0
- data/docs/dashboard.png +0 -0
- data/lib/afterparty/afterparty_job.rb +10 -0
- data/lib/afterparty/queue.rb +1 -13
- data/lib/afterparty/queue_helpers.rb +17 -50
- data/lib/afterparty/version.rb +1 -1
- data/lib/afterparty/worker.rb +0 -9
- data/spec/afterparty_job_spec.rb +2 -2
- data/spec/helpers.rb +1 -1
- data/spec/queue_functional_spec.rb +1 -7
- data/spec/queue_spec.rb +49 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1cd702bf393ba9846ebeea3e8195dd7f76914488
|
4
|
+
data.tar.gz: a7f24532f757b22660d31720ccd0bec948ba7545
|
5
5
|
!binary "U0hBNTEy":
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86e634284043827e87dcdc3fce39e5be359a3592077dca1427041faa9f1fd147e9d1ed374b6241643836617b5393da9822fca6d985db39d5c6431fa53d5667b1
|
7
|
+
data.tar.gz: c49dbbed658e44ab6a7eb1cc55d0528621a6eb17612744e7f733a00cf91770d0c398d360f1628b7c20cf44310f1657f515c41b773d6f61263479470698493ec2
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -57,11 +57,13 @@ mailer_job.execute_at = Time.now + 20.minutes
|
|
57
57
|
Rails.configuration.queue << mailer_job
|
58
58
|
|
59
59
|
job = Afterparty::BasicJob.new @user, :reset_password
|
60
|
-
Rails.configuration.queue <<
|
60
|
+
Rails.configuration.queue << job
|
61
61
|
~~~
|
62
62
|
|
63
63
|
### Dashboard
|
64
64
|
|
65
|
+
![dashboard screenshot](https://raw.github.com/hstove/afterparty/master/docs/dashboard.png)
|
66
|
+
|
65
67
|
This gem provides a handy dashboard for inspecting, debugging, and re-running jobs.
|
66
68
|
|
67
69
|
Visit [http://localhost:3000/afterparty/](http://localhost:3000/afterparty/) and login with
|
@@ -90,6 +92,10 @@ before_fork do |server, worker|
|
|
90
92
|
|
91
93
|
This has the advantage of, for example, staying within Heroku's free tier by not running a worker dyno.
|
92
94
|
|
95
|
+
## TODO
|
96
|
+
|
97
|
+
* Finish namespacing support by adding documentation and allowing a worker rake task to pull jobs from a custom (or all) queues.
|
98
|
+
|
93
99
|
## Contributing
|
94
100
|
|
95
101
|
1. Fork it
|
data/afterparty_test.sqlite3
CHANGED
Binary file
|
data/docs/dashboard.png
ADDED
Binary file
|
@@ -5,6 +5,7 @@ class AfterpartyJob < ::ActiveRecord::Base
|
|
5
5
|
|
6
6
|
validates_presence_of :job_dump, :execute_at, :queue
|
7
7
|
|
8
|
+
scope :namespaced, lambda { |name| where(queue: name) }
|
8
9
|
scope :incomplete, -> { where(completed: false).order("execute_at") }
|
9
10
|
scope :valid, -> { incomplete.where(execute_at: 10.years.ago..DateTime.now) }
|
10
11
|
scope :completed, -> { where(completed: true).order("execute_at desc") }
|
@@ -28,4 +29,13 @@ class AfterpartyJob < ::ActiveRecord::Base
|
|
28
29
|
j.run
|
29
30
|
end
|
30
31
|
end
|
32
|
+
|
33
|
+
# def self.namespaced name
|
34
|
+
# where(queue: name)
|
35
|
+
# end
|
36
|
+
|
37
|
+
# def namespaced name
|
38
|
+
# where(queue: name)
|
39
|
+
# end
|
40
|
+
|
31
41
|
end
|
data/lib/afterparty/queue.rb
CHANGED
@@ -3,16 +3,6 @@ module Afterparty
|
|
3
3
|
attr_accessor :options, :temp_namespace, :login_block
|
4
4
|
include Afterparty::QueueHelpers
|
5
5
|
|
6
|
-
def initialize options={}, consumer_options={}
|
7
|
-
# @consumer = ThreadedQueueConsumer.new(self, consumer_options).start
|
8
|
-
@options = options
|
9
|
-
@options[:namespace] ||= "default"
|
10
|
-
# Afterparty.add_queue @options[:namespace]
|
11
|
-
@options[:sleep] ||= 5
|
12
|
-
@mutex = Mutex.new
|
13
|
-
@options[:logger] ||= Logger.new($stderr)
|
14
|
-
end
|
15
|
-
|
16
6
|
def push job
|
17
7
|
# @mutex.synchronize do
|
18
8
|
return nil if job.nil?
|
@@ -27,8 +17,6 @@ module Afterparty
|
|
27
17
|
# @mutex.synchronize do
|
28
18
|
while true do
|
29
19
|
unless (_job = AfterpartyJob.valid.first).nil?
|
30
|
-
ap "poppin job"
|
31
|
-
_job.completed = true
|
32
20
|
_job.save
|
33
21
|
return _job
|
34
22
|
end
|
@@ -41,7 +29,7 @@ module Afterparty
|
|
41
29
|
class TestQueue < Queue
|
42
30
|
attr_accessor :completed_jobs
|
43
31
|
|
44
|
-
def initialize opts={}
|
32
|
+
def initialize opts={}
|
45
33
|
super
|
46
34
|
@completed_jobs = []
|
47
35
|
@exceptions = []
|
@@ -1,36 +1,25 @@
|
|
1
1
|
module Afterparty
|
2
2
|
module QueueHelpers
|
3
|
-
|
4
|
-
|
3
|
+
|
4
|
+
def initialize options = {}
|
5
|
+
@options = options
|
6
|
+
@options[:namespace] ||= :default
|
7
|
+
@options[:sleep] ||= 10
|
8
|
+
@options[:logger] ||= Logger.new($stderr)
|
9
|
+
self
|
5
10
|
end
|
6
11
|
|
7
|
-
def
|
8
|
-
|
9
|
-
a
|
12
|
+
def [] namespace
|
13
|
+
@temp_namespace = namespace
|
10
14
|
end
|
11
15
|
|
12
16
|
def clear
|
13
17
|
# redis_call :del
|
14
|
-
AfterpartyJob.destroy_all
|
15
|
-
end
|
16
|
-
|
17
|
-
def redis_call command, *args
|
18
|
-
result = Afterparty.redis_call (@temp_namespace || @options[:namespace]), command, *args
|
19
|
-
@temp_namespace = nil
|
20
|
-
result
|
21
|
-
end
|
22
|
-
|
23
|
-
def async_redis_call &block
|
24
|
-
Afterparty.redis.pipelined &block
|
18
|
+
AfterpartyJob.namespaced(@options[:namespace]).destroy_all
|
25
19
|
end
|
26
20
|
|
27
21
|
def jobs
|
28
|
-
|
29
|
-
# _jobs.each_with_index do |job, i|
|
30
|
-
# _jobs[i] = Marshal.load(job)
|
31
|
-
# end
|
32
|
-
# _jobs
|
33
|
-
AfterpartyJob.incomplete
|
22
|
+
AfterpartyJob.namespaced(@options[:namespace]).incomplete
|
34
23
|
end
|
35
24
|
|
36
25
|
def jobs_with_scores
|
@@ -38,41 +27,27 @@ module Afterparty
|
|
38
27
|
end
|
39
28
|
|
40
29
|
def valid_jobs
|
41
|
-
|
42
|
-
AfterpartyJob.valid
|
30
|
+
AfterpartyJob.namespaced(@options[:namespace]).valid
|
43
31
|
end
|
44
32
|
|
45
33
|
def next_valid_job
|
46
|
-
|
47
|
-
AfterpartyJob.valid.first
|
34
|
+
AfterpartyJob.namespaced(@options[:namespace]).valid.first
|
48
35
|
end
|
49
36
|
|
50
37
|
def jobs_empty?
|
51
|
-
|
52
|
-
# # ap count
|
53
|
-
# count == 0
|
54
|
-
AfterpartyJob.valid.empty?
|
38
|
+
AfterpartyJob.namespaced(@options[:namespace]).valid.empty?
|
55
39
|
end
|
56
40
|
|
57
41
|
def total_jobs_count
|
58
|
-
|
59
|
-
AfterpartyJob.incomplete.count
|
60
|
-
end
|
61
|
-
|
62
|
-
def redis
|
63
|
-
@@redis
|
42
|
+
AfterpartyJob.namespaced(@options[:namespace]).incomplete.count
|
64
43
|
end
|
65
44
|
|
66
45
|
def last_completed
|
67
|
-
|
68
|
-
# redis_call(:zrange, -1, -1).first
|
69
|
-
AfterpartyJob.completed.first
|
46
|
+
AfterpartyJob.namespaced(@options[:namespace]).completed.first
|
70
47
|
end
|
71
48
|
|
72
49
|
def completed
|
73
|
-
|
74
|
-
# redis_call(:zrange, -20, -1).reverse
|
75
|
-
AfterpartyJob.completed
|
50
|
+
AfterpartyJob.namespaced(@options[:namespace]).completed
|
76
51
|
end
|
77
52
|
|
78
53
|
def completed_with_scores
|
@@ -121,14 +96,6 @@ module Afterparty
|
|
121
96
|
|
122
97
|
private
|
123
98
|
|
124
|
-
def hash_from_scores raw
|
125
|
-
arr = []
|
126
|
-
raw.each do |group|
|
127
|
-
arr << Afterparty::JobContainer.new(group[0], group[1])
|
128
|
-
end
|
129
|
-
arr
|
130
|
-
end
|
131
|
-
|
132
99
|
# returns true if job has an :execute_at value
|
133
100
|
def job_valid? job
|
134
101
|
job.respond_to?(:execute_at) && !job.execute_at.nil?
|
data/lib/afterparty/version.rb
CHANGED
data/lib/afterparty/worker.rb
CHANGED
@@ -2,15 +2,6 @@ module Afterparty
|
|
2
2
|
class Worker
|
3
3
|
include QueueHelpers
|
4
4
|
|
5
|
-
def initialize options = {}
|
6
|
-
@options = options
|
7
|
-
@options[:adapter] ||= :redis
|
8
|
-
@options[:namespace] ||= :default
|
9
|
-
@options[:sleep] ||= 10
|
10
|
-
@options[:logger] ||= Logger.new($stderr)
|
11
|
-
self
|
12
|
-
end
|
13
|
-
|
14
5
|
def consume
|
15
6
|
@stopped = false
|
16
7
|
# puts "starting worker with namespace [#{@options[:namespace]}]."
|
data/spec/afterparty_job_spec.rb
CHANGED
@@ -10,8 +10,8 @@ describe AfterpartyJob do
|
|
10
10
|
job = AfterpartyJob.make_with_job tester
|
11
11
|
job.reload
|
12
12
|
(reloaded = job.reify).class.should == tester.class
|
13
|
-
reloaded.execute_at.should == tester.execute_at
|
14
|
-
job.execute_at.should == reloaded.execute_at
|
13
|
+
reloaded.execute_at.utc.to_i.should == tester.execute_at.utc.to_i
|
14
|
+
job.execute_at.utc.to_i.should == reloaded.execute_at.utc.to_i
|
15
15
|
end
|
16
16
|
|
17
17
|
end
|
data/spec/helpers.rb
CHANGED
@@ -1,11 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
describe Afterparty::Queue do
|
3
3
|
before do
|
4
|
-
|
5
|
-
# uri = URI.parse("redis://localhost:6379")
|
6
|
-
# redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
|
7
|
-
# Afterparty.redis = redis
|
8
|
-
@q = Afterparty::TestQueue.new({sleep: 0.5, namespace: "master_test"})
|
4
|
+
@q = Afterparty::Queue.new({sleep: 0.5})
|
9
5
|
end
|
10
6
|
|
11
7
|
after do
|
@@ -14,7 +10,6 @@ describe Afterparty::Queue do
|
|
14
10
|
|
15
11
|
before :each do
|
16
12
|
@worker = Afterparty::Worker.new({sleep: 0.5})
|
17
|
-
# @worker.consume
|
18
13
|
@q.clear
|
19
14
|
@job_time = (ENV['AFTERPARTY_JOB_TIME'] || 5).to_i
|
20
15
|
@slow_job_time = (ENV['AFTERPARTY_SLOW_TIME'] || 7).to_i
|
@@ -54,7 +49,6 @@ describe Afterparty::Queue do
|
|
54
49
|
lambda {
|
55
50
|
@worker.consume_next
|
56
51
|
}.should change{ @q.total_jobs_count }.by(0)
|
57
|
-
# @q.jobs.size.should eq(1)
|
58
52
|
end
|
59
53
|
|
60
54
|
it "waits the correct amount of time to execute a job" do
|
data/spec/queue_spec.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
describe Afterparty::Queue do
|
3
|
+
before do
|
4
|
+
@q = Afterparty::Queue.new
|
5
|
+
end
|
6
|
+
|
7
|
+
before :each do
|
8
|
+
@q.clear
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should make a wrapper job when a job is pushed" do
|
12
|
+
job = nil
|
13
|
+
tester = test_job
|
14
|
+
time = tester.execute_at = Time.now + 10.seconds
|
15
|
+
-> {
|
16
|
+
job = @q.push tester
|
17
|
+
}.should change{ AfterpartyJob.count }.by(1)
|
18
|
+
job.execute_at.utc.to_i.should == tester.execute_at.utc.to_i
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should return a wrapper job for #push" do
|
22
|
+
@q.push(test_job).class.should == AfterpartyJob
|
23
|
+
end
|
24
|
+
|
25
|
+
it "only returns valid jobs for #pop" do
|
26
|
+
tester = test_job(60)
|
27
|
+
@q.push tester
|
28
|
+
tester2 = test_job
|
29
|
+
tester2.name = "testable"
|
30
|
+
@q.push tester2
|
31
|
+
wrapper = @q.pop
|
32
|
+
popped_job = wrapper.reify
|
33
|
+
popped_job.name.should == tester2.name
|
34
|
+
popped_job.execute_at.should == nil
|
35
|
+
end
|
36
|
+
|
37
|
+
it "supports namespacing" do
|
38
|
+
queue = Afterparty::Queue.new namespace: "tester"
|
39
|
+
queue.clear
|
40
|
+
@q.clear
|
41
|
+
tester = test_job
|
42
|
+
tester.name = 'testy'
|
43
|
+
job = queue.push(tester)
|
44
|
+
job.queue.should == "tester"
|
45
|
+
wrapper = queue.pop
|
46
|
+
(inner = wrapper.reify).name.should == "testy"
|
47
|
+
wrapper.queue.should == "tester"
|
48
|
+
end
|
49
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: afterparty
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hank Stoever
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-05-
|
11
|
+
date: 2013-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -88,6 +88,7 @@ files:
|
|
88
88
|
- app/controllers/afterparty/dashboard_controller.rb
|
89
89
|
- app/views/afterparty/dashboard/index.html.haml
|
90
90
|
- config/routes.rb
|
91
|
+
- docs/dashboard.png
|
91
92
|
- dump.rdb
|
92
93
|
- lib/afterparty.rb
|
93
94
|
- lib/afterparty/afterparty_job.rb
|
@@ -109,6 +110,7 @@ files:
|
|
109
110
|
- spec/helpers.rb
|
110
111
|
- spec/queue_functional_spec.rb
|
111
112
|
- spec/queue_helpers_spec.rb
|
113
|
+
- spec/queue_spec.rb
|
112
114
|
- spec/schema.rb
|
113
115
|
- spec/spec_helper.rb
|
114
116
|
homepage: ''
|
@@ -142,6 +144,7 @@ test_files:
|
|
142
144
|
- spec/helpers.rb
|
143
145
|
- spec/queue_functional_spec.rb
|
144
146
|
- spec/queue_helpers_spec.rb
|
147
|
+
- spec/queue_spec.rb
|
145
148
|
- spec/schema.rb
|
146
149
|
- spec/spec_helper.rb
|
147
150
|
has_rdoc:
|