rubykiq 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 949a8300a8a43c43d1105663f865d8cae2616743
4
- data.tar.gz: e85504b5f5d8927a3a4ce4dbb9ffd467d95f5fdf
3
+ metadata.gz: fe8a63e0620e49a3abee5a4b793f6a47de9d2134
4
+ data.tar.gz: 538763a88753b76a2d0647b0a66efba79da5db4f
5
5
  SHA512:
6
- metadata.gz: 67ecd131870891c2c92cd955e576506c45784c99caf22e93bed4ce771addacafa80de9a45535defa281c4a0570760e99fba03c16f516382a9ba15843b4d68342
7
- data.tar.gz: e980fc1dc5d22d6572a33b05787369664bc2eb3f32899992896c100e0803254aee1d514d6bef542ae3d1d184d245093823d7c5be1fd4a429f54a62df07a810dc
6
+ metadata.gz: 61192efb893a6bbe6d0d094e3c672a56ae4cea8e1e6d65dbcdc7c21762423f49fcfd6f6e54d311c7852844af4bafeee5a069342a2e9fe4188bae1109a72bedfd
7
+ data.tar.gz: 000c33af26dfd8edb0342f35265f2e85284affbd2e49338af480af75fee90db40b38abb08618981510d761136443da1d25513cb0ce8e559ec2fb391b56b9f79e
data/README.md CHANGED
@@ -16,9 +16,9 @@ require 'rubykiq'
16
16
  ## Features / Usage Examples
17
17
 
