WHY!? (Rails x Apache) Why does the old code still run until I restart Apache?
Overview
I'm Motoya, a PM (trainee)! I've just joined the company, but I've come across some knowledge that I'd like to share when I'm involved in a really interesting project, so I'll share it with you
Premise
When considering the procedure for checking whether files that were created for debugging purposes and need to be deleted at the time of release have been mixed in as a pre-release procedure, wouldn't it be most reliable to check the internal structure after updating the server, taking advantage of the characteristic that even if a file on the server is updated, the old code continues to run until "Apache" is restarted? Can this characteristic be guaranteed on Apache? This is what we considered from that question!
Target Audience
- Someone who lives a broad but shallow life like me
- Back-end engineer
Main topic
conclusion
It is a misconception that the old code is guaranteed to work until you restart Apache. It is guaranteed to work if your Rails production environment is configured as follows :
config.cache_classes = true
Commentary
config.cache_classes The official Rails documentation for config.cache_classes states the following:
3.1.6 config.cache_classes Specifies whether to reload application classes and modules for each request (= not cache them). The default value of config.cache_classes is false in development mode, so code updates are reflected immediately, and true in production mode, so it works faster. In test mode, if the spring gem is installed, the default is false, otherwise it is true. https://railsguides.jp/configuring.html#config-cache-classes
From the above, the important points are
- config.cache_classes = true caches application classes and modules at server startup
- The default setting for the production environment is config.cache_classes = true.
It is two points.
Why restarting Apache (web server) became a focus
With the following three-tier web structure in mind
Basically, during the server construction stage, the web server and application server are set up to start and stop in tandem. *Until now, I've thought of it as a no-brainer, but if you don't set it up this way, there seem to be many cases where you forget to start one of them and something terrible happens.
In other words, restarting Apache also restarts the application server, so restarting Apache became the focus.
Misleading this topic
Those who have experience building applications with Rails
[CentOS7]の場合
systemctl restart httpd
If you don't run the command above, even if you update the source code on the server side to the latest version, it won't be reflected in the production environment
I think many people have had this experience.
In other words, I think many people think that the updates will not be reflected unless the web server restart command is executed. *That was me, to a certain extent...
However, as mentioned in the previous section, the issue of updates in the production environment not being reflected unless the web server is restarted is actually triggered by restarting the application server, and this can be changed depending on the settings on the web framework side
Thoughts
While researching this topic, I came across processes, DSOs, shared objects, what are they, can you eat them?
I've stumbled into a deep infrastructure technology, so I'd like to continue digging deeper and output my findings!!!!!
ps. I really wanted to try out CI/CD tools.