Module: Bootscale::Utils

Defined in:
lib/bootscale/utils.rb

Class Method Summary collapse

Class Method Details

.atomic_write(filename) ⇒ Object

Write to a file atomically. Useful for situations where you don’t want other processes or threads to see half-written files.

Utils.atomic_write('important.file') do |file|
  file.write('hello')
end

Returns nothing.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bootscale/utils.rb', line 11

def self.atomic_write(filename)
  dirname, basename = File.split(filename)
  basename = [
      basename,
      Thread.current.object_id,
      Process.pid,
      rand(1000000)
  ].join('.'.freeze)
  tmpname = File.join(dirname, basename)

  File.open(tmpname, 'wb+') do |f|
    yield f
  end

  File.rename(tmpname, filename)
ensure
  File.delete(tmpname) if File.exist?(tmpname)
end