restful_metrics 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +23 -0
- data/.rspec +3 -0
- data/.rvmrc +1 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +66 -0
- data/Rakefile +22 -0
- data/lib/restful_metrics/client.rb +21 -33
- data/restful_metrics.gemspec +28 -0
- data/spec/client_spec.rb +40 -76
- data/spec/cookie_integration_spec.rb +15 -13
- data/spec/delayed/backend/mock.rb +112 -0
- data/spec/delayed/serialization/mock.rb +0 -0
- data/spec/spec_helper.rb +7 -17
- metadata +87 -177
- data/lib/delayed/backend/mock.rb +0 -25
data/.document
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create use 1.9.3-p125@restful_metrics-ruby
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
couchlogic (1.1.0)
|
5
|
+
rest-client (~> 1.6.0)
|
6
|
+
yajl-ruby (~> 0.8.1)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
abstract (1.0.0)
|
12
|
+
actionpack (3.0.11)
|
13
|
+
activemodel (= 3.0.11)
|
14
|
+
activesupport (= 3.0.11)
|
15
|
+
builder (~> 2.1.2)
|
16
|
+
erubis (~> 2.6.6)
|
17
|
+
i18n (~> 0.5.0)
|
18
|
+
rack (~> 1.2.1)
|
19
|
+
rack-mount (~> 0.6.14)
|
20
|
+
rack-test (~> 0.5.7)
|
21
|
+
tzinfo (~> 0.3.23)
|
22
|
+
activemodel (3.0.11)
|
23
|
+
activesupport (= 3.0.11)
|
24
|
+
builder (~> 2.1.2)
|
25
|
+
i18n (~> 0.5.0)
|
26
|
+
activesupport (3.0.11)
|
27
|
+
builder (2.1.2)
|
28
|
+
delayed_job (3.0.1)
|
29
|
+
activesupport (~> 3.0)
|
30
|
+
diff-lcs (1.1.3)
|
31
|
+
erubis (2.6.6)
|
32
|
+
abstract (>= 1.0.0)
|
33
|
+
i18n (0.5.0)
|
34
|
+
metaclass (0.0.1)
|
35
|
+
mime-types (1.17.2)
|
36
|
+
mocha (0.10.4)
|
37
|
+
metaclass (~> 0.0.1)
|
38
|
+
rack (1.2.5)
|
39
|
+
rack-mount (0.6.14)
|
40
|
+
rack (>= 1.0.0)
|
41
|
+
rack-test (0.5.7)
|
42
|
+
rack (>= 1.0)
|
43
|
+
rest-client (1.6.7)
|
44
|
+
mime-types (>= 1.16)
|
45
|
+
rspec (2.8.0)
|
46
|
+
rspec-core (~> 2.8.0)
|
47
|
+
rspec-expectations (~> 2.8.0)
|
48
|
+
rspec-mocks (~> 2.8.0)
|
49
|
+
rspec-core (2.8.0)
|
50
|
+
rspec-expectations (2.8.0)
|
51
|
+
diff-lcs (~> 1.1.2)
|
52
|
+
rspec-mocks (2.8.0)
|
53
|
+
tzinfo (0.3.31)
|
54
|
+
yajl-ruby (0.8.3)
|
55
|
+
|
56
|
+
PLATFORMS
|
57
|
+
ruby
|
58
|
+
|
59
|
+
DEPENDENCIES
|
60
|
+
actionpack (~> 3.0.0)
|
61
|
+
activesupport (~> 3.0.0)
|
62
|
+
couchlogic!
|
63
|
+
delayed_job (~> 3.0.1)
|
64
|
+
mocha (~> 0.10.4)
|
65
|
+
rack (~> 1.2.5)
|
66
|
+
rspec (~> 2.8.0)
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rake'
|
5
|
+
require 'rspec/core'
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
|
8
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
9
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
10
|
+
end
|
11
|
+
|
12
|
+
task :default => :spec
|
13
|
+
|
14
|
+
require 'rake/rdoctask'
|
15
|
+
Rake::RDocTask.new do |rdoc|
|
16
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
17
|
+
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = "test #{version}"
|
20
|
+
rdoc.rdoc_files.include('README*')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module RestfulMetrics
|
2
2
|
|
3
3
|
class Client
|
4
|
-
|
4
|
+
|
5
5
|
extend LogTools
|
6
6
|
|
7
7
|
@@connection = nil
|
@@ -23,27 +23,27 @@ module RestfulMetrics
|
|
23
23
|
def debug
|
24
24
|
@@debug
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
def async=(async_flag)
|
28
28
|
# DelayedJob integration
|
29
29
|
require 'delayed_job' if async_flag
|
30
|
-
|
30
|
+
|
31
31
|
@@async = async_flag && (defined?(Delayed) != nil)
|
32
32
|
@@connection.async = @@async if @@connection
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
def async?
|
36
36
|
@@async
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
def disabled=(disabled_flag)
|
40
40
|
@@disabled = disabled_flag
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
def disabled?
|
44
44
|
@@disabled
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
def add_metric(fqdn, name, value, distinct_id = nil)
|
48
48
|
params = Hash.new
|
49
49
|
params[:metric] = Hash.new
|
@@ -53,11 +53,11 @@ module RestfulMetrics
|
|
53
53
|
unless distinct_id.nil?
|
54
54
|
params[:metric][:distinct_id] = distinct_id
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
post(Endpoint.metrics, params)
|
58
58
|
end
|
59
|
-
|
60
|
-
def add_compound_metric(fqdn, name, values, distinct_id = nil)
|
59
|
+
|
60
|
+
def add_compound_metric(fqdn, name, values, distinct_id = nil)
|
61
61
|
params = Hash.new
|
62
62
|
params[:compound_metric] = Hash.new
|
63
63
|
params[:compound_metric][:fqdn] = fqdn
|
@@ -66,14 +66,18 @@ module RestfulMetrics
|
|
66
66
|
unless distinct_id.nil?
|
67
67
|
params[:compound_metric][:distinct_id] = distinct_id
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
post(Endpoint.compound_metrics, params)
|
71
71
|
end
|
72
72
|
|
73
73
|
private
|
74
|
-
|
74
|
+
|
75
75
|
def post(endpoint, data=nil)
|
76
|
-
|
76
|
+
if disabled?
|
77
|
+
logger "Skipping data points (client disabled)", :info
|
78
|
+
return false
|
79
|
+
end
|
80
|
+
|
77
81
|
if @@connection.nil?
|
78
82
|
if ENV["RESTFUL_METRICS_API_KEY"]
|
79
83
|
@@connection = Connection.new(ENV["RESTFUL_METRICS_API_KEY"])
|
@@ -81,20 +85,15 @@ module RestfulMetrics
|
|
81
85
|
raise NoConnectionEstablished
|
82
86
|
end
|
83
87
|
end
|
84
|
-
|
85
|
-
unless production_env?
|
86
|
-
logger "Skipping while not in production", :info
|
87
|
-
return false
|
88
|
-
end
|
89
|
-
|
88
|
+
|
90
89
|
if async?
|
91
|
-
self.delay.transmit
|
90
|
+
self.delay.transmit(endpoint, data)
|
92
91
|
true
|
93
92
|
else
|
94
|
-
transmit
|
93
|
+
transmit(endpoint, data)
|
95
94
|
end
|
96
95
|
end
|
97
|
-
|
96
|
+
|
98
97
|
def transmit(endpoint, data)
|
99
98
|
begin
|
100
99
|
@@connection.post(endpoint, data)
|
@@ -113,17 +112,6 @@ module RestfulMetrics
|
|
113
112
|
end
|
114
113
|
end
|
115
114
|
|
116
|
-
def production_env?
|
117
|
-
return true if defined?(RAILS_ENV) && RAILS_ENV == "production"
|
118
|
-
return true if defined?(RACK_ENV) && RACK_ENV == "production"
|
119
|
-
|
120
|
-
if defined?(Rails.env) && Rails.env == "production"
|
121
|
-
return true
|
122
|
-
end
|
123
|
-
|
124
|
-
false
|
125
|
-
end
|
126
|
-
|
127
115
|
end
|
128
116
|
|
129
117
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = %q{restful_metrics}
|
6
|
+
s.summary = %q{Ruby client for the RESTful Metrics service.}
|
7
|
+
s.description = %q{Ruby client for the RESTful Metrics service.}
|
8
|
+
s.homepage = %q{http://github.com/restful-labs/resetful_metrics-ruby}
|
9
|
+
s.version = "1.1.0"
|
10
|
+
s.authors = ["Mauricio Gomes"]
|
11
|
+
s.email = "mauricio@restful-labs.com"
|
12
|
+
|
13
|
+
s.add_dependency "yajl-ruby", "~> 0.8.1"
|
14
|
+
s.add_dependency "rest-client", "~> 1.6.0"
|
15
|
+
|
16
|
+
s.add_development_dependency "rspec", "~> 2.8.0"
|
17
|
+
s.add_development_dependency "mocha", "~> 0.10.4"
|
18
|
+
s.add_development_dependency "actionpack", "~> 3.0.0"
|
19
|
+
s.add_development_dependency "rack", "~> 1.2.5"
|
20
|
+
s.add_development_dependency "activesupport", "~> 3.0.0"
|
21
|
+
s.add_development_dependency "delayed_job", "~> 3.0.1"
|
22
|
+
|
23
|
+
s.files = `git ls-files`.split("\n")
|
24
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
26
|
+
s.require_paths = ["lib"]
|
27
|
+
end
|
28
|
+
|
data/spec/client_spec.rb
CHANGED
@@ -1,155 +1,119 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe "A NON-initialized Restful Metrics client" do
|
4
|
-
|
4
|
+
|
5
5
|
it "should NOT send a metric data point" do
|
6
|
-
lambda {
|
6
|
+
lambda {
|
7
7
|
RestfulMetrics::Client.add_metric("foo.bar.org", "hit", 1)
|
8
8
|
}.should raise_error(RestfulMetrics::NoConnectionEstablished)
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
it "should NOT send a compound metric data point" do
|
12
|
-
lambda {
|
12
|
+
lambda {
|
13
13
|
RestfulMetrics::Client.add_compound_metric("foo.bar.org", "hit", [1,2,3])
|
14
14
|
}.should raise_error(RestfulMetrics::NoConnectionEstablished)
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
describe "in a Heroku environment" do
|
18
|
-
|
18
|
+
|
19
19
|
before(:each) do
|
20
20
|
ENV["RESTFUL_METRICS_API_KEY"] = "xyz123"
|
21
21
|
RestfulMetrics::Connection.any_instance.stubs(:post).returns(100)
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
it "should send a metric data point" do
|
25
|
-
|
26
|
-
RestfulMetrics::Client.add_metric("foo.bar.org", "hit", 1).should be_true
|
27
|
-
end
|
25
|
+
RestfulMetrics::Client.add_metric("foo.bar.org", "hit", 1).should be_true
|
28
26
|
end
|
29
|
-
|
27
|
+
|
30
28
|
it "should send a compound metric data point" do
|
31
|
-
|
32
|
-
RestfulMetrics::Client.add_compound_metric("foo.bar.org", "hit", [1,2,3]).should be_true
|
33
|
-
end
|
29
|
+
RestfulMetrics::Client.add_compound_metric("foo.bar.org", "hit", [1,2,3]).should be_true
|
34
30
|
end
|
35
|
-
|
31
|
+
|
36
32
|
end
|
37
|
-
|
33
|
+
|
38
34
|
end
|
39
35
|
|
40
36
|
describe "A disabled Restful Metrics client" do
|
41
|
-
|
37
|
+
|
42
38
|
before(:each) do
|
43
39
|
RestfulMetrics::Client.disabled = true
|
44
40
|
end
|
45
|
-
|
41
|
+
|
46
42
|
it "should identify itself as disabled" do
|
47
43
|
RestfulMetrics::Client.disabled?.should == true
|
48
44
|
end
|
49
|
-
|
45
|
+
|
50
46
|
it "should NOT send a metric if the client is in disabled-mode" do
|
51
47
|
RestfulMetrics::Client.expects(:transmit).never
|
52
48
|
RestfulMetrics::Client.add_metric("foo.bar.org", "hit", 1).should == false
|
53
49
|
end
|
54
|
-
|
50
|
+
|
55
51
|
it "should NOT send a compound metric if the client is in disabled-mode" do
|
56
52
|
RestfulMetrics::Client.expects(:transmit).never
|
57
53
|
RestfulMetrics::Client.add_compound_metric("foo.bar.org", "hit", [1,2,3]).should == false
|
58
54
|
end
|
59
|
-
|
55
|
+
|
60
56
|
end
|
61
57
|
|
62
58
|
describe "An initialized Restful Metrics client" do
|
63
|
-
|
59
|
+
|
64
60
|
before(:each) do
|
65
61
|
@connection = RestfulMetrics::Connection
|
66
62
|
@connection.any_instance.stubs(:post).returns(true)
|
67
63
|
RestfulMetrics::Client.set_credentials('xyz123')
|
68
64
|
RestfulMetrics::Client.disabled = false
|
69
|
-
Delayed::Job.
|
65
|
+
Delayed::Job.delete_all
|
70
66
|
end
|
71
|
-
|
67
|
+
|
72
68
|
describe "initializing the client" do
|
73
|
-
|
69
|
+
|
74
70
|
it "should set the API key" do
|
75
71
|
RestfulMetrics::Client.set_credentials('4ed4ef44e44ed4').should be_true
|
76
72
|
end
|
77
|
-
|
73
|
+
|
78
74
|
it "should set async mode when delayed_job is loaded" do
|
79
75
|
RestfulMetrics::Client.async = true
|
80
76
|
RestfulMetrics::Client.async?.should be_true
|
81
77
|
end
|
82
|
-
|
83
|
-
end
|
84
|
-
|
85
|
-
describe "environment detection" do
|
86
|
-
|
87
|
-
it "should return false if NOT in production" do
|
88
|
-
rails_env "development" do
|
89
|
-
RestfulMetrics::Client.expects(:logger).returns('')
|
90
|
-
RestfulMetrics::Client.add_metric("foo.bar.org", "hit", 1).should be_false
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
it "should successfully send the request if in Rails production mode" do
|
95
|
-
rails_env "production" do
|
96
|
-
@connection.any_instance.stubs(:post).returns(100)
|
97
|
-
RestfulMetrics::Client.add_metric("foo.bar.org", "hit", 1).should be_true
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
it "should successfully send the request if in Rack production mode" do
|
102
|
-
RACK_ENV = "production"
|
103
|
-
@connection.any_instance.stubs(:post).returns(100)
|
104
|
-
RestfulMetrics::Client.add_metric("foo.bar.org", "hit", 1).should be_true
|
105
|
-
end
|
106
|
-
|
78
|
+
|
107
79
|
end
|
108
80
|
|
109
81
|
describe "adding metrics synchronously" do
|
110
|
-
|
82
|
+
|
111
83
|
before(:all) do
|
112
84
|
RestfulMetrics::Client.async = false
|
113
85
|
RestfulMetrics::Client.async?.should be_false
|
114
86
|
end
|
115
|
-
|
87
|
+
|
116
88
|
it "should send the metric to Restful Metrics" do
|
117
|
-
|
118
|
-
|
119
|
-
RestfulMetrics::Client.add_metric("foo.bar.org", "hit", 1).should be_true
|
120
|
-
end
|
89
|
+
@connection.any_instance.expects(:post).at_least_once.returns({})
|
90
|
+
RestfulMetrics::Client.add_metric("foo.bar.org", "hit", 1).should be_true
|
121
91
|
end
|
122
|
-
|
92
|
+
|
123
93
|
it "should send the compound metric to Restful Metrics" do
|
124
|
-
|
125
|
-
|
126
|
-
RestfulMetrics::Client.add_compound_metric("foo.bar.org", "hit", [1,2,3]).should be_true
|
127
|
-
end
|
94
|
+
@connection.any_instance.expects(:post).at_least_once.returns({})
|
95
|
+
RestfulMetrics::Client.add_compound_metric("foo.bar.org", "hit", [1,2,3]).should be_true
|
128
96
|
end
|
129
|
-
|
97
|
+
|
130
98
|
end
|
131
99
|
|
132
100
|
describe "adding metrics asynchronously" do
|
133
|
-
|
101
|
+
|
134
102
|
before(:all) do
|
135
103
|
RestfulMetrics::Client.async = true
|
136
104
|
RestfulMetrics::Client.async?.should be_true
|
137
105
|
end
|
138
|
-
|
106
|
+
|
139
107
|
it "should create a delayed job for the metric" do
|
140
|
-
|
141
|
-
|
142
|
-
Delayed::Job.count.should == 1
|
143
|
-
end
|
108
|
+
RestfulMetrics::Client.add_metric("foo.bar.org", "hit", 1).should be_true
|
109
|
+
Delayed::Job.count.should == 1
|
144
110
|
end
|
145
|
-
|
111
|
+
|
146
112
|
it "should create a delayed job for the compound metric" do
|
147
|
-
|
148
|
-
|
149
|
-
Delayed::Job.count.should == 1
|
150
|
-
end
|
113
|
+
RestfulMetrics::Client.add_compound_metric("foo.bar.org", "hit", [1,2,3]).should be_true
|
114
|
+
Delayed::Job.count.should == 1
|
151
115
|
end
|
152
|
-
|
116
|
+
|
153
117
|
end
|
154
|
-
|
118
|
+
|
155
119
|
end
|
@@ -1,19 +1,19 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe "Rails controller integration" do
|
4
|
-
|
4
|
+
|
5
5
|
class ApplicationController < ActionController::Base
|
6
|
-
|
6
|
+
|
7
7
|
before_filter :track_impression
|
8
|
-
|
8
|
+
|
9
9
|
private
|
10
|
-
|
10
|
+
|
11
11
|
def track_impression
|
12
12
|
RestfulMetrics::Client.add_metric("foo.bar.org", "impression", 1, restful_metrics_cookie)
|
13
13
|
end
|
14
14
|
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
class TestController < ApplicationController
|
18
18
|
|
19
19
|
def index
|
@@ -24,19 +24,21 @@ describe "Rails controller integration" do
|
|
24
24
|
|
25
25
|
def rescue_action(e); raise(e); end
|
26
26
|
end
|
27
|
-
|
28
|
-
|
27
|
+
|
29
28
|
before(:each) do
|
29
|
+
ActionDispatch::Request.any_instance.stubs(:cookie_jar).returns({})
|
30
30
|
RestfulMetrics::Connection.any_instance.stubs(:post).returns(true)
|
31
31
|
RestfulMetrics::Client.set_credentials('xyz123')
|
32
|
-
@request = ActionController::TestRequest.new
|
33
|
-
@request.action = 'index'
|
34
|
-
@request.path = "/index"
|
35
32
|
end
|
36
|
-
|
33
|
+
|
37
34
|
it "should track all metrics in the stack" do
|
38
35
|
RestfulMetrics::Client.expects(:add_metric).twice.returns(true)
|
39
|
-
|
36
|
+
|
37
|
+
env = Rack::MockRequest.env_for('/', :params => {'id' => '1'})
|
38
|
+
status, headers, body = TestController.action(:index).call(env)
|
39
|
+
@response = ActionDispatch::TestResponse.new(status, headers, body)
|
40
|
+
|
41
|
+
@response.code.should == "200"
|
40
42
|
end
|
41
|
-
|
43
|
+
|
42
44
|
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
# An in-memory backend suitable only for testing. Tries to behave as if it were an ORM.
|
4
|
+
module Delayed
|
5
|
+
module Backend
|
6
|
+
module Mock
|
7
|
+
class Job
|
8
|
+
attr_accessor :id
|
9
|
+
attr_accessor :priority
|
10
|
+
attr_accessor :attempts
|
11
|
+
attr_accessor :handler
|
12
|
+
attr_accessor :last_error
|
13
|
+
attr_accessor :run_at
|
14
|
+
attr_accessor :locked_at
|
15
|
+
attr_accessor :locked_by
|
16
|
+
attr_accessor :failed_at
|
17
|
+
attr_accessor :queue
|
18
|
+
|
19
|
+
include Delayed::Backend::Base
|
20
|
+
|
21
|
+
cattr_accessor :id
|
22
|
+
self.id = 0
|
23
|
+
|
24
|
+
def initialize(hash = {})
|
25
|
+
self.attempts = 0
|
26
|
+
self.priority = 0
|
27
|
+
self.id = (self.class.id += 1)
|
28
|
+
hash.each{|k,v| send(:"#{k}=", v)}
|
29
|
+
end
|
30
|
+
|
31
|
+
@jobs = []
|
32
|
+
def self.all
|
33
|
+
@jobs
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.count
|
37
|
+
all.size
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.delete_all
|
41
|
+
all.clear
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.create(attrs = {})
|
45
|
+
new(attrs).tap do |o|
|
46
|
+
o.save
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.create!(*args); create(*args); end
|
51
|
+
|
52
|
+
def self.clear_locks!(worker_name)
|
53
|
+
all.select{|j| j.locked_by == worker_name}.each {|j| j.locked_by = nil; j.locked_at = nil}
|
54
|
+
end
|
55
|
+
|
56
|
+
# Find a few candidate jobs to run (in case some immediately get locked by others).
|
57
|
+
def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time)
|
58
|
+
jobs = all.select do |j|
|
59
|
+
j.run_at <= db_time_now &&
|
60
|
+
(j.locked_at.nil? || j.locked_at < db_time_now - max_run_time || j.locked_by == worker_name) &&
|
61
|
+
j.failed_at.nil?
|
62
|
+
end
|
63
|
+
|
64
|
+
jobs = jobs.select{|j| Worker.queues.include?(j.queue)} if Worker.queues.any?
|
65
|
+
jobs = jobs.select{|j| j.priority >= Worker.min_priority} if Worker.min_priority
|
66
|
+
jobs = jobs.select{|j| j.priority <= Worker.max_priority} if Worker.max_priority
|
67
|
+
jobs.sort_by{|j| [j.priority, j.run_at]}[0..limit-1]
|
68
|
+
end
|
69
|
+
|
70
|
+
# Lock this job for this worker.
|
71
|
+
# Returns true if we have the lock, false otherwise.
|
72
|
+
def lock_exclusively!(max_run_time, worker)
|
73
|
+
now = self.class.db_time_now
|
74
|
+
if locked_by != worker
|
75
|
+
# We don't own this job so we will update the locked_by name and the locked_at
|
76
|
+
self.locked_at = now
|
77
|
+
self.locked_by = worker
|
78
|
+
end
|
79
|
+
|
80
|
+
return true
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.db_time_now
|
84
|
+
Time.current
|
85
|
+
end
|
86
|
+
|
87
|
+
def update_attributes(attrs = {})
|
88
|
+
attrs.each{|k,v| send(:"#{k}=", v)}
|
89
|
+
save
|
90
|
+
end
|
91
|
+
|
92
|
+
def destroy
|
93
|
+
self.class.all.delete(self)
|
94
|
+
end
|
95
|
+
|
96
|
+
def save
|
97
|
+
self.run_at ||= Time.current
|
98
|
+
|
99
|
+
self.class.all << self unless self.class.all.include?(self)
|
100
|
+
true
|
101
|
+
end
|
102
|
+
|
103
|
+
def save!; save; end
|
104
|
+
|
105
|
+
def reload
|
106
|
+
reset
|
107
|
+
self
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
File without changes
|
data/spec/spec_helper.rb
CHANGED
@@ -2,15 +2,21 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
2
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
3
|
|
4
4
|
require 'active_support'
|
5
|
+
require 'active_support/core_ext'
|
5
6
|
require 'active_support/test_case'
|
6
7
|
require 'action_controller'
|
7
|
-
require '
|
8
|
+
require 'action_dispatch/testing/test_process'
|
9
|
+
require 'action_dispatch/testing/test_request'
|
10
|
+
require 'action_dispatch/testing/test_response'
|
8
11
|
|
9
12
|
require 'rspec'
|
10
13
|
require 'mocha'
|
11
14
|
require 'restful_metrics'
|
15
|
+
|
12
16
|
require 'delayed_job'
|
13
17
|
|
18
|
+
include Rack::Test::Methods
|
19
|
+
|
14
20
|
# Requires supporting files with custom matchers and macros, etc,
|
15
21
|
# in ./support/ and its subdirectories.
|
16
22
|
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
@@ -19,20 +25,4 @@ RSpec.configure do |config|
|
|
19
25
|
config.mock_framework = :mocha
|
20
26
|
end
|
21
27
|
|
22
|
-
#
|
23
|
-
# Use a mock backend for testing
|
24
|
-
#
|
25
28
|
Delayed::Worker.backend = :mock
|
26
|
-
|
27
|
-
#
|
28
|
-
# Fake the rails environment for testing
|
29
|
-
#
|
30
|
-
class Rails; cattr_accessor :env; end
|
31
|
-
|
32
|
-
# Simulate Rails enviornments
|
33
|
-
def rails_env(env, &block)
|
34
|
-
old_env = Rails.env
|
35
|
-
Rails.env = env
|
36
|
-
yield
|
37
|
-
Rails.env = old_env
|
38
|
-
end
|
metadata
CHANGED
@@ -1,251 +1,161 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: restful_metrics
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
4
5
|
prerelease:
|
5
|
-
version: 1.0.1
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Mauricio Gomes
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
dependencies:
|
16
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-02-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
17
15
|
name: yajl-ruby
|
18
|
-
requirement: &
|
16
|
+
requirement: &70354861209440 !ruby/object:Gem::Requirement
|
19
17
|
none: false
|
20
|
-
requirements:
|
18
|
+
requirements:
|
21
19
|
- - ~>
|
22
|
-
- !ruby/object:Gem::Version
|
20
|
+
- !ruby/object:Gem::Version
|
23
21
|
version: 0.8.1
|
24
22
|
type: :runtime
|
25
23
|
prerelease: false
|
26
|
-
version_requirements: *
|
27
|
-
- !ruby/object:Gem::Dependency
|
24
|
+
version_requirements: *70354861209440
|
25
|
+
- !ruby/object:Gem::Dependency
|
28
26
|
name: rest-client
|
29
|
-
requirement: &
|
27
|
+
requirement: &70354861208320 !ruby/object:Gem::Requirement
|
30
28
|
none: false
|
31
|
-
requirements:
|
29
|
+
requirements:
|
32
30
|
- - ~>
|
33
|
-
- !ruby/object:Gem::Version
|
31
|
+
- !ruby/object:Gem::Version
|
34
32
|
version: 1.6.0
|
35
33
|
type: :runtime
|
36
34
|
prerelease: false
|
37
|
-
version_requirements: *
|
38
|
-
- !ruby/object:Gem::Dependency
|
39
|
-
name: jeweler
|
40
|
-
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
|
-
requirements:
|
43
|
-
- - ">="
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version: "0"
|
46
|
-
type: :development
|
47
|
-
prerelease: false
|
48
|
-
version_requirements: *id003
|
49
|
-
- !ruby/object:Gem::Dependency
|
50
|
-
name: i18n
|
51
|
-
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
-
none: false
|
53
|
-
requirements:
|
54
|
-
- - ">="
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
version: "0"
|
57
|
-
type: :development
|
58
|
-
prerelease: false
|
59
|
-
version_requirements: *id004
|
60
|
-
- !ruby/object:Gem::Dependency
|
61
|
-
name: actionpack
|
62
|
-
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
-
none: false
|
64
|
-
requirements:
|
65
|
-
- - ~>
|
66
|
-
- !ruby/object:Gem::Version
|
67
|
-
version: 2.3.9
|
68
|
-
type: :development
|
69
|
-
prerelease: false
|
70
|
-
version_requirements: *id005
|
71
|
-
- !ruby/object:Gem::Dependency
|
72
|
-
name: activesupport
|
73
|
-
requirement: &id006 !ruby/object:Gem::Requirement
|
74
|
-
none: false
|
75
|
-
requirements:
|
76
|
-
- - ~>
|
77
|
-
- !ruby/object:Gem::Version
|
78
|
-
version: 2.3.9
|
79
|
-
type: :development
|
80
|
-
prerelease: false
|
81
|
-
version_requirements: *id006
|
82
|
-
- !ruby/object:Gem::Dependency
|
83
|
-
name: delayed_job
|
84
|
-
requirement: &id007 !ruby/object:Gem::Requirement
|
85
|
-
none: false
|
86
|
-
requirements:
|
87
|
-
- - ~>
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: 2.0.0
|
90
|
-
type: :development
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: *id007
|
93
|
-
- !ruby/object:Gem::Dependency
|
35
|
+
version_requirements: *70354861208320
|
36
|
+
- !ruby/object:Gem::Dependency
|
94
37
|
name: rspec
|
95
|
-
requirement: &
|
38
|
+
requirement: &70354861207620 !ruby/object:Gem::Requirement
|
96
39
|
none: false
|
97
|
-
requirements:
|
40
|
+
requirements:
|
98
41
|
- - ~>
|
99
|
-
- !ruby/object:Gem::Version
|
100
|
-
version: 2.
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.8.0
|
101
44
|
type: :development
|
102
45
|
prerelease: false
|
103
|
-
version_requirements: *
|
104
|
-
- !ruby/object:Gem::Dependency
|
46
|
+
version_requirements: *70354861207620
|
47
|
+
- !ruby/object:Gem::Dependency
|
105
48
|
name: mocha
|
106
|
-
requirement: &
|
107
|
-
none: false
|
108
|
-
requirements:
|
109
|
-
- - ">="
|
110
|
-
- !ruby/object:Gem::Version
|
111
|
-
version: "0"
|
112
|
-
type: :development
|
113
|
-
prerelease: false
|
114
|
-
version_requirements: *id009
|
115
|
-
- !ruby/object:Gem::Dependency
|
116
|
-
name: yajl-ruby
|
117
|
-
requirement: &id010 !ruby/object:Gem::Requirement
|
49
|
+
requirement: &70354861206880 !ruby/object:Gem::Requirement
|
118
50
|
none: false
|
119
|
-
requirements:
|
51
|
+
requirements:
|
120
52
|
- - ~>
|
121
|
-
- !ruby/object:Gem::Version
|
122
|
-
version: 0.
|
123
|
-
type: :
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.10.4
|
55
|
+
type: :development
|
124
56
|
prerelease: false
|
125
|
-
version_requirements: *
|
126
|
-
- !ruby/object:Gem::Dependency
|
127
|
-
name:
|
128
|
-
requirement: &
|
57
|
+
version_requirements: *70354861206880
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: actionpack
|
60
|
+
requirement: &70354861206140 !ruby/object:Gem::Requirement
|
129
61
|
none: false
|
130
|
-
requirements:
|
62
|
+
requirements:
|
131
63
|
- - ~>
|
132
|
-
- !ruby/object:Gem::Version
|
133
|
-
version:
|
134
|
-
type: :runtime
|
135
|
-
prerelease: false
|
136
|
-
version_requirements: *id011
|
137
|
-
- !ruby/object:Gem::Dependency
|
138
|
-
name: rack
|
139
|
-
requirement: &id012 !ruby/object:Gem::Requirement
|
140
|
-
none: false
|
141
|
-
requirements:
|
142
|
-
- - ">"
|
143
|
-
- !ruby/object:Gem::Version
|
144
|
-
version: 1.0.0
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 3.0.0
|
145
66
|
type: :development
|
146
67
|
prerelease: false
|
147
|
-
version_requirements: *
|
148
|
-
- !ruby/object:Gem::Dependency
|
149
|
-
name:
|
150
|
-
requirement: &
|
68
|
+
version_requirements: *70354861206140
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rack
|
71
|
+
requirement: &70354861205400 !ruby/object:Gem::Requirement
|
151
72
|
none: false
|
152
|
-
requirements:
|
153
|
-
- -
|
154
|
-
- !ruby/object:Gem::Version
|
155
|
-
version:
|
73
|
+
requirements:
|
74
|
+
- - ~>
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 1.2.5
|
156
77
|
type: :development
|
157
78
|
prerelease: false
|
158
|
-
version_requirements: *
|
159
|
-
- !ruby/object:Gem::Dependency
|
79
|
+
version_requirements: *70354861205400
|
80
|
+
- !ruby/object:Gem::Dependency
|
160
81
|
name: activesupport
|
161
|
-
requirement: &
|
82
|
+
requirement: &70354861204600 !ruby/object:Gem::Requirement
|
162
83
|
none: false
|
163
|
-
requirements:
|
164
|
-
- -
|
165
|
-
- !ruby/object:Gem::Version
|
166
|
-
version:
|
84
|
+
requirements:
|
85
|
+
- - ~>
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: 3.0.0
|
167
88
|
type: :development
|
168
89
|
prerelease: false
|
169
|
-
version_requirements: *
|
170
|
-
- !ruby/object:Gem::Dependency
|
90
|
+
version_requirements: *70354861204600
|
91
|
+
- !ruby/object:Gem::Dependency
|
171
92
|
name: delayed_job
|
172
|
-
requirement: &
|
93
|
+
requirement: &70354861203940 !ruby/object:Gem::Requirement
|
173
94
|
none: false
|
174
|
-
requirements:
|
175
|
-
- -
|
176
|
-
- !ruby/object:Gem::Version
|
177
|
-
version:
|
178
|
-
type: :development
|
179
|
-
prerelease: false
|
180
|
-
version_requirements: *id015
|
181
|
-
- !ruby/object:Gem::Dependency
|
182
|
-
name: rspec
|
183
|
-
requirement: &id016 !ruby/object:Gem::Requirement
|
184
|
-
none: false
|
185
|
-
requirements:
|
186
|
-
- - <
|
187
|
-
- !ruby/object:Gem::Version
|
188
|
-
version: 2.0.0
|
95
|
+
requirements:
|
96
|
+
- - ~>
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: 3.0.1
|
189
99
|
type: :development
|
190
100
|
prerelease: false
|
191
|
-
version_requirements: *
|
101
|
+
version_requirements: *70354861203940
|
192
102
|
description: Ruby client for the RESTful Metrics service.
|
193
103
|
email: mauricio@restful-labs.com
|
194
104
|
executables: []
|
195
|
-
|
196
105
|
extensions: []
|
197
|
-
|
198
|
-
|
199
|
-
-
|
200
|
-
-
|
201
|
-
|
106
|
+
extra_rdoc_files: []
|
107
|
+
files:
|
108
|
+
- .document
|
109
|
+
- .gitignore
|
110
|
+
- .rspec
|
111
|
+
- .rvmrc
|
112
|
+
- Gemfile
|
113
|
+
- Gemfile.lock
|
202
114
|
- LICENSE
|
203
115
|
- README.md
|
116
|
+
- Rakefile
|
204
117
|
- VERSION
|
205
|
-
- lib/delayed/backend/mock.rb
|
206
118
|
- lib/restful_metrics.rb
|
207
119
|
- lib/restful_metrics/client.rb
|
208
120
|
- lib/restful_metrics/connection.rb
|
209
121
|
- lib/restful_metrics/endpoint.rb
|
210
122
|
- lib/restful_metrics/log_tools.rb
|
211
123
|
- lib/restful_metrics/railtie/cookie_integration.rb
|
124
|
+
- restful_metrics.gemspec
|
212
125
|
- spec/client_spec.rb
|
213
126
|
- spec/cookie_integration_spec.rb
|
127
|
+
- spec/delayed/backend/mock.rb
|
128
|
+
- spec/delayed/serialization/mock.rb
|
214
129
|
- spec/endpoint_spec.rb
|
215
130
|
- spec/spec_helper.rb
|
216
|
-
|
217
|
-
homepage: http://restfulmetrics.com
|
131
|
+
homepage: http://github.com/restful-labs/resetful_metrics-ruby
|
218
132
|
licenses: []
|
219
|
-
|
220
133
|
post_install_message:
|
221
134
|
rdoc_options: []
|
222
|
-
|
223
|
-
require_paths:
|
135
|
+
require_paths:
|
224
136
|
- lib
|
225
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
137
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
226
138
|
none: false
|
227
|
-
requirements:
|
228
|
-
- -
|
229
|
-
- !ruby/object:Gem::Version
|
230
|
-
|
231
|
-
|
232
|
-
- 0
|
233
|
-
version: "0"
|
234
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - ! '>='
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
235
144
|
none: false
|
236
|
-
requirements:
|
237
|
-
- -
|
238
|
-
- !ruby/object:Gem::Version
|
239
|
-
version:
|
145
|
+
requirements:
|
146
|
+
- - ! '>='
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
240
149
|
requirements: []
|
241
|
-
|
242
150
|
rubyforge_project:
|
243
|
-
rubygems_version: 1.
|
151
|
+
rubygems_version: 1.8.16
|
244
152
|
signing_key:
|
245
153
|
specification_version: 3
|
246
|
-
summary: Ruby client for RESTful Metrics
|
247
|
-
test_files:
|
154
|
+
summary: Ruby client for the RESTful Metrics service.
|
155
|
+
test_files:
|
248
156
|
- spec/client_spec.rb
|
249
157
|
- spec/cookie_integration_spec.rb
|
158
|
+
- spec/delayed/backend/mock.rb
|
159
|
+
- spec/delayed/serialization/mock.rb
|
250
160
|
- spec/endpoint_spec.rb
|
251
161
|
- spec/spec_helper.rb
|
data/lib/delayed/backend/mock.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
# Used by Delayed::Job during testing
|
2
|
-
|
3
|
-
module Delayed
|
4
|
-
module Backend
|
5
|
-
module Mock
|
6
|
-
class Job
|
7
|
-
include Delayed::Backend::Base
|
8
|
-
|
9
|
-
@@job_count = 0
|
10
|
-
|
11
|
-
def self.create(params)
|
12
|
-
@@job_count += 1
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.count
|
16
|
-
@@job_count
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.reset
|
20
|
-
@@job_count = 0
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|