我们有一个用例,我们需要在部署/重新启动过程之外运行 Assets :预编译,因此最好不必重新启动 Rails 服务器进程。这在乘客环境中可能吗?
我一直在努力尝试 Rake 任务中的一堆东西并摆弄 Rails.application.config.assets 东西,但是除了使用 /usr 重新启动 Passenger 之外,没有什么能让应用程序获取对摘要的更改/bin/env touch ~/project/current/tmp/restart.txt
最佳答案
我们最终采用了两部分解决方案:
第 1 部分是设置应用程序以命中 redis 以存储“assets:version”(我们只是使用了时间戳)。然后,每当我们的流程完成预编译时,我们都会使用最新的时间戳更新此 Assets 版本。
第 2 部分是我们在所有其他 Controller 继承的主 application_controller 中添加了一个 before_filter :check_assets_version
。这个方法看起来像这样:
def check_assets_version
@@version ||= 1
latest_version = get_assets_version # wrapped call to redis to get latest version
# clear assets cache if current version isn't the same as the latest version from redis
unless latest_version.blank? || latest_version.to_s == @@version
@@version = latest_version
if Rails.env.production? || Rails.env.sandbox? || Rails.env.experimental?
nondev_reset_sprockets
else
dev_reset_sprockets @@version
end
end
end
这两个重置方法如下所示:
def nondev_reset_sprockets
manifest = YAML.load(File.read(Rails.root.join("public","assets","manifest.yml")))
manifest.each do |key, value|
Rails.application.config.assets.digests[key] = value
end
end
nondev 重置将每个值从新生成的 list 文件“填充”到内存中
def dev_reset_sprockets(version)
environment = Rails.application.assets
environment = environment.instance_variable_get('@environment') if environment.is_a?(Sprockets::Index)
environment.version = version
end
dev 重置只是启动 sprockets 的“版本”值,以便它认为(正确地)它需要重新解析并实时重新编译最新的 Assets 。
关于ruby-on-rails-3 - 如何在 Assets :precompile 之后刷新 Rails/Sprockets 以了解生产服务器上的新 list ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26769240/