Skip to content

Commit

Permalink
Version 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Savage committed Jun 21, 2017
1 parent 0396b25 commit 911ec73
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 11 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.0 (2017-06-21)

- Change lock_key signature

## 0.2.0 (2017-06-20)

- Bug fixes
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class UniqueJob < ActiveJob::Base
include ActiveJob::Locking::Unique

# Make sure the lock_key is always the same
def lock_key
def lock_key(object)
self.class.name
end

def perform
def perform(object)
# do some work
end
end
Expand Down Expand Up @@ -102,28 +102,30 @@ and performed.
By default the key is defined as:

```ruby
def lock_key
def lock_key(*args)
[self.class.name, serialize_arguments(self.arguments)].join('/')
end
```
Thus it has the format `<job class name>/<serialized_job_arguments>`

The args passed to the lock key method are the same that are passed to the job's perform method.

To use this gem, you will want to override this method per job.

### Examples

Allow only one job per queue to be enqueued or performed:

```ruby
def lock_key
def lock_key(*args)
self.queue
end
```

Allow only one instance of a job class to be enqueued of performed:

```ruby
def lock_key
def lock_key(*args)
self.class.name
end
```
Expand Down
2 changes: 1 addition & 1 deletion activejob-locking.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'activejob-locking'
s.version = '0.2.0'
s.version = '0.3.0'
s.date = Time.now.strftime('%Y-%m-%d')
s.summary = 'ActiveJob locking to control how jobs are enqueued and performed.'
s.license = 'MIT'
Expand Down
8 changes: 6 additions & 2 deletions lib/activejob/locking/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def deserialize(job_data)
self.adapter.lock_token = job_data['lock_token']
end

def lock_key
def lock_key(*args)
[self.class.name, serialize_arguments(self.arguments)].join('/')
end

Expand All @@ -36,8 +36,12 @@ def adapter
# Merge local and global options
merged_options = ActiveJob::Locking.options.dup.merge(self.class.lock_options)

# Get the key
base_key = self.lock_key(*self.arguments)
key = "activejoblocking:#{base_key}"

# Remember the lock might be acquired in one process and released in another
merged_options.adapter.new(self.lock_key, merged_options)
merged_options.adapter.new(key, merged_options)
end
@adapter
end
Expand Down
2 changes: 1 addition & 1 deletion test/jobs/fail_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class FailJob < ActiveJob::Base
self.lock_acquire_time = 2

# We want the job ids to be all the same for testing
def lock_key
def lock_key(index, sleep_time)
self.class.name
end

Expand Down
2 changes: 1 addition & 1 deletion test/jobs/serial_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SerialJob < ActiveJob::Base
self.lock_acquire_time = 2

# We want the job ids to be all the same for testing
def lock_key
def lock_key(index, sleep_time)
self.class.name
end

Expand Down
2 changes: 1 addition & 1 deletion test/jobs/unique_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class UniqueJob < ActiveJob::Base
self.lock_acquire_time = 2

# We want the job ids to be all the same for testing
def lock_key
def lock_key(index, sleep_time)
self.class.name
end

Expand Down

0 comments on commit 911ec73

Please sign in to comment.