Ruby-on-rails 使用 Phusion 乘客和 Rails 时初始服务器启动缓慢

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/853532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:14:30  来源:igfitidea点击:

Slow initial server startup when using Phusion Passenger and Rails

ruby-on-railsdeploymentpassengerphusion

提问by tsdbrown

To jump on the band-wagon of Phusion Passenger we've setup a staging server for a small rails app to test things out.

为了跟上 Phusion Passenger 的潮流,我们为一个小型 Rails 应用程序设置了一个临时服务器来进行测试。

So far it has been very nice to use, it makes installing/configuring and deploying apps a breeze. The problem is the site we're using doesn't get hit very often and it seems to shut down the servers in the background. Meaning when someone goes to the site they have a really long wait until it starts up a new server to handle the request. We've read through the documentation, tried quite a few different set-ups (smart/smart-lv2 modes, passengeridletime etc) and still haven't found a real solution.

到目前为止,它使用起来非常好,它使安装/配置和部署应用程序变得轻而易举。问题是我们使用的站点并不经常被访问,而且它似乎在后台关闭了服务器。这意味着当有人访问该站点时,他们需要等待很长时间才能启动新服务器来处理请求。我们通读了文档,尝试了很多不同的设置(智能/智能 lv2 模式、passengeridletime 等),但仍然没有找到真正的解决方案。

After ploughing through Google results we can't really find useful information. Currently we have a cron job that makes a request every-so-often in an attempt to keep the servers running.

在谷歌搜索结果后,我们无法真正找到有用的信息。目前,我们有一个 cron 作业,它经常发出请求以尝试保持服务器运行。

Is anyone else experiencing this problem and do you have any advice for a fix?

有没有其他人遇到过这个问题,你有什么修复建议吗?

回答by John Douthat

What's happening is that your Application and/or ApplicationSpawners are shutting down due to time-out. To process your new request, Passenger has to startup a new copy of your application, which can take several seconds, even on a fast machine. To fix the issue, there are a few Apache configuration options you can use to keep your Application alive.

发生的情况是您的应用程序和/或 ApplicationSpawner 由于超时而关闭。为了处理您的新请求,Passenger 必须启动您的应用程序的新副本,这可能需要几秒钟的时间,即使在快速的机器上也是如此。要解决此问题,您可以使用一些 Apache 配置选项来使应用程序保持活动状态。

Here's specifically what I've done on my servers. The PassengerSpawnMethod and PassengerMaxPreloaderIdleTime are the configuration options most important in your situation.

这是我在服务器上所做的具体工作。在您的情况下,PassengerSpawnMethod 和PassengerMaxPreloaderIdleTime 是最重要的配置选项。

# Speeds up spawn time tremendously -- if your app is compatible. 
# RMagick seems to be incompatible with smart spawning
# Older versions of Passenger called this RailsSpawnMethod
PassengerSpawnMethod smart

# Keep the application instances alive longer. Default is 300 (seconds)
PassengerPoolIdleTime 1000

# Keep the spawners alive, which speeds up spawning a new Application
# listener after a period of inactivity at the expense of memory.
# Older versions of Passenger called this RailsAppSpawnerIdleTime
PassengerMaxPreloaderIdleTime 0

# Just in case you're leaking memory, restart a listener 
# after processing 5000 requests
PassengerMaxRequests 5000

By using "smart" spawning mode and turning off PassengerMaxPreloaderIdleTime, Passenger will keep 1 copy of your application in memory at all times (after the first request after starting Apache). Individual Applicationlisteners will be forked from this copy, which is a super-cheap operation. It happens so quickly you can't tell whether or not your application has had to spawn a listener.

通过使用“智能”生成模式并关闭PassengerMaxPreloaderIdleTime,Passenger 将始终在内存中保留一份应用程序副本(在启动Apache 后的第一个请求之后)。个人Application听众fork将从这个副本中被编辑,这是一个超级便宜的操作。它发生得如此之快,您无法判断您的应用程序是否必须生成侦听器。

If your app is incompatible with smart spawning, I'd recommend keeping a large PassengerPoolIdleTime and hitting your site periodically using curl and a cronjob or monit or something to ensure the listener stays alive.

如果您的应用程序与智能生成不兼容,我建议保留一个大的PassengerPoolIdleTime 并使用curl 和cronjob 或monit 或其他东西定期访问您的站点,以确保侦听器保持活动状态。

The Passenger User Guideis an awesome reference for these and more configuration options.

乘客使用手册是为这些以及更多的配置选项真棒参考。

edit: If your app is incompatible with smart spawning, there are some new optionsthat are very nice

编辑:如果您的应用程序与智能生成不兼容,则有一些非常好的新选项

