Skip to content

Commit

Permalink
Add option to expire subjob keys
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthMax committed Mar 31, 2015
1 parent 0c8df02 commit 78d8659
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ When a superjob is queued, records for all of its subjobs are created. By defaul
Sidekiq::Superworker.options[:delete_subjobs_after_superjob_completes] = false
```

#### Expire superworkers

When a subjob dies due to too many retries depending jobs will never run and the superjob will never be completed. Therefore the sujob redis keys will never be removed.
When setting `superjob_expiration` to *x* the subjobs keys will expire in *x* seconds. Default value is `nil` (the keys will never expire).

```ruby
# config/initializers/superworker.rb
Sidekiq::Superworker.options[:superjob_expiration] = 2592000 # 1 Month
```

### Logging

To make debugging easier, Sidekiq Superworker provides detailed log messages when its logger is set to the DEBUG level:
Expand Down
3 changes: 2 additions & 1 deletion lib/sidekiq/superworker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module Sidekiq
module Superworker
DEFAULTS = {
delete_subjobs_after_superjob_completes: true,
subjob_redis_prefix: 'subjob'
subjob_redis_prefix: 'subjob',
superjob_expiration: nil
}

def self.options
Expand Down
6 changes: 4 additions & 2 deletions lib/sidekiq/superworker/subjob.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def transaction(&block)
result = nil
Sidekiq.redis do |conn|
conn.multi do
result = yield
result = yield(conn)
end
end
result
Expand Down Expand Up @@ -102,8 +102,10 @@ def initialize(params={})

def save
return false unless self.valid?
Sidekiq.redis do |conn|

self.class.transaction do |conn|
conn.mapped_hmset(key, to_param)
conn.expire(key,Superworker.options[:superjob_expiration]) if Superworker.options[:superjob_expiration]
end
true
end
Expand Down
32 changes: 32 additions & 0 deletions spec/subjob_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,38 @@ def create_subjob(custom_attributes={})
end
end

describe '#save' do
it 'creates a hashmap in redis' do
subjob = described_class.new(attributes)
subjob.save
Sidekiq.redis do |conn|
expect(conn.hkeys(subjob.key)).to eq(subjob.to_param.keys.map(&:to_s))
end
end

context 'superjob_expiration is set' do
it "sets the subjobs expiry accordingly" do
allow(Sidekiq::Superworker).to receive("options") {{subjob_redis_prefix: 'subjob', superjob_expiration: 123}}

subjob = described_class.new(attributes)
subjob.save
Sidekiq.redis do |conn|
expect(conn.ttl(subjob.key)).to be 123
end
end
end

context 'superjob_expiration is not set' do
it "sets the subjobs expiry accordingly" do
subjob = described_class.new(attributes)
subjob.save
Sidekiq.redis do |conn|
expect(conn.ttl(subjob.key)).to be -1
end
end
end
end

describe '#to_param' do
it 'returns a hash including all given attributes' do
subjob = described_class.new(attributes)
Expand Down

0 comments on commit 78d8659

Please sign in to comment.