Module: RunLoop::DotDir
- Defined in:
- lib/run_loop/dot_dir.rb
Overview
A module for managing the ~/.run-loop directory.
Class Method Summary collapse
- .directory ⇒ Object
- .make_results_dir ⇒ Object
- .next_timestamped_dirname(base_dir) ⇒ Object
- .rotate_result_directories ⇒ Object
- .timestamped_dirname(plus_seconds = 0) ⇒ Object
Class Method Details
.directory ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/run_loop/dot_dir.rb', line 4 def self.directory home = RunLoop::Environment.user_home_directory dir = File.join(home, ".run-loop") if !File.exist?(dir) FileUtils.mkdir_p(dir) end dir end |
.make_results_dir ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/run_loop/dot_dir.rb', line 13 def self.make_results_dir if RunLoop::Environment.xtc? next_results_dir = Dir.mktmpdir("run_loop") else results_dir = File.join(self.directory, 'results') next_results_dir = self.(results_dir) FileUtils.mkdir_p(next_results_dir) end next_results_dir end |
.next_timestamped_dirname(base_dir) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/run_loop/dot_dir.rb', line 62 def self.(base_dir) dir = File.join(base_dir, self.) return dir if !File.exist?(dir) # Rather than wait, just increment the second. Per-second accuracy # is not important; uniqueness is. counter = 0 loop do break if !File.exist?(dir) break if counter == 4 counter = counter + 1 dir = File.join(base_dir, self.(counter)) end # If all else fails, just return a unique UUID if File.exist?(dir) dir = File.join(base_dir, SecureRandom.uuid) end dir end |
.rotate_result_directories ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/run_loop/dot_dir.rb', line 25 def self.rotate_result_directories return :xtc if RunLoop::Environment.xtc? start = Time.now glob = "#{self.directory}/results/*" RunLoop.log_debug("Searching for run-loop results with glob: #{glob}") directories = Dir.glob(glob).select do |path| File.directory?(path) end oldest_first = directories.sort_by { |f| File.mtime(f) } RunLoop.log_debug("Found #{oldest_first.count} previous run-loop results") oldest_first.pop(5) RunLoop.log_debug("Will delete #{oldest_first.count} previous run-loop results") oldest_first.each do |path| FileUtils.rm_rf(path) end elapsed = Time.now - start RunLoop.log_debug("Deleted #{oldest_first.count} previous results in #{elapsed} seconds") rescue StandardError => e RunLoop.log_error("While rotating previous results, encounterd: #{e}") end |
.timestamped_dirname(plus_seconds = 0) ⇒ Object
58 59 60 |
# File 'lib/run_loop/dot_dir.rb', line 58 def self.(plus_seconds = 0) (Time.now + plus_seconds).strftime("%Y-%m-%d_%H-%M-%S") end |