Sha256: cd5ace4c4902c931b3363219a4b6e6778733ea77099a7537c922dc2e5f42d4ff

Contents?: true

Size: 1010 Bytes

Versions: 6

Compression:

Stored size: 1010 Bytes

Contents

require 'snapshots'

module Milkshake
  class Linker
    
    attr_reader :environment, :validator, :cache, :current_snapshot
    
    def initialize(environment, validator, cache)
      @environment = environment
      @validator   = validator
      @cache       = cache
    end
    
    def link!
      link_only_once do
        if validator.relink?
          @current_snapshot = Snapshots.dump
          
          run_migrations!
          
          validator.persist!
          cache.persist!
        end
      end
    end
    
  private
    
    def run_migrations!
      ActiveRecord::Migrator.migrate("db/migrate/", nil)
    end
    
    def link_only_once
      lock_path  = Milkshake.cache_file
      FileUtils.touch(lock_path)
      lock_file  = File.new(lock_path)
      acquired   = !!lock_file.flock(File::LOCK_EX | File::LOCK_NB)
      if acquired
        yield
      else
        lock_file.flock(File::LOCK_EX)
      end
    ensure
      lock_file.flock(File::LOCK_UN)
    end
    
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
milkshake-0.1.6 lib/milkshake/linker.rb
milkshake-0.1.5 lib/milkshake/linker.rb
milkshake-0.1.4 lib/milkshake/linker.rb
milkshake-0.1.3 lib/milkshake/linker.rb
milkshake-0.1.2 lib/milkshake/linker.rb
milkshake-0.1.1 lib/milkshake/linker.rb