Diff chef search return sets?

Greetings,

What would be a method to capture the differences between a previous Chef search return set and a current search return set?

First Chef search:

node01
node02
node03

Second Chef search:

node01
node03

The problem I'm facing is I need to stop a process for node02, before using the search results to dynamically update a cluster configuration file. This is a client's homegrown, clustering solution, so I cannot just restart the cluster service to remove the deleted node.

Maybe write the search results into a JSON file for comparison purposes. Is there a better way?

This seems to be working, but I’d still like to hear if there’s a better way.

# Perform Chef Search for all nodes assigned to Oracle Observer
ha_databases = {}

query = "observers:#{node['fqdn']}"

if Chef::Config[:solo]
  Chef::Log.warn('This recipe uses search. Chef Solo does not support search.')
else
  search(:node, query).each do |result|
    result['db_instances'].each do |db_instance|
      ha_databases[db_instance[:sid]] ||= []
      if result['ha_role'] == 'primary'
        ha_databases[db_instance[:sid]] << { fqdn: result['fqdn'], dbunique: "#{db_instance[:sid]}_PRI", ver: db_instance[:ver] }
      elsif result['ha_role'] == 'standby'
        ha_databases[db_instance[:sid]] << { fqdn: result['fqdn'], dbunique: "#{db_instance[:sid]}_SBY", ver: db_instance[:ver] }
      end
    end
  end
end

# Stop Observer for any deleted database
prev_search = JSON.parse(File.read('/tmp/prev_search.json'))
(prev_search.keys - ha_databases.keys).each do |sid|
  execute "stop_observer_#{sid}" do
    command "/u01/app/oracle/observer/bin/StartStopOBS.ksh -A stop -I #{sid}"
    cwd "/u01/app/oracle/observer/log"
    environment ({ ORACLE_BASE: '/u01/app/oracle',
                   ORACLE_HOME: "/u01/app/oracle/product/#{OBSVR_VER}/dbclient_1",
                   DB_NAME: sid })
    only_if "/bin/ps -ef | grep [d]gmgrl | grep -q #{sid}"
  end
end

# Store Chef Search results into JSON file
ruby_block "Saving search results" do
  block do
    File.open("/tmp/prev_search.json", "w") do |f|
      f.write(ha_databases.to_json)
    end
  end
end