如何理解 Chef 中的通知和订阅?

How to understand notifies and subscribes in Chef?

我是 Chef 的新手。最近我发现“notifies/subscribes”让我感到困惑——我认为通知应该触发另一个资源的动作,而订阅意味着一个资源应该被另一个资源触发。所以我做了一个测试,但它总是运行不符合预期。 这是我的代码 --

我的食谱--

file '/tmp/e.sh' do
    content '
#!/bin/bash
    
date +%F" "%T >> /tmp/e.log
'
    mode '777'
    notifies :run, 'execute[e.sh]', :delayed
end

execute 'e.sh' do
    command '/tmp/e.sh'
    subscribes :run, 'file[/tmp/e.sh]', :immediately
end

我认为应该是——一旦“/tmp/e.sh”改变,那么“e.sh”的“execute”资源应该被触发,否则“/tmp/e” .sh" 不应该 运行。 但令我惊讶的是——一旦 /tmp/e.sh 修改(无论从客户端还是工作站端更改),那么“执行”将 运行 三次。如果“/tmp/e.sh”不变——“执行”将运行——只执行一次。
我真的搞砸了这个 --

  1. “通知”中的“延迟”是什么意思——它应该阻止被通知的资源运行ning吗?

  2. “订阅”有什么作用? -- 即使是订阅的资源也没有触发,资源本身还在 运行ning?

    请帮忙。谢谢

Chef(notifiessubscribes)中的通知系统应满足您的要求:

once the "/tmp/e.sh" changed, then the "execute" resource of "e.sh" should be triggered, otherwise "/tmp/e.sh" should not run

Chef 中的所有资源都有默认操作。这取决于资源。 file 资源具有默认操作 :create。而对于 execute 资源,它是 :runexecute 资源每次都是 运行ning 的原因是因为它的默认操作是 :run.

所有资源也有一个 :nothing 操作。这也是一个common functionality。所以我们在使用通知系统的时候,应该使用action :nothing.

如下所示:

file '/tmp/e.sh' do
  content "#!/bin/bash\ndate +%F" "%T >> /tmp/e.log"
  mode '0755'
end

execute '/tmp/e.sh' do
  action :nothing
  subscribes :run, 'file[/tmp/e.sh]', :immediately
end

您可以使用 notifiessubscribes。现在,命令执行只会在 e.sh 文件更改后发生。

回答您的问题...

  1. what does that "delayed" mean in "notifies" -- should it prevent the notified resource running?

这是一个通知计时器。它定义了“通知”资源何时应该 运行.

  • :immediately - 之后立即将资源通知给 运行,即使在

    之间还有其他资源声明
  • :delayed - 通知资源但 运行 在所有其他资源声明已 运行 之后(这是默认设置)。这在多个资源可能发生变化但我们只想触发一次通知时很有用。

  1. what does that "subscribes" do? -- even the subscribed resouce didn't fire, and the resource itself still running?

这与 notifies 相反,即它“侦听”指定资源的变化。