Skip to content

Commit

Permalink
Merge pull request #57 from dks17/utc_time
Browse files Browse the repository at this point in the history
Replace time by UTC
  • Loading branch information
afeld committed Aug 25, 2018
2 parents ecc0ab7 + b5729ba commit 673a871
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
### 1.0.0 (Next)

* Your contribution here.
* [#57](https://github.com/mongoid/mongoid-locker/pull/57): `Time.now` replaced by `Time.now.utc` - [@dks17](https://github.com/dks17).
* [#55](https://github.com/mongoid/mongoid-locker/pull/55): Customizable :locked_at and :locked_until fields - [@dks17](https://github.com/dks17).

### 0.3.6 (4/18/2018)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -40,7 +40,7 @@ queue_item.with_lock do

# do stuff

queue_item.completed_at = Time.now
queue_item.completed_at = Time.now.utc
queue_item.save!
end
```
Expand Down
10 changes: 5 additions & 5 deletions lib/mongoid/locker.rb
Expand Up @@ -15,14 +15,14 @@ module ClassMethods
#
# @return [Mongoid::Criteria]
def locked
where locked_until_field.gt => Time.now
where locked_until_field.gt => Time.now.utc
end

# A scope to retrieve all unlocked documents in the collection.
#
# @return [Mongoid::Criteria]
def unlocked
any_of({ locked_until_field => nil }, locked_until_field.lte => Time.now)
any_of({ locked_until_field => nil }, locked_until_field.lte => Time.now.utc)
end

# Set the default lock timeout for this class. Note this only applies to new locks. Defaults to five seconds.
Expand Down Expand Up @@ -87,7 +87,7 @@ def configure
#
# @return [Boolean] true if locked, false otherwise
def locked?
!!(self[locked_until_field] && self[locked_until_field] > Time.now)
!!(self[locked_until_field] && self[locked_until_field] > Time.now.utc)
end

# Returns whether the current instance has the lock or not.
Expand Down Expand Up @@ -124,7 +124,7 @@ def with_lock(opts = {})
protected

def acquire_lock(opts = {})
time = Time.now
time = Time.now.utc
timeout = opts[:timeout] || self.class.lock_timeout
expiration = time + timeout

Expand Down Expand Up @@ -174,7 +174,7 @@ def lock(opts = {})
locked_until = Mongoid::Locker::Wrapper.locked_until(self)
# the lock might be released since the last check so make another attempt
next unless locked_until
retry_sleep = locked_until - Time.now
retry_sleep = locked_until - Time.now.utc
end

sleep retry_sleep if retry_sleep > 0
Expand Down
6 changes: 3 additions & 3 deletions spec/mongoid-locker_spec.rb
Expand Up @@ -154,7 +154,7 @@ def remove_class(klass)

it 'should retry the number of times given, if desired' do
allow(@user).to receive(:acquire_lock).and_return(false)
allow(Mongoid::Locker::Wrapper).to receive(:locked_until).and_return(Time.now)
allow(Mongoid::Locker::Wrapper).to receive(:locked_until).and_return(Time.now.utc)

expect(@user).to receive(:acquire_lock).exactly(6).times
expect do
Expand All @@ -178,7 +178,7 @@ def remove_class(klass)

it 'should, by default, when retrying, sleep until the lock expires' do
allow(@user).to receive(:acquire_lock).and_return(false)
allow(Mongoid::Locker::Wrapper).to receive(:locked_until).and_return(Time.now + 5.seconds)
allow(Mongoid::Locker::Wrapper).to receive(:locked_until).and_return(Time.now.utc + 5.seconds)
allow(@user).to receive(:sleep) { |time| expect(time).to be_within(0.1).of(5) }

expect do
Expand All @@ -202,7 +202,7 @@ def remove_class(klass)
it 'should override the default timeout' do
User.timeout_lock_after 1

expiration = (Time.now + 3).to_i
expiration = (Time.now.utc + 3).to_i
@user.with_lock timeout: 3 do
expect(@user[@user.locked_until_field].to_i).to eq(expiration)
end
Expand Down

0 comments on commit 673a871

Please sign in to comment.