Skip to content

Instantly share code, notes, and snippets.

@apeiros
Created April 25, 2018 09:44
Show Gist options
  • Save apeiros/d38f157c19b09a878e0a360b7c0f2cfd to your computer and use it in GitHub Desktop.
Save apeiros/d38f157c19b09a878e0a360b7c0f2cfd to your computer and use it in GitHub Desktop.
require 'pry'
require 'concurrent'
require_relative '../lib/mirror_notification'
require_relative '../lib/mirror_observer'
require_relative '../lib/mirror_sync'
require_relative '../lib/mirror'
class Sync
include Mirror
include MirrorNotification
include MirrorSync
def self.run
Sync.new.run
end
def initialize
@mirrors = Mirror::IniFileLoader.retrieve_upstream_mirror_configuration # retrieve configuration for each mirror
@thread_pool = Concurrent::FixedThreadPool.new(10, max_queue: 20) # from the concurrent-ruby gem. tried implementing my own thread pool, but got too time consuming
@run_time = Time.now # will be used for generating report indicating what time this script was run
end
def run
process_jobs
puts 'after process_jobs'
wait_for_jobs_to_finish
puts 'jobs finish'
#generate_report
end
private
def process_jobs
until @mirrors.empty?
mirror = @mirrors.shift
puts mirror
mirror_observer = MirrorObserver.new
future = future_for(mirror)
future.add_observer(mirror_observer, :sync_end)
future.execute
end
binding.pry
end
def future_for(mirror)
Concurrent::Future.new(executor: @thread_pool) {
puts 'future for: ' + mirror[:name]
mirror_observer.sync_start(Time.now, mirror[:name])
#MirrorSync::MirrorRsync.sync(mirror)
}
end
def wait_for_jobs_to_finish
@thread_pool.shutdown
@thread_pool.wait_for_termination
end
def generate_report
mirror_end_status = MirrorDatabase.instance.query_mirror_status
MirrorNotification::Report.new(MirrorNotification::EmailNotification.new, mirror_end_status, @time).report
end
end
Sync.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment