diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2016-10-11 10:42:58 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2016-10-11 10:45:50 -0700 |
commit | bb9a4d31ed5864e00b5379f6b69acb2dc6588133 (patch) | |
tree | c3446913b0bd6ddb8019b252e8718d6f761447c4 | |
parent | 5e7c3b96f125c6a07c6c48c0d27899100b3be9a6 (diff) | |
download | chef-bb9a4d31ed5864e00b5379f6b69acb2dc6588133.tar.gz |
add delayed_action to the Chef::Resource API
effectively creates a resource which sends itself a delayed notification
useful to create a resource which runs exactly once at the end of a run:
```ruby
with_run_context :root do
find_resource(:execute, "/bin/true") do
action :nothing
delayed_action :run
end
end
```
avoids using a log resource to force a resource to update and force a
delayed notification which avoids the extra output from the log
resource, and avoids the resource updated count always being updated.
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r-- | lib/chef/resource.rb | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index d11fa1c80c..8048330ce1 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -181,6 +181,31 @@ class Chef alias_method :action=, :action # + # Force a delayed notification into this resource's run_context. + # + # This should most likely be paired with action :nothing + # + # @param arg [Array[Symbol], Symbol] A list of actions (e.g. `:create`) + # @return [Array[Symbol]] the list of actions. + # + def delayed_action(arg = nil) + if arg + arg = Array(arg).map(&:to_sym) + arg.each do |action| + validate( + { action: action }, + { action: { kind_of: Symbol, equal_to: allowed_actions } } + ) + # the resource effectively sends a delayed notification to itself + run_context.add_delayed_action(Notification.new(self, action, self)) + end + @delayed_action = arg + else + @delayed_action + end + end + + # # Sets up a notification that will run a particular action on another resource # if and when *this* resource is updated by an action. # |