# frozen_string_literal: true module Wso2Toolbox module TokenManager module Adapters module LockFile FileLockedError = Class.new(StandardError) ATTEMPTS = 5 TIME_INTERVAL = 1 FILE_PATH = 'tmp/localstorage.json' def with_free_localstorage ATTEMPTS.times do |i| locked_file = lock_file do |localstorage| yield(localstorage) end raise FileLockedError if !locked_file && i == (ATTEMPTS - 1) !locked_file ? sleep(TIME_INTERVAL) : break end end def lock_file File.open(FILE_PATH, 'a+') do |file| lock_status = file.flock(File::LOCK_EX | File::LOCK_NB) if lock_status block_value = yield(file) file.flock(File::LOCK_UN) block_value end end end end end end end