lib/flapjack/data/migration.rb in flapjack-1.2.0 vs lib/flapjack/data/migration.rb in flapjack-1.2.1rc1
- old
+ new
@@ -16,82 +16,118 @@
raise "Redis connection not set" unless redis = options[:redis]
semaphore = nil
strikes = 0
begin
- semaphore = Flapjack::Data::Semaphore.new(resource, :redis => redis, :expiry => 60)
+ semaphore = Flapjack::Data::Semaphore.new(resource, :redis => redis, :expiry => 300)
rescue Flapjack::Data::Semaphore::ResourceLocked
strikes += 1
- if strikes < 5
+ if strikes < 10
sleep 2
retry
end
sempahore = nil
end
semaphore
end
- def self.migrate_entity_check_data_if_required(options = {})
+ def self.create_entity_ids_if_required(options = {})
raise "Redis connection not set" unless redis = options[:redis]
-
logger = options[:logger]
+ if redis.exists('created_ids_for_old_entities_without_ids')
+ return
+ end
+
semaphore = obtain_semaphore(ENTITY_DATA_MIGRATION, :redis => redis)
if semaphore.nil?
unless logger.nil?
- logger.fatal "Could not obtain lock for data migration. Ensure that " +
+ logger.fatal "Could not obtain lock for data migration (entity id creation). Ensure that " +
"no other flapjack processes are running that might be executing " +
"migrations, check logs for any exceptions, manually delete the " +
"'#{ENTITY_DATA_MIGRATION}' key from your Flapjack Redis " +
"database and try running Flapjack again."
end
- exit
+ raise "Unable to obtain semaphore #{ENTITY_DATA_MIGRATION}"
end
- if redis.exists('all_checks')
+ begin
+ logger.warn "Ensuring all entities have ids ..." unless logger.nil?
+
+ Flapjack::Data::EntityCheck.find_current_names_by_entity(:redis => redis, :logger => logger).keys.each {|entity_name|
+ entity = Flapjack::Data::Entity.find_by_name(entity_name, :create => true, :redis => redis, :logger => logger)
+ }
+
+ all_checks = Flapjack::Data::EntityCheck.all(:redis => redis, :logger => logger, :create_entity => true)
+
+ redis.set('created_ids_for_old_entities_without_ids', 'true')
+ logger.warn "Entity id creation complete."
+ ensure
semaphore.release
- return
end
+ end
- logger.warn "Upgrading Flapjack's entity/check Redis indexes..." unless logger.nil?
+ def self.migrate_entity_check_data_if_required(options = {})
+ raise "Redis connection not set" unless redis = options[:redis]
- check_names = redis.keys('check:*').map {|c| c.sub(/^check:/, '') } |
- Flapjack::Data::EntityCheck.find_current_names(:redis => redis)
+ logger = options[:logger]
- unless check_names.empty?
- timestamp = Time.now.to_i
+ if redis.exists('all_checks')
+ return
+ end
- check_names.each do |ecn|
- redis.zadd("all_checks", timestamp, ecn)
- entity_name, check = ecn.split(':', 2)
- redis.zadd("all_checks:#{entity_name}", timestamp, check)
- # not deleting the check hashes, they store useful data
+ semaphore = obtain_semaphore(ENTITY_DATA_MIGRATION, :redis => redis)
+ if semaphore.nil?
+ unless logger.nil?
+ logger.fatal "Could not obtain lock for entity check data migration. Ensure that " +
+ "no other flapjack processes are running that might be executing " +
+ "migrations, check logs for any exceptions, manually delete the " +
+ "'#{ENTITY_DATA_MIGRATION}' key from your Flapjack Redis " +
+ "database and try running Flapjack again."
end
+ raise "Unable to obtain semaphore #{ENTITY_DATA_MIGRATION}"
end
- logger.warn "Checks indexed." unless logger.nil?
+ begin
+ logger.warn "Upgrading Flapjack's entity/check Redis indexes..." unless logger.nil?
- entity_name_keys = redis.keys("entity_id:*")
- unless entity_name_keys.empty?
- ids = redis.mget(*entity_name_keys)
+ check_names = redis.keys('check:*').map {|c| c.sub(/^check:/, '') } |
+ Flapjack::Data::EntityCheck.find_current_names(:redis => redis)
- entity_name_keys.each do |enk|
- enk =~ /^entity_id:(.+)$/; entity_name = $1; entity_id = ids.shift
+ unless check_names.empty?
+ timestamp = Time.now.to_i
- redis.hset('all_entity_names_by_id', entity_id, entity_name)
- redis.hset('all_entity_ids_by_name', entity_name, entity_id)
-
- redis.del(enk)
- redis.del("entity:#{entity_id}")
+ check_names.each do |ecn|
+ redis.zadd("all_checks", timestamp, ecn)
+ entity_name, check = ecn.split(':', 2)
+ redis.zadd("all_checks:#{entity_name}", timestamp, check)
+ # not deleting the check hashes, they store useful data
+ end
end
- end
- logger.warn "Entities indexed." unless logger.nil?
+ logger.warn "Checks indexed." unless logger.nil?
- semaphore.release
+ entity_name_keys = redis.keys("entity_id:*")
+ unless entity_name_keys.empty?
+ ids = redis.mget(*entity_name_keys)
- logger.warn "Indexing complete." unless logger.nil?
+ entity_name_keys.each do |enk|
+ enk =~ /^entity_id:(.+)$/; entity_name = $1; entity_id = ids.shift
+
+ redis.hset('all_entity_names_by_id', entity_id, entity_name)
+ redis.hset('all_entity_ids_by_name', entity_name, entity_id)
+
+ redis.del(enk)
+ redis.del("entity:#{entity_id}")
+ end
+ end
+
+ logger.warn "Entities indexed." unless logger.nil?
+ logger.warn "Indexing complete." unless logger.nil?
+ ensure
+ semaphore.release
+ end
end
def self.refresh_archive_index(options = {})
raise "Redis connection not set" unless redis = options[:redis]
archive_keys = redis.keys('events_archive:*')
@@ -145,6 +181,6 @@
redis.set('corrected_notification_rule_contact_linkages', 'true')
end
end
end
-end
\ No newline at end of file
+end