lundi 29 janvier 2018

Rails reload dynamic routes on multiple instances/servers

How can we force Rails to reload_routes on multiple servers/instances?

We have a multi-tenant platform in Google App-Engine running on 5+ instances and we want all of our sites to define their own set of routes from the backend. Whenever we have a new site we currently have to restart all servers in order to be able to access the new routes.

We followed this guide but it does only work on a local environment and is not updating routes on all servers in production without restarting the servers.

Our route files look like this:

routes.rb

Frontend::Application.routes.draw do
  root 'home#index'
  ...
  DynamicRoutes.load
end

lib/dynamic_routes.rb

def self.load 
  Frontend::Application.routes.draw do
    Site.all.each do |site|
      site.routes.each do |custom_route|
       route_name = custom_route[0]
       route = custom_route[1]

       # write the route with the host constraint
       self.constraints(:host => site.hostname) do
         case route_name
         when :contact_form
           mapper.match "#{route}", to: 'contact_forms#new' as: "contact_#{site.id}"
         end
         ...
       end
     end
    end
  end
end

def self.reload
  Frontend::Application.reload_routes!     
end

after each update of routes or creation of a new site we are running DynamicRoutes::reload

Aucun commentaire:

Enregistrer un commentaire