lib/flapjack/data/migration.rb in flapjack-1.2.1 vs lib/flapjack/data/migration.rb in flapjack-1.2.2
- old
+ new
@@ -10,13 +10,15 @@
class Migration
ENTITY_DATA_MIGRATION = 'entity_data_migration'
# copied from jsonapi/contact_methods.rb, could extract both into separate file
- def self.obtain_semaphore(resource, options = {})
+ def self.obtain_semaphore(resource, description, options = {})
raise "Redis connection not set" unless redis = options[:redis]
+ logger = options[:logger]
+
semaphore = nil
strikes = 0
begin
semaphore = Flapjack::Data::Semaphore.new(resource, :redis => redis, :expiry => 300)
rescue Flapjack::Data::Semaphore::ResourceLocked
@@ -25,31 +27,34 @@
sleep 2
retry
end
sempahore = nil
end
+
+ if semaphore.nil?
+ unless logger.nil?
+ logger.fatal "Could not obtain lock for data migration (#{reason}). Ensure that " +
+ "no other flapjack processes are running that might be executing " +
+ "migrations, check logs for any exceptions, manually delete the " +
+ "'#{resource}' key from your Flapjack Redis " +
+ "database and try running Flapjack again."
+ end
+ raise "Unable to obtain semaphore #{resource}"
+ end
+
semaphore
end
def self.create_entity_ids_if_required(options = {})
raise "Redis connection not set" unless redis = options[:redis]
logger = options[:logger]
return if redis.exists('created_ids_for_old_entities_without_ids')
- semaphore = obtain_semaphore(ENTITY_DATA_MIGRATION, :redis => redis)
- if semaphore.nil?
- unless logger.nil?
- 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
- raise "Unable to obtain semaphore #{ENTITY_DATA_MIGRATION}"
- end
+ semaphore = obtain_semaphore(ENTITY_DATA_MIGRATION, 'entity id creation',
+ :redis => redis, :logger => logger)
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|
@@ -70,21 +75,12 @@
logger = options[:logger]
return if redis.exists('all_checks')
- 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
+ semaphore = obtain_semaphore(ENTITY_DATA_MIGRATION, 'entity check data',
+ :redis => redis, :logger => logger)
begin
logger.warn "Upgrading Flapjack's entity/check Redis indexes..." unless logger.nil?
check_names = redis.keys('check:*').map {|c| c.sub(/^check:/, '') } |
@@ -120,9 +116,36 @@
logger.warn "Entities indexed." unless logger.nil?
logger.warn "Indexing complete." unless logger.nil?
ensure
semaphore.release
+ end
+ end
+
+ def self.clear_orphaned_entity_ids(options = {})
+ raise "Redis connection not set" unless redis = options[:redis]
+
+ logger = options[:logger]
+
+ semaphore = obtain_semaphore(ENTITY_DATA_MIGRATION,
+ 'orphaned entity ids', :redis => redis, :logger => logger)
+
+ begin
+ logger.info "Checking for orphaned entity ids..." unless logger.nil?
+
+ valid_entity_data = redis.hgetall('all_entity_ids_by_name')
+
+ missing_ids = redis.hgetall('all_entity_names_by_id').reject {|e_id, e_name|
+ valid_entity_data[e_name] == e_id
+ }
+
+ unless missing_ids.empty?
+ logger.info "Clearing ids (#{missing_ids.inspect})" unless logger.nil?
+ redis.hdel('all_entity_names_by_id', missing_ids.keys)
+ end
+ ensure
+ semaphore.release
+ logger.info "Finished checking for orphaned entity ids." unless logger.nil?
end
end
def self.refresh_archive_index(options = {})
raise "Redis connection not set" unless redis = options[:redis]