1

Hyperstack is an isomorphic framework where same code can run server or client side. So there are specific cases where depending on where some piece of code gets executed (server or client side) different things should be accomplished (client synchronization etc.).

The problem is that relying on the default check if

defined?(Rails::Server) 

depends on the webserver you are running and the enclosing environment.

For example i run on puma (in docker for development and in Ubuntu server for production) and even in that case defined?(Rails::Server) works fine in development but not in production. This reveals that server execution detection depends not only on the actual server you are running on, but also on the method used to start it (e.x. rails s VS puma start)

Additional information can be found here:

  1. Detect if application was started as HTTP server or not (rake task, rconsole etc)

  2. https://gitter.im/ruby-hyperloop/chat?at=59d60f2201110b72317cd61c

  3. https://hyperstack-org.slack.com/archives/CHRQ5U8HL/p1557262851049900

Is there a standard way to check whether something in Rails is executing on the server process/thread (not in browser, some sort of client, console, migration, rake task etc.) without relying on some hack to identify or declare what server we deploy on (puma, thin, nginx etc.)?

Michail
  • 147
  • 8

1 Answers1

0

You can use the RUBY_ENGINE guard to see if the code is running in Opal or not.

if RUBY_ENGINE == 'opal'
    # I am on the client
end

This is very useful in your Isomorphic models to exclude parts of the model's methods from existing on the client. Also very useful for using added Gem methods which make no sense in the client code.

BarrieH
  • 373
  • 3
  • 11
  • That is the way to check if you are in opal context. I ask for a way to distinguish between console execution and in-server execution. The problem is that Rails::Server constant definition depends on how you start you web server process (in case of puma for example), so that you can decide if you need to broadcast messages to your connected channels... – Michail May 16 '19 at 04:29