3

I've got a Redirect model that allows admins to build in a replacement route if they accidentally emailed out a bad one. For instance, /documents/40 accidentally got deleted, but they emailed it out as a link, so instead build a redirect that will go from /documents/40 to /documents/41.

The code works just fine, most of the magic is in the routes file:

Tenant.includes(:redirects).each do |tenant|
  constraints(subdomain: tenant.domain[0...tenant.domain.index('.')]) do
    tenant.redirects.each do |redirect|
      get redirect.old_path, to: 'redirects#show', id: redirect.id
    end
  end
end

And that works just fine. The issue comes when you try to create, update, or destroy an instance of the Redirect class. In the RedirectsController I've got it so that it executes:

Platform::Application.reload_routes!

Which works great when there's only one Passenger process running. However, in production we have upwards of 8 processes running at a given time, and so this reload_routes! call only affects the one process.

Does anyone know how I would go about reloading the routes in all Passenger processes short of restarting Passenger?

4
  • Are you running Passenger multithreaded, or multi-process single-threaded? Jun 2, 2014 at 20:58
  • Good point. It's the latter. I've updated the question.
    – kddeisz
    Jun 2, 2014 at 21:07
  • Instead of redirecting, could you just set the document instance to the revised id like @document = Document.find(redirect.id)
    – n_i_c_k
    Jun 2, 2014 at 21:22
  • This is used for many possible models, not just documents.
    – kddeisz
    Jun 2, 2014 at 21:35

2 Answers 2

1

If you want to share data between processes, use a layer that is meant for that task, such as memcached, redis or the database.

Phusion Passenger may spawn multiple application processes. Each process has its own memory and cannot access the memory of other processes. As described in his architecture:

http://www.modrails.com/documentation/Architectural%20overview.html#spawn_server

0

We finally found a solution. We used the nature of Threads to keep state across multiple requests and created a middleware that forces the current Thread to run Platform::Application.reload_routes! whenever the timestamp saved in Thread.current doesn`t match the timestamp of the last routes change.

See the full code here:

Rails reload dynamic routes on multiple instances/servers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.