Module: Excursion::Pool

Defined in:
lib/excursion/pool.rb,
lib/excursion/pool/dsl.rb,
lib/excursion/pool/application.rb,
lib/excursion/pool/dummy_application.rb

Defined Under Namespace

Classes: Application, DSL, DummyApplication

Constant Summary collapse

@@applications =
{}

Class Method Summary collapse

Class Method Details

.active_record_datastoreObject

Raises:



104
105
106
107
# File 'lib/excursion/pool.rb', line 104

def self.active_record_datastore
  raise TableDoesNotExist, "To use the :active_record datastore you must first run `rails generate excursion:active_record` followed by `rake db:migrate` to create the storage table" unless Excursion::RoutePool.table_exists?
  @@datastore ||= Excursion::Datastores::ActiveRecord.new
end

.active_record_with_memcache_datastoreObject



109
110
111
112
113
# File 'lib/excursion/pool.rb', line 109

def self.active_record_with_memcache_datastore
  raise MemcacheConfigurationError, "You must configure the :active_record_with_memcache datastore with a memcache_server" if Excursion.configuration.memcache_server.nil?
  raise TableDoesNotExist, "To use the :active_record_with_memcache datastore you must first run `rails generate excursion:active_record` followed by `rake db:migrate` to create the storage table" unless Excursion::RoutePool.table_exists?
  @@datastore ||= Excursion::Datastores::ActiveRecord.new(Excursion.configuration.memcache_server)
end

.all_applicationsObject



10
11
12
13
14
15
# File 'lib/excursion/pool.rb', line 10

def self.all_applications
  datastore.all_apps.each do |app|
    @@applications[app.name] = app
  end
  @@applications
end

.app_hash_defaultsObject



25
26
27
# File 'lib/excursion/pool.rb', line 25

def self.app_hash_defaults
  {default_url_options: Excursion.configuration.default_url_options, routes: {}, registered_at: Time.now}
end

.application(name) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/excursion/pool.rb', line 17

def self.application(name)
  check_local_cache
  return @@applications[name.to_s] if @@applications.has_key?(name.to_s) && !@@applications[name.to_s].nil?
  
  app = datastore.app(name)
  @@applications[name.to_s] = app unless app.nil?
end

.check_local_cacheObject



127
128
129
# File 'lib/excursion/pool.rb', line 127

def self.check_local_cache
  (@@refreshed = Time.now.to_i) && (@@applications = {}) if pool_updated > pool_refreshed
end

.datastoreObject

Raises:



80
81
82
83
84
85
# File 'lib/excursion/pool.rb', line 80

def self.datastore
  raise NoDatastoreError, "You must configure excursion with a datastore." if Excursion.configuration.datastore.nil?
  require "excursion/datastores/#{Excursion.configuration.datastore.to_s}"

  send "#{Excursion.configuration.datastore.to_sym}_datastore"
end

.datastore_class(type) ⇒ Object



87
88
89
# File 'lib/excursion/pool.rb', line 87

def self.datastore_class(type)
  "Excursion::Datastores::#{type.to_s.capitalize}".constantize
end

.file_datastoreObject



96
97
98
# File 'lib/excursion/pool.rb', line 96

def self.file_datastore
  simple_datastore(:file, :datastore_file)
end

.memcache_datastoreObject



100
101
102
# File 'lib/excursion/pool.rb', line 100

def self.memcache_datastore
  simple_datastore(:memcache, :memcache_server)
end

.pool_refreshedObject



123
124
125
# File 'lib/excursion/pool.rb', line 123

def self.pool_refreshed
  @@refreshed ||= 0
end

.pool_updatedObject



119
120
121
# File 'lib/excursion/pool.rb', line 119

def self.pool_updated
  datastore.get('_pool_updated').to_i || 0
end

.register_application(app = nil, opts = {}, &block) ⇒ Object

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/excursion/pool.rb', line 29

def self.register_application(app=nil, opts={}, &block)
  raise ArgumentError, "app must be an instance of Rails::Application" unless app.is_a?(Rails::Application) || block_given?
  opts = {store: true}.merge(opts)
  
  if app.is_a?(Rails::Application)
    name = app.class.name.underscore.split("/").first
    config = {default_url_options: Excursion.configuration.default_url_options}
    routes = app.routes.named_routes
    @@applications[name] = Application.new(name, config, routes)
  end
  
  if block_given?
    if name && @@applications.has_key?(name)
      DSL.block_eval(@@applications[name], &block)
    else
      block_app = DSL.block_eval(&block)
      name = block_app.name
      @@applications[name] = block_app
    end
  end
  
  if opts[:store]
    datastore.set(name, @@applications[name].to_cache)
    datastore.set('_pool_updated', Time.now.to_i)
  end
  @@applications[name]
end

.register_hash(app_hash, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/excursion/pool.rb', line 57

def self.register_hash(app_hash, opts={})
  raise ArgumentError, "you must provide at minimum a hash with a :name key" unless app_hash.is_a?(Hash) && app_hash.has_key?(:name)
  opts = {store: true}.merge(opts)
  
  app_hash = app_hash_defaults.merge(app_hash)
  name = app_hash[:name]

  if opts[:store]
    datastore.set(name, app_hash)
    datastore.set('_pool_updated', Time.now.to_i)
  end
  @@applications[name.to_s] = datastore.app(name)
end

.remove_application(app) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
# File 'lib/excursion/pool.rb', line 71

def self.remove_application(app)
  raise ArgumentError, "app must be an instance of Rails::Application" unless app.is_a?(Rails::Application)
  
  name = app.class.name.underscore.split("/").first
  datastore.delete(name)
  @@applications.delete(name)
  datastore.set('_pool_updated', Time.now.to_i)
end

.secret_key_baseObject



135
136
137
# File 'lib/excursion/pool.rb', line 135

def self.secret_key_base
  key = datastore.get('_secret_key_base') || set_secret_key_base
end

.set_secret_key_baseObject



131
132
133
# File 'lib/excursion/pool.rb', line 131

def self.set_secret_key_base
  datastore.set('_secret_key_base', Digest::MD5.hexdigest(SecureRandom.base64(32)))
end

.simple_datastore(type, config_opt) ⇒ Object



91
92
93
94
# File 'lib/excursion/pool.rb', line 91

def self.simple_datastore(type, config_opt)
  raise DatastoreConfigurationError, "You must configure the :#{type.to_s} datastore with a #{config_opt.to_s}" if Excursion.configuration.send(config_opt.to_sym).nil?
  @@datastore ||= datastore_class(type).new(Excursion.configuration.send(config_opt.to_sym))
end

.test_datastoreObject



115
116
117
# File 'lib/excursion/pool.rb', line 115

def self.test_datastore
    @@datastore ||= Excursion::Datastores::Test.new
end