Mai*_*kon 15 ruby yaml ruby-on-rails sidekiq
我目前正在项目中使用Sidekiq,我有以下YAML配置文件:
:concurrency: 5
:pidfile: /tmp/pids/sidekiq.pid
:logfile: log/sidekiq.log
staging:
:concurrency: 10
production:
:concurrency: 20
queues:
- default
Run Code Online (Sandbox Code Playgroud)
我之前没有看到在一把钥匙前面有一个冒号,但省略了冒号会产生有趣的结果.:pidfile:例如,在前面有冒号的情况下,它会创建/覆盖没有它的目标文件,它使用已存在的目标文件并且不写入它.
这是在某处记录的,还是Sidekiq对某些键的期望?
spi*_*ann 17
以冒号开头的YAML键在Ruby中生成符号化键,而没有冒号的键将生成字符串化键:
require 'yaml'
string =<<-END_OF_YAML
:concurrency: 5
:pidfile: /tmp/pids/sidekiq.pid
:logfile: log/sidekiq.log
staging:
:concurrency: 10
production:
:concurrency: 20
queues:
- default
END_OF_YAML
YAML.load(string)
# {
# :concurrency => 5,
# :pidfile => "/tmp/pids/sidekiq.pid",
# :logfile => "log/sidekiq.log",
# "staging" => {
# :concurrency => 10
# },
# "production" => {
# :concurrency => 20
# },
# "queues" => [
# [0] "default"
# ]
# }
Run Code Online (Sandbox Code Playgroud)
注意:如果gem依赖于符号化键,则字符串化键不会覆盖其默认值.
它实际上不是 sidekiq 特定的。键前面的冒号只是使该键成为符号而不是字符串:
# example.yml
a:
value: 1
:b:
value: 2
yaml = YAML.load_file('example.yml')
yaml["a"] => { "value" => 1 }
yaml[:b] => { "value" => 1 }
Run Code Online (Sandbox Code Playgroud)
因此,如果您的代码使用键符号访问配置,您应该在 yaml 文件中的键前面添加冒号,或者使用一些键转换,例如#with_indifferent_access结果哈希(解析 yaml 文件后)
| 归档时间: |
|
| 查看次数: |
2165 次 |
| 最近记录: |