<commit_before>require 'rufus/scheduler'

class Site

  include MongoMapper::Document
  many :events

  key :name, String
  key :url, String

  after_create :update_new_site

  timestamps!

  def update_new_site
    scheduler = Rufus::Scheduler.new
    scheduler.in '1s' do
      self.update_events
    end
  end

  def update_events
    p "DEBUG: updating events for site #{self.name}"
    nagios = NagiosHarder::Site.new("#{self.url}/cgi-bin/", ENV['NAGIOS_USER'], ENV['NAGIOS_PASS'])
    begin
      if ! Site.find_by_id(self.id).nil?
        self.events.clear unless self.events.empty?
        nagios.service_status(:critical).each do |problem|
          self.events << Event.new(problem.to_hash)
        end
        self.save
      end
    rescue => e
      raise "Unable to retrieve service_status for site #{self.name} at #{self.url}: #{e.message}"
    end
    begin
      nagios.host_status(:critical).each do |problem|
        p problem
      end
    rescue => e
      raise "Unable to retrieve host_status for site #{self.name} at #{self.url}: #{e.message}"
    end
  end

  def self.update_all_sites
    Site.all.each do |site|
      site.update_events
    end
  end
end<commit_msg>effectively make update_events run in parallel<commit_after>require 'rufus/scheduler'

class Site

  include MongoMapper::Document
  many :events

  key :name, String
  key :url, String

  after_create :update_new_site

  timestamps!

  def update_new_site
    scheduler = Rufus::Scheduler.new
    scheduler.in '1s' do
      self.update_events
    end
  end

  def update_events
    p "DEBUG: updating events for site #{self.name}"
    nagios = NagiosHarder::Site.new("#{self.url}/cgi-bin/", ENV['NAGIOS_USER'], ENV['NAGIOS_PASS'])
    begin
      if ! Site.find_by_id(self.id).nil?
        self.events.clear unless self.events.empty?
        nagios.service_status(:critical).each do |problem|
          self.events << Event.new(problem.to_hash)
        end
        self.save
      end
    rescue => e
      raise "Unable to retrieve service_status for site #{self.name} at #{self.url}: #{e.message}"
    end
    begin
      nagios.host_status(:critical).each do |problem|
        p problem
      end
    rescue => e
      raise "Unable to retrieve host_status for site #{self.name} at #{self.url}: #{e.message}"
    end
  end

  def self.update_all_sites
    scheduler = Rufus::Scheduler.new
    Site.all.each do |site|
      scheduler.in '1s' do
        site.update_events
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.1062124248496994, 'fraction_numerical': 0.0012024048096192384, 'mean_word_length': 2.6977777777777776, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1978286', 'n_tokens_mistral': 844, 'n_tokens_neox': 819, 'n_words': 208}