# Automatically hit your site when apache starts, so that you don't have to wait
# for the first request for passenger to "spin up" your application. This even
# helps when you have smart spawning enabled. 
PassengerPreStart http://myexample.com/
PassengerPreStart http://myexample2.com:3500/

# the minimum number of application instances that must be kept around whenever 
# the application is first accessed or after passenger cleans up idle instances
# With this option, 3 application instances will ALWAYS be available after the
# first request, even after passenger cleans up idle ones
PassengerMinInstances 3

So, if you combine PassengerPreStart and PassengerMinInstances, Passenger will spin up 3 instances immediately after apache loads, and will always keep at least 3 instances up, so your users will rarely (if ever) see a delay.

因此,如果您结合PassengerPreStart 和PassengerMinInstances,Passenger 将在apache 加载后立即启动3 个实例,并且将始终保持至少3 个实例运行,因此您的用户很少(如果有的话)看到延迟。

Or, if you're using smart spawning (recommended) with PassengerMaxPreloaderIdleTime 0already, you can add PassengerPreStartto get the additional benefit of immediate startup.

或者,如果您已经在使用智能生成(推荐)PassengerMaxPreloaderIdleTime 0,您可以添加PassengerPreStart以获得立即启动的额外好处。

Many thanks to the heroes at phusion.nl!

非常感谢phusion.nl的英雄们!

回答by Gav

Just incase there are any nginx server users stumbling upon this question, both the 'PassengerMaxRequests' and 'PassengerStatThrottleRate' directives don't translate to nginx. However the others do:

以防万一有任何 nginx 服务器用户遇到这个问题,“PassengerMaxRequests”和“PassengerStatThrottleRate”指令都不会转换为 nginx。但是其他人这样做:

rails_spawn_method smart;
rails_app_spawner_idle_time 0;
rails_framework_spawner_idle_time 0;
passenger_pool_idle_time 1000;

HTH!

哼!

EDIT rails_spawn_methodis deprecated in passenger 3 instead use

EDITrails_spawn_method在乘客 3 中被弃用,而是使用

passenger_spawn_method smart; 

everything else is just good till date.

到目前为止,其他一切都很好。

回答by Josh

You can also use PassengerMinInstances:

您还可以使用PassengerMinInstances:

http://www.modrails.com/documentation/Users%20guide%20Apache.html#PassengerMinInstances

http://www.modrails.com/documentation/Users%20guide%20Apache.html#PassengerMinInstances

This can be combined with PassengerPreStart

这可以与PassengerPreStart结合使用

回答by Shuoling Liu

RE:

关于:

# Additionally keep a copy of the Rails framework in memory. If you're 
# using multiple apps on the same version of Rails, this will speed up
# the creation of new RailsAppSpawners. This isn't necessary if you're
# only running one or 2 applications, or if your applications use
# different versions of Rails.
RailsFrameworkSpawnerIdleTime 0

Just something to add and might be useful.

只是要添加一些东西,可能会有用。

The default spawn method in the current release is "smart-lv2", which skips the framework spawner, so setting the framework spawner timeout wouldn't have effect anyway unless you explicitly set the spawn method to "smart".

当前版本中的默认 spawn 方法是“smart-lv2”,它跳过框架 spawner,因此设置框架 spawner 超时无论如何都不会生效,除非您将 spawn 方法显式设置为“smart”。

Source: http://groups.google.com/group/phusion-passenger/browse_thread/thread/c21b8d17cdb073fd?pli=1

来源:http: //groups.google.com/group/phusion-passenger/browse_thread/thread/c21b8d17cdb073fd?pli=1

回答by Shuoling Liu

If your host is a shared server, like mine, you can't change the settings and are stuck with a cron job.

如果你的主机是一个共享服务器,就像我的一样,你不能改变设置并且被一个 cron 作业卡住了。

回答by SteenhouwerD

I had also this problem but I was not able to change the passenger settings because I had no write permission to this file. I found a tool ( http://www.wekkars.com) that keeps my app responding fast. Maybe this can also be a solution for you.

我也有这个问题,但我无法更改乘客设置,因为我没有对此文件的写权限。我找到了一个工具 ( http://www.wekkars.com) 可以让我的应用程序快速响应。也许这也可以成为您的解决方案。

回答by JmJ

check the version of passenger. it was RailsSpawnMethod <string>for old versions.

检查乘客的版本。它是<string>旧版本的RailsS​​pawnMethod 。

If so (if I remember correctly), replace Passenger with Rails in all the configuration directives or look for old passenger docs for more details

如果是这样(如果我没记错的话),请在所有配置指令中用 Rails 替换乘客,或者查找旧的乘客文档以获取更多详细信息