lib/test_ids/git.rb in test_ids-0.2.1 vs lib/test_ids/git.rb in test_ids-0.3.0

- old
+ new

@@ -16,81 +16,86 @@ unless File.exist?("#{options[:local]}/.git") FileUtils.rm_rf(options[:local]) if File.exist?(options[:local]) FileUtils.mkdir_p(options[:local]) Dir.chdir options[:local] do `git clone #{options[:remote]} .` - if !File.exist?('store.json') || !File.exist?('lock.json') + unless File.exist?('lock.json') # Should really try to use the Git driver for this - exec 'touch store.json lock.json' - exec 'git add store.json lock.json' + exec 'touch lock.json' + exec 'git add lock.json' exec 'git commit -m "Initial commit"' exec 'git push' end end end @local = options[:local] @repo = ::Git.open(options[:local]) + # Get rid of any local edits coming in here, this is only called once at the start + # of the program generation run. + # No need to pull latest as that will be done when we obtain a lock. @repo.reset_hard - @repo.pull unless options[:no_pull] end def exec(cmd) r = system(cmd) unless r fail "Something went wrong running command: #{cmd}" end end def publish - write('store.json') - release_lock - repo.commit('Publishing latest store') - repo.push('origin') + Origen.profile 'Publishing the test IDs store' do + release_lock + repo.add # Checkin everything + repo.commit('Publishing latest store') + repo.push('origin') + end end # Writes the data to the given file and pushes to the remote repo def write(path, data = nil) f = File.join(local, path) File.write(f, data) if data repo.add(f) end def get_lock - until available_to_lock? - puts "Waiting for lock, currently locked by #{lock_user} (the lock will expire in less than #{lock_minutes_remaining} #{'minute'.pluralize(lock_minutes_remaining)} if not released before that)" - sleep 5 + return if @lock_open + Origen.profile 'Obtaining test IDs lock' do + until available_to_lock? + Origen.log "Waiting for lock, currently locked by #{lock_user} (the lock will expire in less than #{lock_minutes_remaining} #{'minute'.pluralize(lock_minutes_remaining)} if not released before that)" + sleep 5 + end + data = { + 'user' => User.current.name, + 'expires' => (Time.now + minutes(5)).to_f + } + write('lock.json', JSON.pretty_generate(data)) + repo.commit('Obtaining lock') + repo.push('origin') end - data = { - 'user' => User.current.name, - 'expires' => (Time.now + minutes(5)).to_f - } - write('lock.json', JSON.pretty_generate(data)) - repo.commit('Obtaining lock') - repo.push('origin') + @lock_open = true end def release_lock data = { 'user' => nil, 'expires' => nil } write('lock.json', JSON.pretty_generate(data)) end - def with_lock - get_lock - yield - ensure - release_lock - end - def available_to_lock? - repo.pull - if lock_content && lock_user && lock_user != User.current.name - Time.now.to_f > lock_expires - else - true + result = false + Origen.profile 'Checking for lock' do + repo.pull + if lock_content && lock_user && lock_user != User.current.name + result = Time.now.to_f > lock_expires + else + result = true + end end + result end def lock_minutes_remaining ((lock_expires - Time.now.to_f) / 60).ceil end