Skip to content

Instantly share code, notes, and snippets.

@marzdgzmn
Created December 20, 2018 08:08
Show Gist options
  • Save marzdgzmn/e42976cfd10a45690d29e751104d0f7a to your computer and use it in GitHub Desktop.
Save marzdgzmn/e42976cfd10a45690d29e751104d0f7a to your computer and use it in GitHub Desktop.
module Foo
module Bar
LOG = Logging.app_logger
def self.run(intervals)
mirrors = retrieve_mirrors(intervals)
run_syncs(mirrors)
end
def self.run_syncs(mirrors)
thread_pool = Concurrent::FixedThreadPool.new(10, max_queue:50)
until mirrors.empty?
mirror = mirrors.shift
puts mirror.inspect
Concurrent::Future.execute(executor: thread_pool) { Sync.run(mirror) }
end
puts thread_pool.completed_task_count
thread_pool.shutdown
thread_pool.wait_for_termination
end
def self.retrieve_mirrors(intervals)
mirrors = []
intervals.each do |i|
retrieved_mirrors = Mirror.retrieve_mirrors(i)
mirrors.push(*retrieved_mirrors)
end
if mirrors.empty?
LOG.error 'No mirrors were retrieved from #{intervals}.'
exit
end
mirrors
end
at_exit do
if $ERROR_INFO
open('error.log', 'a') do |log|
error = {
timestamp: Time.now,
message: $ERROR_INFO.message,
backtrace: $ERROR_INFO.backtrace,
gems: Gem.loaded_specs.inject({}) do |m, (n, s)|
m.merge(n => s.version)
end
}
YAML.dump(error, log)
end
end
end
end
end
RSpec.describe Foo::Bar do
it "has a version number" do
expect(Rise::MirrorManager::VERSION).not_to be nil
end
describe '.run' do
it 'should run mirror syncs in different threads' do
result = OpenStruct.new(output: 'failed rsync', success?: false)
allow(Rsync).to receive(:run).and_yield(result)
thread_pool = Concurrent::FixedThreadPool.new(10, max_queue:50)
expect(Concurrent::FixedThreadPool).to receive(:new).with(10, {:max_queue=>50}).and_return(thread_pool)
puts thread_pool.completed_task_count
subject.run([4])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment