From 422d248cbef401dabe7f1e972f642368c997c962 Mon Sep 17 00:00:00 2001 From: Digimer Date: Tue, 15 Mar 2022 23:42:31 -0400 Subject: [PATCH] * Updated Database->insert_or_update_states() to not actually record unless the state_host_uuid exists in all available databases. Signed-off-by: Digimer --- Anvil/Tools/Database.pm | 42 +++++++++++++++++++++++++++++++++++++++++ share/words.xml | 1 + 2 files changed, 43 insertions(+) diff --git a/Anvil/Tools/Database.pm b/Anvil/Tools/Database.pm index 5bf1a5d9..1ce8fa02 100644 --- a/Anvil/Tools/Database.pm +++ b/Anvil/Tools/Database.pm @@ -12226,6 +12226,48 @@ sub insert_or_update_states return(""); } + # It's possible during initialization that a state could be set before the host is in the database's + # hosts table. This prevents that condition from causing a problem. + my $hosts_ok = 1; + my $db_uuids = []; + my $query = "SELECT COUNT(*) FROM hosts WHERE host_uuid = ".$anvil->Database->quote($state_host_uuid).";"; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); + if ($uuid) + { + push @{$db_uuids}, $uuid; + } + else + { + foreach my $db_uuid (sort {$a cmp $b} keys %{$anvil->data->{cache}{database_handle}}) + { + push @{$db_uuids}, $db_uuid; + } + } + foreach my $db_uuid (@{$db_uuids}) + { + my $count = $anvil->Database->query({debug => 2, uuid => $db_uuid, query => $query, source => $THIS_FILE, line => __LINE__})->[0]->[0]; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { + 's2:db_uuid' => $db_uuid, + 's2:count' => $count, + }}); + if (not $count) + { + $hosts_ok = 0; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { hosts_ok => $hosts_ok }}); + + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "warning_0144", variables => { + state_info => $state_name." -> ".$state_note, + db_uuid => $db_uuid, + host_uuid => $state_host_uuid, + }}); + } + } + if (not $hosts_ok) + { + # Don't save. + return(""); + } + # If we don't have a UUID, see if we can find one for the given state server name. if (not $state_uuid) { diff --git a/share/words.xml b/share/words.xml index bfc3fe76..485e5c96 100644 --- a/share/words.xml +++ b/share/words.xml @@ -3132,6 +3132,7 @@ We will sleep a bit and try again. [ Warning ] - We waited for: [#!variable!wait_time!#] seconds for all users of the local database to exit. Giving up waiting and taking the database down now. [ Warning ] - The command: [#!variable!command!#] is still using our database. [ Warning ] - While evaluating database shutdown, the host UUID: [#!variable!host_uuid!#] was not yet found in the database on host: [#!variable!db_uuid!#]. DB shutdown will not happen until all hosts are in all DBs. + [ Warning ] - While preparing to record the state: [#!variable!state_info!#], the host UUID: [#!variable!host_uuid!#] was not yet found in the database on host: [#!variable!db_uuid!#]. NOT recording the state!