-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check config file was written correctly #18024
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work 👍 , left a few comments as realised the check we use on Cloud might not work for On-Premise. Sorry about that.
… retry failed write once more before logging
core/Config.php
Outdated
@@ -410,13 +410,15 @@ protected function writeConfig() | |||
throw $this->getConfigNotWritableException(); | |||
} | |||
|
|||
if (!$this->sanityCheck($localPath, $output)) { | |||
// If sanity check fails, try to write the contents once more before logging the issue. | |||
if (@file_put_contents($localPath, $output, LOCK_EX === false)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JasonMortonNZ I think there's a typo re the === false
which should be outside the function call?
Was also wondering if we should maybe check again the if
to also do the sanity check again but not 100% sure. It may be useful I suppose but not sure if there could be any issues with that.
if (@file_put_contents($localPath, $output, LOCK_EX) === false
|| !$this->sanityCheck($localPath, $output)) {
if (!$this->sanityCheck($localPath, $output)) { | ||
// If sanity check fails, try to write the contents once more before logging the issue. | ||
if (@file_put_contents($localPath, $output, LOCK_EX === false)) { | ||
StaticContainer::get(LoggerInterface::class)->info("The configuration file {$localPath} did not write correctly."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we will want to post the event here before the log call in case we adjust the above if
statement to also check for the sanityCheck
. Otherwise we would post the event twice. I suppose putting the event here would make it harder to test if the event was triggered and it be no longer possible to properly test it I suppose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tsteur yes, I was thinking about sanity check in this above if
too and moving the event to be posted within, but it was tricky for the tests. I have added an optional 3rd argument $notify
to the sanityCheck()
function which allows us to set this to true on the second try, and this will trigger the event to be posted. This should prevent the event being posted twice, and also keep testability.
core/Config.php
Outdated
$content = @file_get_contents($localPath); | ||
|
||
if (trim($content) !== trim($expectedContent)) { | ||
Piwik::postEvent('Core.configFileSanityCheckFailed', [$localPath]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the event it would be great to also add a little bit of text through a comment. The event will then be documented automatically on https://developer.matomo.org/api-reference/events
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tsteur does the comment have to follow a specific format for it to be automatically documented?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JasonMortonNZ something similar to https://github.com/matomo-org/matomo/pull/18024/files#diff-8edb7d933e422f060b96277680eaada01d6ed1a7139e20963363b9973d625d12L415 where the comment is for the configFileChanged event. Basically, each variable in the array should be documented using @param
and otherwise just regular comment AFAIK
Description:
Issue: dev-2272
Whenever we save the config file, it be great to double check if the config file was written correctly. This PR adds a couple of small sanity checks on the config file upon writing its content and writes a log entry should the sanity checks not pass.
Review