I have Sinatra service that updates a local git repo by executing git pull
when it is hit. Since it is a write, I want only one git pull
to happen at a time. How do I effectively detect if a pull
is already happening ? Currently am just checking if index.lock
file exists in .git
directory.
if @file_obj.exists? "#{@data_dir}/.git/index.lock"
@log.info('********* LOCKED **********')
else
MUTEX.synchronize do
@log.info('********* Inside Sync **********')
@executor.run(git_pull_command, in: @data_dir)
end
end
index.lock
is probably the only file that you need to worry about. Since you're using a mutex to synchronize your operations, this code should be fine.
Git only really creates the index.lock
file when it needs to have sole control over the index
for a while (and to prevent other Git processes from clobbering stuff). This would only happen while the pull is completing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.