Skip to content

Instantly share code, notes, and snippets.

@samg
Last active September 28, 2015 11:18

Revisions

  1. samg revised this gist Dec 11, 2014. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions lockfile.rb
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,13 @@
    def self.with_lock_file path_to_lock_file
    require 'fileutils'

    begin
    path_to_lock_file = '/tmp/lock.lock'
    File.new(path_to_lock_file, File::CREAT|File::EXCL) # raise if file already exits
    log "Checking for commits"
    begin
    yield
    system *ARGV
    ensure
    FileUtils.rm path_to_lock_file
    end
    rescue Errno::EEXIST
    log "Job currently running, exiting."
    warn "Job currently running, exiting."
    end
  2. samg created this gist Dec 4, 2011.
    11 changes: 11 additions & 0 deletions lockfile.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    def self.with_lock_file path_to_lock_file
    File.new(path_to_lock_file, File::CREAT|File::EXCL) # raise if file already exits
    log "Checking for commits"
    begin
    yield
    ensure
    FileUtils.rm path_to_lock_file
    end
    rescue Errno::EEXIST
    log "Job currently running, exiting."
    end