Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not assume redis or dalli are available #20

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ source "https://rubygems.org"

# Specify your gem's dependencies in activejob-traffic_control.gemspec
gemspec

gem 'redis'
gem 'dalli'
31 changes: 21 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@ Rate controls for your `ActiveJob`s, powered by [Suo](https://github.com/nickels

## Installation

Add this line to your application's Gemfile:
1. Add this line to your application's Gemfile:

```ruby
gem 'activejob-traffic_control'
```
```ruby
gem 'activejob-traffic_control'
```

And then execute:
2. Add `dalli` if you want to use Memcached or `redis` if you want to use Redis.

$ bundle
```ruby
# Memcached
gem 'dalli'

Or install it yourself as:
# Redis
gem 'redis'
```

$ gem install activejob-traffic_control
3. And then execute:

```bash
$ bundle
```

## Usage

Expand Down Expand Up @@ -73,8 +81,11 @@ end
For `Disable`, you also need to configure the cache client:

```ruby
ActiveJob::TrafficControl.cache_client = Rails.cache.dalli # if using :dalli_store
# or ActiveJob::TrafficControl.cache_client = ActiveSupport::Cache.lookup_store(:dalli_store, "localhost:11211")
# Memcached
ActiveJob::TrafficControl.cache_client = ActiveSupport::Cache.lookup_store(:mem_cache_store, "localhost:11211")

# Redis
ActiveJob::TrafficControl.cache_client = ActiveSupport::Cache.lookup_store(:redis_cache_store)
```

```ruby
Expand Down
4 changes: 2 additions & 2 deletions lib/active_job/traffic_control.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def client=(cli)
end

def client_class_type(client)
if client.instance_of?(Dalli::Client)
if defined?(::Dalli::Client) && client.instance_of?(::Dalli::Client)
Suo::Client::Memcached
elsif client.instance_of?(::Redis)
elsif defined?(::Redis) && client.instance_of?(::Redis)
Suo::Client::Redis
else
raise ArgumentError, "Unsupported client type: #{client}"
Expand Down
6 changes: 4 additions & 2 deletions test/active_job/traffic_control_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class MemcachedTrafficControlTest < Minitest::Test

def setup
ActiveJob::TrafficControl.client = Dalli::Client.new
ActiveJob::TrafficControl.cache_client = ActiveSupport::Cache.lookup_store(:dalli_store, "localhost:11211")
ActiveJob::TrafficControl.cache_client = ActiveSupport::Cache.lookup_store(:mem_cache_store, "localhost:11211")
setup_globals
end
end
Expand All @@ -213,7 +213,7 @@ class MemcachedPooledTrafficControlTest < Minitest::Test

def setup
ActiveJob::TrafficControl.client = ConnectionPool.new(size: 5, timeout: 5) { Dalli::Client.new }
ActiveJob::TrafficControl.cache_client = ActiveSupport::Cache.lookup_store(:dalli_store, "localhost:11211", pool_size: 5)
ActiveJob::TrafficControl.cache_client = ActiveSupport::Cache.lookup_store(:mem_cache_store, "localhost:11211", pool_size: 5)
setup_globals
end
end
Expand All @@ -223,6 +223,7 @@ class RedisTrafficControlTest < Minitest::Test

def setup
ActiveJob::TrafficControl.client = Redis.new
ActiveJob::TrafficControl.cache_client = ActiveSupport::Cache.lookup_store(:redis_cache_store)
setup_globals
end
end
Expand All @@ -232,6 +233,7 @@ class RedisPooledTrafficControlTest < Minitest::Test

def setup
ActiveJob::TrafficControl.client = ConnectionPool.new(size: 5, timeout: 5) { Redis.new }
ActiveJob::TrafficControl.cache_client = ActiveSupport::Cache.lookup_store(:redis_cache_store, pool_size: 5)
setup_globals
end
end