Module: Motor::Configs::WriteToFile

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

Constant Summary collapse

THREAD_POOL =
Concurrent::FixedThreadPool.new(1)
FILE_PATH =
Motor::Configs::FILE_PATH

Class Method Summary collapse

Class Method Details

.callObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/motor/configs/write_to_file.rb', line 11

def call
  return if Rails.env.test?
  return if THREAD_POOL.queue_length.positive?

  THREAD_POOL.post do
    ActiveRecord::Base.logger.silence do
      write_with_lock
    end
  rescue StandardError => e
    Rails.logger.error(e)
  end
end

.write_with_lockObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/motor/configs/write_to_file.rb', line 24

def write_with_lock
  File.open(Rails.root.join(FILE_PATH), 'w') do |file|
    file.flock(File::LOCK_EX)

    YAML.dump(Motor::Configs::BuildConfigsHash.call, file)

    file.flock(File::LOCK_UN)

    file
  end.close
end