Skip to content

Commit

Permalink
Merge pull request #108 from chaps-io/update-redis-configuration
Browse files Browse the repository at this point in the history
Allow setting redis options
  • Loading branch information
krzyzak committed Feb 29, 2024
2 parents fdcae59 + a4354dc commit ac941a2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ graph TD
E --> F
F --> G[IndexJob]
G --> H{Finish}
```
```


## Defining workflows
Expand Down Expand Up @@ -412,6 +412,8 @@ In order to prevent getting the RedisMutex::LockError error when having a large
# config/initializers/gush.rb
Gush.configure do |config|
config.redis_url = "redis://localhost:6379"
# NB: you can also pass redis options as a hash:
# config.redis = { host: "localhost", port: 6379, db: 1 }
config.concurrency = 5
config.locking_duration = 2 # how long you want to wait for the lock to be released, in seconds
config.polling_interval = 0.3 # how long the polling interval should be, in seconds
Expand All @@ -426,6 +428,8 @@ Running `NotifyWorkflow.create` inserts multiple keys into Redis every time it i
# config/initializers/gush.rb
Gush.configure do |config|
config.redis_url = "redis://localhost:6379"
# NB: you can also pass redis options as a hash:
# config.redis = { host: "localhost", port: 6379, db: 1 }
config.concurrency = 5
config.ttl = 3600*24*7
end
Expand Down
10 changes: 5 additions & 5 deletions lib/gush/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class Client
@@redis_connection = Concurrent::ThreadLocalVar.new(nil)

def self.redis_connection(config)
cached = (@@redis_connection.value ||= { url: config.redis_url, connection: nil })
return cached[:connection] if !cached[:connection].nil? && config.redis_url == cached[:url]
cached = (@@redis_connection.value ||= config.redis.merge(connection: nil))
return cached[:connection] if !cached[:connection].nil? && config.redis == cached.except(:connection)

Redis.new(url: config.redis_url).tap do |instance|
Redis.new(config.redis).tap do |instance|
RedisClassy.redis = instance
@@redis_connection.value = { url: config.redis_url, connection: instance }
@@redis_connection.value = config.redis.merge(connection: instance)
end
end

Expand Down Expand Up @@ -157,7 +157,7 @@ def enqueue_job(workflow_id, job)
persist_job(workflow_id, job)
queue = job.queue || configuration.namespace
wait = job.wait

if wait.present?
Gush::Worker.set(queue: queue, wait: wait).perform_later(*[workflow_id, job.name])
else
Expand Down
15 changes: 13 additions & 2 deletions lib/gush/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
module Gush
class Configuration
attr_accessor :concurrency, :namespace, :redis_url, :ttl, :locking_duration, :polling_interval
attr_accessor :concurrency, :namespace, :redis, :ttl, :locking_duration, :polling_interval

def self.from_json(json)
new(Gush::JSON.decode(json, symbolize_keys: true))
end

def initialize(hash = {})
raise ArgumentError, "You can't set both redis_url and redis" if hash.key?(:redis_url) && hash.key?(:redis)

self.concurrency = hash.fetch(:concurrency, 5)
self.namespace = hash.fetch(:namespace, 'gush')
self.redis_url = hash.fetch(:redis_url, 'redis://localhost:6379')
self.redis = hash.key?(:redis) ? hash.fetch(:redis) : { url: hash.fetch(:redis_url, 'redis://localhost:6379') }
self.gushfile = hash.fetch(:gushfile, 'Gushfile')
self.ttl = hash.fetch(:ttl, -1)
self.locking_duration = hash.fetch(:locking_duration, 2) # how long you want to wait for the lock to be released, in seconds
Expand All @@ -24,11 +26,20 @@ def gushfile
@gushfile.realpath if @gushfile.exist?
end

def redis_url=(url)
redis[:url] = url
end

def redis_url
redis[:url]
end

def to_hash
{
concurrency: concurrency,
namespace: namespace,
redis_url: redis_url,
redis: redis,
ttl: ttl,
locking_duration: locking_duration,
polling_interval: polling_interval
Expand Down
17 changes: 16 additions & 1 deletion spec/gush/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,23 @@
config.locking_duration = 5
config.polling_interval = 0.5
end

expect(Gush.configuration.redis_url).to eq("redis://localhost")
expect(Gush.configuration.redis).to eq({ url: "redis://localhost" })
expect(Gush.configuration.concurrency).to eq(25)
expect(Gush.configuration.locking_duration).to eq(5)
expect(Gush.configuration.polling_interval).to eq(0.5)
end

it "allows setting redis opptions" do
Gush.configure do |config|
config.redis = { host: "localhost", port: 6379, db: 1 }
config.concurrency = 25
config.locking_duration = 5
config.polling_interval = 0.5
end

expect(Gush.configuration.redis).to eq({ host: "localhost", port: 6379, db: 1 })
expect(Gush.configuration.redis_url).to eq(nil)
expect(Gush.configuration.concurrency).to eq(25)
expect(Gush.configuration.locking_duration).to eq(5)
expect(Gush.configuration.polling_interval).to eq(0.5)
Expand Down

0 comments on commit ac941a2

Please sign in to comment.