require 'fileutils'
require 'timeout'

# The Sprockets::Cache::FileStore is not thread/parallel safe.
# This one uses file locks to be safe.
module SprocketsDerailleur
  class FileStore < Sprockets::Cache::FileStore
    def lock
      @lock ||= begin
        FileUtils.mkdir_p @root
        File.open(@root.join("lock"), File::RDWR|File::CREAT)
      end
    end

    # Lookup value in cache
    def [](key)
      with_lock(File::LOCK_SH) { super }
    end

    # Save value to cache
    def []=(key, value)
      with_lock(File::LOCK_EX) { super }
    end

    def with_lock(type)
      Timeout::timeout(1) { lock.flock(type) }
      yield
    ensure
      lock.flock(File::LOCK_UN)
    end
  end
end

require 'fileutils'
require 'timeout'

# The Sprockets::Cache::FileStore is not thread/parallel safe.
# This one uses file locks to be safe.
module SprocketsDerailleur
  class FileStore < Sprockets::Cache::FileStore
    def lock
      @lock ||= begin
        FileUtils.mkdir_p @root
        File.open(@root.join("lock"), File::RDWR|File::CREAT)
      end
    end

    # Lookup value in cache
    def [](key)
      with_lock(File::LOCK_SH) { super }
    end

    # Save value to cache
    def []=(key, value)
      with_lock(File::LOCK_EX) { super }
    end

    def with_lock(type)
      Timeout::timeout(10) { lock.flock(type) }
      yield
    ensure
      lock.flock(File::LOCK_UN)
    end
  end
end

Update file store timeout value
Update file store timeout value

After upgrading from rails 3 to rails 4 we hit timeout error with the file store lock. We're compiling assets on a 32core machine, so that doesn't give much time given the timeout is only 1000ms. Raising the timeout has fixed this problem for us and doesn't seem to have any ill effects.