diff --git a/app/models/duckrails/application_state.rb b/app/models/duckrails/application_state.rb index f599483..d55850d 100644 --- a/app/models/duckrails/application_state.rb +++ b/app/models/duckrails/application_state.rb @@ -13,17 +13,14 @@ class ApplicationState < ActiveRecord::Base def update_token(type) case type when :mock - self.mock_synchronization_token = Synchronizer.generate_token + after_save :save_timestamp save end end - class << self - # @return [ApplicationState] the one and only application state - def instance - ApplicationState.first || ApplicationState.create(singleton_guard: 0, - mock_synchronization_token: Synchronizer.generate_token) - end + def save_timestamp + ts = Time.zone.now.to_i + Rails.cache.write(:routes_changed_timestamp, ts) end end end diff --git a/lib/duckrails/router.rb b/lib/duckrails/router.rb index 5fa138e..bd72133 100644 --- a/lib/duckrails/router.rb +++ b/lib/duckrails/router.rb @@ -7,7 +7,6 @@ class Router class << self def register_mock(mock) REGISTERED_MOCKS << mock.id - Duckrails::ApplicationState.instance.update_token :mock REGISTERED_MOCKS.uniq! end @@ -19,7 +18,6 @@ def register_current_mocks def unregister_mock(mock) REGISTERED_MOCKS.delete mock.id - Duckrails::ApplicationState.instance.update_token :mock end def reset! diff --git a/lib/duckrails/synchronizer.rb b/lib/duckrails/synchronizer.rb index d8d84dc..5ec54ab 100644 --- a/lib/duckrails/synchronizer.rb +++ b/lib/duckrails/synchronizer.rb @@ -1,18 +1,15 @@ module Duckrails class Synchronizer - cattr_accessor :mock_synchronization_token - def initialize(app) @app = app end def call(env) - application_state = Duckrails::ApplicationState.instance - - if Duckrails::Synchronizer.mock_synchronization_token != application_state.mock_synchronization_token + timestamp = Rails.cache.read(:routes_changed_timestamp) + if Thread.current[:routes_changed_timestamp] != timestamp Rails.logger.info 'Mock synchronization token missmatch. Syncronizing...' Router.reset! - Duckrails::Synchronizer.mock_synchronization_token = application_state.mock_synchronization_token + Thread.current[:routes_changed_timestamp] = timestamp Rails.logger.info 'Mock synchronization completed.' end