activejob-lockable 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 00d85e35a9333dc6aff62522868ca06ae5b232da
4
- data.tar.gz: 2e37ff7e727e2b1e52b1bddfeb109a986db42318
2
+ SHA256:
3
+ metadata.gz: 664f6c1cc3b4fcaa153c34c1a3dd734c66a45dbbcfa751d79c23a24f2e5bcc58
4
+ data.tar.gz: e1a97f5959d30c1357d77a2fe628e63a367215c08ee1c6e3bf57b33eb1faf4cd
5
5
  SHA512:
6
- metadata.gz: b7867247ae9ee03903cb36cf9f91c0b18d892808457d16ccfc214be3189c7190d98ae841a37c8f8b2bc547760d08f6f757654ff5f5889105704dc19dec82ffa1
7
- data.tar.gz: 680c8b7980c7bc5c6651a0ca924551638a8277aa6a4e226c36cf79ce2c8641ae606d694c90b1a6f478edb5224806a16847d247ec957c8a3c3d3878159fb2fbdc
6
+ metadata.gz: 45c4f6b76f35093fbf81926898794ccac7444b8aef051b16e7b6113956f65ef02a0fae3b3d1d8282758a226de4a6a712ddae4e3c426d5e6a488ffefb4e07955f
7
+ data.tar.gz: 5343ddb76f4e63fe321604d2f73df42d98d89d030922489878db6b6e6c9af5e30d7dc1f0e5a650679e05e46b2f453e0a8ad2cbb73bc95561472efc601ba4a077
@@ -30,8 +30,7 @@ module ActiveJob
30
30
  end
31
31
  end
32
32
 
33
- # Returns the current Redis connection. If none has been created, will
34
- # create a new one.
33
+ # Returns the current Redis connection, raising an error if it hasn't been created
35
34
  def redis
36
35
  return @redis if @redis
37
36
  raise 'Redis is not configured'
@@ -17,7 +17,7 @@ module ActiveJob
17
17
  def enqueue(options = {})
18
18
  @options = options
19
19
  if locked?
20
- logger.info "job is locked, expires in #{locked_ttl} second"
20
+ logger.info "Job is locked, expires in #{locked_ttl} second(s)"
21
21
  send(on_locked_action) if on_locked_action && respond_to?(on_locked_action)
22
22
  else
23
23
  lock!
@@ -27,18 +27,25 @@ module ActiveJob
27
27
 
28
28
  def lock!
29
29
  return if lock_period.to_i <= 0
30
- logger.info "locked with #{lock_key} for #{lock_period} seconds. Job_id: #{self.job_id} class_name: #{self.class}"
30
+ logger.info "Acquiring lock #{lock_extra_info}"
31
31
  begin
32
- ActiveJob::Lockable::RedisStore.setex(lock_key, lock_period, self.job_id)
33
- rescue => e
34
- logger.info "EXCEPTION: locked with #{lock_key} for #{lock_period} seconds. Job_id: #{self.job_id} class_name: #{self.class}"
35
- raise e
32
+ # `:ex => Fixnum`: Set the specified expire time, in seconds.
33
+ # `:nx => true`: Only set the key if it does not already exist.
34
+ lock_acquired = ActiveJob::Lockable::RedisStore.set(
35
+ lock_key,
36
+ self.job_id,
37
+ { ex: lock_period.to_i, nx: true }
38
+ )
39
+ raise "Could not acquire lock #{lock_extra_info}" unless lock_acquired
40
+ rescue StandardError => e
41
+ logger.info "EXCEPTION acquiring lock #{lock_extra_info}"
42
+ raise
36
43
  end
37
44
  end
38
45
 
39
46
  def unlock!
40
47
  return unless locked?
41
- logger.info "unlocked with #{lock_key}. Job_id: #{self.job_id} class_name: #{self.class}"
48
+ logger.info "Releasing lock #{lock_extra_info}"
42
49
  ActiveJob::Lockable::RedisStore.del(lock_key)
43
50
  end
44
51
 
@@ -58,7 +65,13 @@ module ActiveJob
58
65
  private
59
66
 
60
67
  def lock_period
61
- options[:lock]
68
+ return 0 unless options
69
+
70
+ options[:lock].to_i
71
+ end
72
+
73
+ def lock_extra_info
74
+ "[key #{lock_key}] [seconds #{lock_period.to_i}] [job_id #{self.job_id}] [class_name: #{self.class}]"
62
75
  end
63
76
  end
64
77
  end
@@ -2,8 +2,8 @@ module ActiveJob
2
2
  module Lockable
3
3
  class RedisStore
4
4
  class << self
5
- def setex(cache_key, expiration, cache_value)
6
- ActiveJob::Lockable.redis.setex(cache_key, expiration, cache_value)
5
+ def set(cache_key, cache_value, options = {})
6
+ ActiveJob::Lockable.redis.set(cache_key, cache_value, options)
7
7
  end
8
8
 
9
9
  def exists?(cache_key)
@@ -1,5 +1,5 @@
1
1
  module ActiveJob
2
2
  module Lockable
3
- VERSION = '0.1.4'
3
+ VERSION = '0.1.5'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activejob-lockable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmytro Zakharov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-11 00:00:00.000000000 Z
11
+ date: 2020-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob
@@ -148,8 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  - !ruby/object:Gem::Version
149
149
  version: '0'
150
150
  requirements: []
151
- rubyforge_project:
152
- rubygems_version: 2.6.13
151
+ rubygems_version: 3.0.3
153
152
  signing_key:
154
153
  specification_version: 4
155
154
  summary: Prevents jobs from enqueuing with unique arguments for a certain period of