Runtime: reset stale Core baseline on upgrade
This commit is contained in:
1 parent
d002c528f5
commit
fca26fd066
4 files changed
+51
-7
No files matched your search
@@ -52,6 +52,11 @@ namespace Echoion::Plugins::ShortUrl
|
||||
std::size_t maximumConcurrency{4};
|
||||
};
|
||||
|
||||
// Missing endpoint means this optional provider is intentionally dormant.
|
||||
// Malformed or partially-present configuration still counts as configured
|
||||
// so plugin activation rejects it instead of silently ignoring it.
|
||||
[[nodiscard]] bool hasExternalConfiguration(std::string_view payload);
|
||||
|
||||
[[nodiscard]] std::expected<ExternalConfig, std::string>
|
||||
parseExternalConfig(std::string_view payload);
|
||||
|
||||
|
||||
@@ -615,6 +615,18 @@ namespace Echoion::Plugins::ShortUrl
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool hasExternalConfiguration(const std::string_view payload)
|
||||
{
|
||||
if (payload.empty()) {
|
||||
return false;
|
||||
}
|
||||
auto root = Void::Json::parse(payload);
|
||||
if (!root || !root->is_object()) {
|
||||
return true;
|
||||
}
|
||||
return settingsObject(*root).find("endpoint") != nullptr;
|
||||
}
|
||||
|
||||
std::expected<ExternalConfig, std::string>
|
||||
parseExternalConfig(const std::string_view payload)
|
||||
{
|
||||
|
||||
+24
-7
@@ -41,6 +41,11 @@ namespace Echoion::Plugins::ShortUrl
|
||||
{
|
||||
shortener_.reset();
|
||||
activationError_.clear();
|
||||
dormant_ = !hasExternalConfiguration(configPayload_);
|
||||
if (dormant_) {
|
||||
activationError_ = "plugin is not configured";
|
||||
return;
|
||||
}
|
||||
auto config = parseExternalConfig(configPayload_);
|
||||
if (!config) {
|
||||
activationError_ = std::move(config).error();
|
||||
@@ -56,6 +61,11 @@ namespace Echoion::Plugins::ShortUrl
|
||||
|
||||
void on_unload() override { shortener_.reset(); }
|
||||
|
||||
[[nodiscard]] bool ready() const noexcept
|
||||
{
|
||||
return dormant_ || shortener_ != nullptr;
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::ShortUrl::Health health() const override
|
||||
{
|
||||
if (shortener_ != nullptr) {
|
||||
@@ -73,10 +83,16 @@ namespace Echoion::Plugins::ShortUrl
|
||||
shorten(const Void::ShortUrl::ShortenRequest& request) override
|
||||
{
|
||||
if (shortener_ == nullptr) {
|
||||
return {.error = {
|
||||
return {
|
||||
.short_url = {},
|
||||
.key = {},
|
||||
.expires_at_unix_seconds = 0,
|
||||
.error =
|
||||
{
|
||||
.code = Void::ShortUrl::ErrorCode::unavailable,
|
||||
.message = "external shortener is unavailable",
|
||||
}};
|
||||
},
|
||||
};
|
||||
}
|
||||
return shortener_->shorten(request);
|
||||
}
|
||||
@@ -86,6 +102,7 @@ namespace Echoion::Plugins::ShortUrl
|
||||
std::string configPayload_;
|
||||
std::string activationError_;
|
||||
std::unique_ptr<ExternalShortener> shortener_;
|
||||
bool dormant_ = false;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Plugins::ShortUrl
|
||||
@@ -124,11 +141,11 @@ void void_plugin_configure(void* instance, const char* config)
|
||||
|
||||
int void_plugin_ready(void* instance)
|
||||
{
|
||||
const auto health =
|
||||
static_cast<Echoion::Plugins::ShortUrl::ExternalShortenerPlugin*>(
|
||||
instance)
|
||||
->health();
|
||||
return health.status == Void::ShortUrl::HealthStatus::healthy ? 0 : 1;
|
||||
return static_cast<Echoion::Plugins::ShortUrl::ExternalShortenerPlugin*>(
|
||||
instance)
|
||||
->ready()
|
||||
? 0
|
||||
: 1;
|
||||
}
|
||||
|
||||
Void::Service::IService* void_plugin_service(void* instance)
|
||||
|
||||
@@ -34,6 +34,7 @@ namespace
|
||||
int main()
|
||||
{
|
||||
using Echoion::Plugins::ShortUrl::ExternalShortener;
|
||||
using Echoion::Plugins::ShortUrl::hasExternalConfiguration;
|
||||
using Echoion::Plugins::ShortUrl::parseExternalConfig;
|
||||
|
||||
int failures{};
|
||||
@@ -46,6 +47,15 @@ int main()
|
||||
};
|
||||
|
||||
EnvironmentVariable token{"ECHOION_SHORTENER_TEST_TOKEN", "test secret"};
|
||||
check(!hasExternalConfiguration(
|
||||
R"JSON({"settings":{},"_host":{"stateDirectory":"/tmp"}})JSON"),
|
||||
"empty host envelope did not keep the optional provider dormant");
|
||||
check(
|
||||
hasExternalConfiguration(R"JSON({"settings":{"endpoint":false}})JSON"),
|
||||
"partially configured provider was incorrectly treated as dormant");
|
||||
check(hasExternalConfiguration("{"),
|
||||
"malformed provider configuration was incorrectly treated as "
|
||||
"dormant");
|
||||
const auto jsonConfig = R"JSON({
|
||||
"endpoint":"https://short.example/api",
|
||||
"method":"POST",
|
||||
|
||||
Reference in new issue
Block a user