Skip to content

Instantly share code, notes, and snippets.

@nrk
Created March 27, 2012 15:06
Show Gist options
  • Save nrk/2216699 to your computer and use it in GitHub Desktop.
Save nrk/2216699 to your computer and use it in GitHub Desktop.
Single instance check with ruby and windows
require 'win32/mutex'
require 'windows/error'
def with_global_lock(lockname, &block)
mutex = Win32::Mutex.new(true, lockname)
if mutex.GetLastError == Windows::Error::ERROR_ALREADY_EXISTS
raise RuntimeError, 'Already running!'
end
begin
block.call
ensure
mutex.release
mutex.close
end
end
with_global_lock('my_unique_lock_name') do
10.times do |i|
puts i
sleep 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment