Module: Motor::Configs::SyncFromFile

Defined in:
lib/motor/configs/sync_from_file.rb

Constant Summary collapse

MUTEXT =
Mutex.new
FILE_TIMESTAMPS_STORE =
ActiveSupport::Cache::MemoryStore.new(size: 1.megabyte)

Class Method Summary collapse

Class Method Details

.call(with_exception: false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/motor/configs/sync_from_file.rb', line 11

def call(with_exception: false)
  MUTEXT.synchronize do
    file = Pathname.new(Motor::Configs.file_path)

    file_timestamp =
      begin
        file.ctime
      rescue Errno::ENOENT
        raise if with_exception

        nil
      end

    next unless file_timestamp

    FILE_TIMESTAMPS_STORE.fetch(file_timestamp.to_s) do
      Motor::Configs::SyncFromHash.call(
        YAML.safe_load(file.read, permitted_classes: [Time, Date])
      )
    end
  end
end