wso2_toolbox 0.3.2 → 0.3.3

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
2
  SHA1:
3
- metadata.gz: 830a70b3c1e859b9daab9918aa7a3bbf932ffc62
4
- data.tar.gz: af1c09c9c035f9a99c2693230d5adf27759d080e
3
+ metadata.gz: 0f5ee367ecc8489e4e5595cc5837afb758d6fdff
4
+ data.tar.gz: 4fe61ef409d2626789594fec52fd4888a5967933
5
5
  SHA512:
6
- metadata.gz: c9dc6dbeca2ab24b60bc507d853df448204f0639b408dea5f8b04881fe6ac79590f80d6a084969012618a29b0925a41ad4bdf384242c5aec22b188f38ca329f8
7
- data.tar.gz: 68c0556025fa54f9c1908a1057c2ffd29de213db1904b2a254c1af0d6a1f16cfaaec0368346f9caf5bf553d1ffa66b3108d18be5de4ff9adb18a2bd7cf7d1592
6
+ metadata.gz: 00be8156cd3a5b4751887890ec3114946569a628d93e5463cc5f8971576791ca7cbce2782aac4c1b3b2c286c326b742ce422b8c29ff95f90436d38feedf8bbf1
7
+ data.tar.gz: 1a76a88c5ab5c025cce86bffa99026b09747a9a7d42b4d9873eaaaea45a49e8ff6b9dc003c12fefdfa13b6ea440376444a40639618e4dedadeee1f6dd0ab539a
@@ -6,7 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
- ## [0.3.2] - 2018-11-07
9
+ ## [0.3.3] - 2018-11-14
10
+ ### Changed
11
+ - Fix bug in local file adapter, refactoring lock file with core language
12
+
13
+ ## [0.3.2] - 2018-11-12
10
14
  ### Changed
11
15
  - Fix bug in local file adapter, when 2 or more process try to read same file
12
16
 
@@ -12,7 +12,6 @@ module Wso2Toolbox
12
12
  class << self
13
13
  include LockFile
14
14
 
15
- FILE_PATH = 'tmp/localstorage.json'
16
15
  TOKEN_DELAY = 5.minutes
17
16
 
18
17
  def generate_token
@@ -21,18 +20,16 @@ module Wso2Toolbox
21
20
  end
22
21
 
23
22
  def read(key)
24
- try_unlock
25
-
26
- lock_file do
27
- local_storage { |store| return store.try(:[], key.to_s) }
23
+ with_free_localstorage do |file|
24
+ local_storage(file.read) do |store|
25
+ return store.try(:[], key.to_s)
26
+ end
28
27
  end
29
28
  end
30
29
 
31
30
  def write(key, value)
32
- try_unlock
33
-
34
- lock_file do
35
- local_storage { |store| store.merge!(key => value) }
31
+ with_free_localstorage do |file|
32
+ local_storage(file.read) { |store| store.merge!(key => value) }
36
33
  end
37
34
  end
38
35
 
@@ -58,8 +55,7 @@ module Wso2Toolbox
58
55
  Time.parse(read(:token_time_for_job)) - TOKEN_DELAY > Time.now
59
56
  end
60
57
 
61
- def local_storage
62
- file = File.open(FILE_PATH, 'a+').read
58
+ def local_storage(file)
63
59
  parsed_stored = file == '' ? {} : JSON.parse(file)
64
60
 
65
61
  yield(parsed_stored)
@@ -1,46 +1,38 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'fileutils'
4
-
5
3
  module Wso2Toolbox
6
4
  module TokenManager
7
5
  module Adapters
8
6
  module LockFile
9
- LockFileExistError = Class.new(StandardError)
10
- ATTEMPTS = 30
7
+ FileLockedError = Class.new(StandardError)
8
+
9
+ ATTEMPTS = 5
11
10
  TIME_INTERVAL = 1
12
- LOCK_PATH = 'tmp/localstorage.lock'
11
+ FILE_PATH = 'tmp/localstorage.json'
13
12
 
14
- def try_unlock
13
+ def with_free_localstorage
15
14
  ATTEMPTS.times do |i|
16
- raise LockFileExistError if lock_valid? && i == (ATTEMPTS - 1)
17
-
18
- if lock_valid?
19
- sleep(TIME_INTERVAL)
20
- else
21
- FileUtils.rm_r(LOCK_PATH, force: true)
22
- break
15
+ locked_file = lock_file do |localstorage|
16
+ yield(localstorage)
23
17
  end
18
+
19
+ raise FileLockedError if !locked_file && i == (ATTEMPTS - 1)
20
+
21
+ !locked_file ? sleep(TIME_INTERVAL) : break
24
22
  end
25
23
  end
26
24
 
27
25
  def lock_file
28
- File.open(LOCK_PATH, 'a+') do |f|
29
- f.write File.basename(LOCK_PATH, '.*')
30
- end
26
+ File.open(FILE_PATH, 'a+') do |file|
27
+ lock_status = file.flock(File::LOCK_EX | File::LOCK_NB)
31
28
 
32
- block_value = yield
29
+ if lock_status
30
+ block_value = yield(file)
33
31
 
34
- FileUtils.rm_r(LOCK_PATH, force: true)
35
-
36
- block_value
37
- end
38
-
39
- def lock_valid?
40
- return false unless File.exist?(LOCK_PATH)
41
-
42
- file_modified = File.ctime(LOCK_PATH)
43
- file_modified + 1.minutes < Time.current
32
+ file.flock(File::LOCK_UN)
33
+ block_value
34
+ end
35
+ end
44
36
  end
45
37
  end
46
38
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wso2Toolbox
4
- VERSION = '0.3.2'
4
+ VERSION = '0.3.3'
5
5
  end
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_dependency 'request_store', '~> 1.3.2'
24
24
  spec.add_dependency 'rest-client', '>= 2.0.2'
25
25
 
26
- spec.add_development_dependency 'bundler', '~> 1.16.1'
26
+ spec.add_development_dependency 'bundler', '~> 1.17.1'
27
27
  spec.add_development_dependency 'dotenv'
28
28
  spec.add_development_dependency 'pry-byebug'
29
29
  spec.add_development_dependency 'rails-controller-testing', '~> 1.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wso2_toolbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abner Carleto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-12 00:00:00.000000000 Z
11
+ date: 2018-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 1.16.1
75
+ version: 1.17.1
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 1.16.1
82
+ version: 1.17.1
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: dotenv
85
85
  requirement: !ruby/object:Gem::Requirement