lib/jason.rb in jason-rails-0.6.0 vs lib/jason.rb in jason-rails-0.6.1

- old
+ new

@@ -28,22 +28,49 @@ self.schema = {} self.transport_service = :action_cable self.pusher_region = 'eu' self.pusher_channel_prefix = 'jason' - # add default values of more config vars here + def self.init + # Check if the schema has changed since last time app was started. If so, do some work to ensure cache contains the correct data + got_lock = $redis_jason.set('jason:schema:lock', nx: true, ex: 3600) # Basic lock mechanism for multi-process environments + return if !got_lock + previous_schema = JSON.parse($redis_jason.get('jason:last_schema') || '{}') + current_schema = Jason.schema.deep_stringify_keys.deep_transform_values { |v| v.is_a?(Symbol) ? v.to_s : v } + pp current_schema + current_schema.each do |model, config| + if config != previous_schema[model] + puts "Config changed for #{model}" + puts "Old config was #{previous_schema[model]}" + puts "New config is #{config}" + puts "Rebuilding cache for #{model}" + model.classify.constantize.cache_all + puts "Done" + end + end + + $redis_jason.set('jason:last_schema', current_schema.to_json) + ensure + $redis_jason.del('jason:schema:lock') + + previous_config = 'test' + end + + # this function maps the vars from your app into your engine def self.setup(&block) yield self - end - $redis_jason = self.redis || ::ConnectionPool::Wrapper.new(size: 5, timeout: 3) { ::Redis.new(url: ENV['REDIS_URL']) } + $redis_jason = self.redis || ::ConnectionPool::Wrapper.new(size: 5, timeout: 3) { ::Redis.new(url: ENV['REDIS_URL']) } - if ![:action_cable, :pusher].include?(self.transport_service) - raise "Unknown transport service '#{self.transport_service}' specified" - end + if ![:action_cable, :pusher].include?(self.transport_service) + raise "Unknown transport service '#{self.transport_service}' specified" + end - if self.transport_service == :pusher && self.pusher.blank? - raise "Pusher specified as transport service but no Pusher client provided. Please configure with config.pusher = Pusher::Client.new(...)" + if self.transport_service == :pusher && self.pusher.blank? + raise "Pusher specified as transport service but no Pusher client provided. Please configure with config.pusher = Pusher::Client.new(...)" + end + + init end end