Module: Wso2Toolbox::TokenManager::Adapters::LockFile

Included in:
LocalFileAdapter
Defined in:
lib/wso2_toolbox/token_manager/adapters/lock_file.rb

Constant Summary collapse

LockFileExistError =
Class.new(StandardError)
ATTEMPTS =
30
TIME_INTERVAL =
1
LOCK_PATH =
'tmp/localstorage.lock'

Instance Method Summary collapse

Instance Method Details

#lock_fileObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/wso2_toolbox/token_manager/adapters/lock_file.rb', line 27

def lock_file
  File.open(LOCK_PATH, 'a+') do |f|
    f.write File.basename(LOCK_PATH, '.*')
  end

  block_value = yield

  FileUtils.rm_r(LOCK_PATH, force: true)

  block_value
end

#lock_valid?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'lib/wso2_toolbox/token_manager/adapters/lock_file.rb', line 39

def lock_valid?
  return false unless File.exist?(LOCK_PATH)

  file_modified = File.ctime(LOCK_PATH)
  file_modified + 1.minutes < Time.current
end

#try_unlockObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/wso2_toolbox/token_manager/adapters/lock_file.rb', line 14

def try_unlock
  ATTEMPTS.times do |i|
    raise LockFileExistError if lock_valid? && i == (ATTEMPTS - 1)

    if lock_valid?
      sleep(TIME_INTERVAL)
    else
      FileUtils.rm_r(LOCK_PATH, force: true)
      break
    end
  end
end