Module: Motor::Configs::WriteToFile

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

Constant Summary collapse

THREAD_POOL =
Concurrent::FixedThreadPool.new(1)

Class Method Summary collapse

Class Method Details

.callObject


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

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


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

def write_with_lock
  File.open(Motor::Configs.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