module Jason

Constants

VERSION

Public Class Methods

init() click to toggle source
# File lib/jason.rb, line 38
def self.init
  # Don't run in AR migration / generator etc.
  return if $PROGRAM_NAME == '-e' || ActiveRecord::Base.connection.migration_context.needs_migration?

  # 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 }
  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}"

      # This is necessary to ensure all Rails methods have been added to model before we attempt to cache.
      Rails.configuration.after_initialize do
         model.classify.constantize.cache_all
      end
      puts "Done"
    end
  end

  $redis_jason.set('jason:last_schema', current_schema.to_json)
ensure
  $redis_jason.del('jason:schema:lock')
end
setup() { |self| ... } click to toggle source

this function maps the vars from your app into your engine

# File lib/jason.rb, line 70
def self.setup(&block)
  yield self

  $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 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