18
18
  * [Redis](http://redis.io) has support for [alternative drivers](https://github.com/redis/redis-rb#alternate-drivers), Rubykiq is tested with these in mind. (eg `:synchrony`)
19
- * the `:class` parameter can be the Class or a String of the Class (eg push jobs to Sidekiq from anywhere, not just where Sidekiq has the classes loaded)
19
+ * the `:class` parameter can be a `Class` or a `String` of a Class (eg push jobs to Sidekiq from anywhere, not just where you have Sidekiq classes loaded)
20
20
  * The `:at` parameter supports `Time`, `Date` and any `Time.parse`-able strings.
21
- * Pushing multiple and singular jobs has the same interface (simply nest args)
21
+ * Pushing multiple and singular jobs have the same interface (simply nest args)
22
22
  * Slightly less gem dependecies, and by that I mean `Sidekiq::Client` without `Celluloid` (which is already very light!)
23
23
  * Easier configuration (IMO)
24
24
 
@@ -33,30 +33,30 @@ Rubykiq.driver = :synchrony
33
33
  Rubykiq.namespace = 'background'
34
34
 
35
35
  # uses 'default' queue unless specified
36
- Rubykiq.push(:class => 'Worker', :args => ['foo', 1, :bat => 'bar'])
36
+ Rubykiq.push(class: 'Worker', args: ['foo', 1, bat: 'bar'])
37
37
 
38
38
  # args are optionally set to empty
39
- Rubykiq.push(:class => 'Scheduler', :queue => 'scheduler')
39
+ Rubykiq.push(class: 'Scheduler', queue: 'scheduler')
40
40
 
41
41
  # will batch up multiple jobs
42
- Rubykiq.push(:class => 'Worker', :args => [['foo'], ['bar']])
42
+ Rubykiq.push(class: 'Worker', args: [['foo'], ['bar']])
43
43
 
44
44
  # at param can be a 'Time', 'Date' or any 'Time.parse'-able strings
45
- Rubykiq.push(:class => 'DelayedHourMailer', :at => Time.now + 3600)
46
- Rubykiq.push(:class => 'DelayedDayMailer', :at => DateTime.now.next_day)
47
- Rubykiq.push(:class => 'DelayedMailer', :at => '2013-01-01T09:00:00Z')
45
+ Rubykiq.push(class: 'DelayedHourMailer', at: Time.now + 3600)
46
+ Rubykiq.push(class: 'DelayedDayMailer', at: DateTime.now.next_day)
47
+ Rubykiq.push(class: 'DelayedMailer', at: '2013-01-01T09:00:00Z')
48
48
 
49
49
  # alias based sugar
50
- job = { :class => 'Worker' }
50
+ job = { class: 'Worker' }
51
51
  Rubykiq << job
52
52
 
53
53
  # create multiple Rubykiq clients with their own drivers
54
54
  ruby_client = Rubykiq::Client.new
55
- hiredis_client = Rubykiq::Client.new(:driver => :hiredis)
55
+ hiredis_client = Rubykiq::Client.new(driver: :hiredis)
56
56
 
57
57
  # create multiple Rubykiq clients with their own namespaces
58
- foo_client = Rubykiq::Client.new(:namespace => 'foo')
59
- bar_client = Rubykiq::Client.new(:namespace => 'bar')
58
+ foo_client = Rubykiq::Client.new(namespace: 'foo')
59
+ bar_client = Rubykiq::Client.new(namespace: 'bar')
60
60
  ```
61
61
 
62
62
  ## Caveats
data/lib/rubykiq.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'thread'
1
2
  require 'forwardable'
2
3
  require 'rubykiq/client'
3
4
  require 'rubykiq/connection'
@@ -17,6 +18,15 @@ module Rubykiq
17
18
  #
18
19
  # @return [Rubykiq::Client]
19
20
  def self.client(options = {})
20
- @client ||= Rubykiq::Client.new(options)
21
+ initialize_client(options) unless defined?(@client)
22
+ @client
23
+ end
24
+
25
+ private
26
+
27
+ def self.initialize_client(options = {})
28
+ Thread.exclusive do
29
+ @client = Rubykiq::Client.new(options)
30
+ end
21
31
  end
22
32
  end
@@ -1,4 +1,5 @@
1
1
  require 'time'
2
+ require 'thread'
2
3
  require 'multi_json'
3
4
  require 'securerandom'
4
5
  require 'connection_pool'
@@ -48,15 +49,12 @@ module Rubykiq
48
49
  # @return [::ConnectionPool]
49
50
  def connection_pool(options = {}, &block)
50
51
  options = valid_options.merge(options)
51
-
52
- @connection_pool ||= ::ConnectionPool.new(timeout: redis_pool_timeout, size: redis_pool_size) do
53
- Rubykiq::Connection.new(options)
54
- end
52
+ initialize_connection_pool(options) unless defined?(@connection_pool)
55
53
 
56
54
  if block_given?
57
55
  @connection_pool.with(&block)
58
56
  else
59
- return @connection_pool
57
+ @connection_pool
60
58
  end
61
59
  end
62
60
 
@@ -197,5 +195,15 @@ module Rubykiq
197
195
 
198
196
  normalized_time
199
197
  end
198
+
199
+ private
200
+
201
+ def initialize_connection_pool(options = {})
202
+ Thread.exclusive do
203
+ @connection_pool = ::ConnectionPool.new(timeout: redis_pool_timeout, size: redis_pool_size) do
204
+ Rubykiq::Connection.new(options)
205
+ end
206
+ end
207
+ end
200
208
  end
201
209
  end
@@ -14,20 +14,19 @@ module Rubykiq
14
14
  url = options.delete(:url) { determine_redis_provider }
15
15
  namespace = options.delete(:namespace)
16
16
  driver = options.delete(:driver)
17
- @redis_connection ||= build_conection(url, namespace, driver)
18
- @redis_client ||= @redis_connection.client
17
+ @redis_connection = initialize_conection(url, namespace, driver)
18
+ @redis_client = @redis_connection.client
19
19
  @redis_connection
20
20
  end
21
21
 
22
22
  private
23
23
 
24
- # lets try and fallback to another redis url
25
24
  def determine_redis_provider
25
+ # lets try and fallback to another redis url
26
26
  ENV['REDISTOGO_URL'] || ENV['REDIS_PROVIDER'] || ENV['REDIS_URL'] || 'redis://localhost:6379/0'
27
27
  end
28
28
 
29
- # construct a namespaced redis connection
30
- def build_conection(url, namespace, driver)
29
+ def initialize_conection(url, namespace, driver)
31
30
  client = ::Redis.new(url: url, driver: driver)
32
31
  ::Redis::Namespace.new(namespace, redis: client)
33
32
  end
@@ -1,3 +1,3 @@
1
1
  module Rubykiq
2
- VERSION = '1.0.0' unless defined?(Rubykiq::VERSION)
2
+ VERSION = '1.0.1' unless defined?(Rubykiq::VERSION)
3
3
  end
@@ -3,7 +3,6 @@ require 'hiredis'
3
3
  require 'em-synchrony'
4
4
 
5
5
  describe Rubykiq::Client do
6
-
7
6
  before(:all) { Timecop.freeze }
8
7
  after(:all) { Timecop.return }
9
8
  let(:ruby_client) { Rubykiq::Client.new(driver: :ruby, namespace: :ruby) }
@@ -12,15 +11,11 @@ describe Rubykiq::Client do
12
11
 
13
12
  # eg with a variety of drivers
14
13
  [:ruby, :hiredis, :synchrony].each do |driver|
15
-
16
14
  # skip incompatible drivers when running in JRuby
17
15
  next if jruby? && (driver == :hiredis || :synchrony)
18
-
19
16
  context "using driver '#{driver}'" do
20
-
21
17
  # make sure the let is the current client being tested
22
18
  let(:client) { send("#{driver}_client") }
23
-
24
19
  describe :defaults do
25
20
  subject { client }
26
21
  its(:namespace) { should eq driver }
@@ -30,9 +25,7 @@ describe Rubykiq::Client do
30
25
  end
31
26
 
32
27
  describe :push do
33
-
34
28
  context :validations do
35
-
36
29
  context 'with an incorrect message type' do
37
30
  it 'raises an ArgumentError' do
38
31
  expect { client.push([]) }.to raise_error(ArgumentError, /Message must be a Hash/)
@@ -58,19 +51,14 @@ describe Rubykiq::Client do
58
51
  expect { client.push(class: Class, args: ['foo', 1, { bat: 'bar' }]) }.to raise_error(ArgumentError, /Message class must be a String representation of the class name/)
59
52
  end
60
53
  end
61
-
62
54
  end
63
55
 
64
56
  # eg singular and batch
65
57
  arguments = [[{ bat: 'bar' }], [[{ bat: 'bar' }], [{ bat: 'foo' }]]]
66
58
  arguments.each do |args|
67
-
68
59
  context "with args #{args}" do
69
-
70
60
  it "should create #{args.length} job(s)" do
71
-
72
61
  wrap_in_synchrony?(driver) do
73
-
74
62
  client.connection_pool do |connection|
75
63
  connection.flushdb
76
64
  end
@@ -84,21 +72,15 @@ describe Rubykiq::Client do
84
72
  job = MultiJson.decode(job, symbolize_keys: true)
85
73
  expect(job).to have_key(:jid)
86
74
  end
87
-
88
75
  end
89
-
90
76
  end
91
77
 
92
78
  # eg with a variety of different time types
93
79
  times = [Time.now, DateTime.now, Time.now.utc.iso8601, Time.now.to_f]
94
80
  times.each do |time|
95
-
96
81
  context "with time #{time} (#{time.class})" do
97
-
98
82
  it "should create #{args.length} job(s)" do
99
-
100
83
  wrap_in_synchrony?(driver) do
101
-
102
84
  client.connection_pool do |connection|
103
85
  connection.flushdb
104
86
  end
@@ -113,23 +95,13 @@ describe Rubykiq::Client do
113
95
  expect(job).to have_key(:at)
114
96
  expect(job[:at]).to be_within(1).of(Time.now.to_f)
115
97
  end
116
-
117
98
  end
118
-
119
99
  end
120
-
121
100
  end
122
-
123
101
  end
124
-
125
102
  end
126
-
127
103
  end
128
-
129
104
  end
130
-
131
105
  end
132
-
133
106
  end
134
-
135
107
  end
@@ -1,7 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Rubykiq::Connection do
4
-
5
4
  describe :defaults do
6
5
  subject { Rubykiq::Connection.new }
7
6
  its(:namespace) { should be_nil }
@@ -12,7 +11,6 @@ describe Rubykiq::Connection do
12
11
  end
13
12
 
14
13
  describe :options do
15
-
16
14
  context :custom do
17
15
  subject { Rubykiq::Connection.new(namespace: 'yyy') }
18
16
  its(:namespace) { should eq 'yyy' }
@@ -26,12 +24,10 @@ describe Rubykiq::Connection do
26
24
  end
27
25
  end
28
26
  end
29
-
30
27
  end
31
28
 
32
29
  describe :env do
33
30
  subject { Rubykiq::Connection.new }
34
-
35
31
  [{ name: 'REDISTOGO_URL', value: 'redistogo' }, { name: 'REDIS_PROVIDER', value: 'redisprovider' }, { name: 'REDIS_URL', value: 'redisurl' }].each do | test_case |
36
32
  context "with ENV[#{test_case[:name]}]" do
37
33
  before do
@@ -43,7 +39,5 @@ describe Rubykiq::Connection do
43
39
  its(:host) { should eq test_case[:value] }
44
40
  end
45
41
  end
46
-
47
42
  end
48
-
49
43
  end
@@ -2,30 +2,37 @@ require 'spec_helper'
2
2
 
3
3
  describe Rubykiq do
4
4
 
5
- describe :version do
6
- subject { Rubykiq::VERSION }
7
- it { should be_kind_of(String) }
8
- end
9
5
 
10
6
  describe :client do
11
- subject { Rubykiq.client }
12
- it { should be_kind_of(Rubykiq::Client) }
7
+ it 'should return a Client' do
8
+ expect(Rubykiq.client).to be_kind_of(Rubykiq::Client)
9
+ end
10
+
11
+ it 'should be thread safe' do
12
+ t1 = Thread.new { client = Rubykiq.client; sleep 0.1; client }
13
+ t2 = Thread.new { Rubykiq.client }
14
+ expect(t1.value).to eql t2.value
15
+ end
13
16
  end
14
17
 
15
18
  describe :connection_pool do
16
- subject { Rubykiq.connection_pool }
17
- it { should be_kind_of(::ConnectionPool) }
19
+ it 'should return a ConnectionPool' do
20
+ expect(Rubykiq.connection_pool).to be_kind_of(::ConnectionPool)
21
+ end
22
+ it 'should be thread safe' do
23
+ t1 = Thread.new { connection_pool = Rubykiq.connection_pool; sleep 0.1; connection_pool }
24
+ t2 = Thread.new { Rubykiq.connection_pool }
25
+ expect(t1.value).to eql t2.value
26
+ end
18
27
  end
19
28
 
20
29
  # for every valid option
21
30
  Rubykiq::Client::VALID_OPTIONS_KEYS.each do |key|
22
-
23
31
  describe key do
24
32
  subject { Rubykiq }
25
33
  it { should respond_to key }
26
34
  it { should respond_to "#{key}=".to_sym }
27
35
  end
28
-
29
36
  end
30
37
 
31
38
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubykiq
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karl Freeman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-14 00:00:00.000000000 Z
11
+ date: 2014-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis