-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit_hash:6fbd19ef34d6ecfb2675580d139306e75d6bdba9
- Loading branch information
Showing
9 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pytest_plugins = ['pytest_userver.plugins.core', 'pytest_userver.plugins'] |
26 changes: 26 additions & 0 deletions
26
core/functional_tests/basic_chaos/tests-restart/test_restart.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import asyncio | ||
import datetime | ||
|
||
|
||
async def wait_for_daemon_stop(_global_daemon_store): | ||
deadline = datetime.datetime.now() + datetime.timedelta(seconds=10) | ||
while ( | ||
datetime.datetime.now() < deadline | ||
and _global_daemon_store.has_running_daemons() | ||
): | ||
await asyncio.sleep(0.05) | ||
|
||
assert ( | ||
not _global_daemon_store.has_running_daemons() | ||
), 'Daemon has not stopped' | ||
await _global_daemon_store.aclose() | ||
|
||
|
||
async def test_restart(monitor_client, service_client, _global_daemon_store): | ||
response = await monitor_client.get('/restart', params={'delay': '1'}) | ||
assert response.status == 200 | ||
|
||
response = await service_client.get('/ping') | ||
assert response.status == 500 | ||
|
||
wait_for_daemon_stop(_global_daemon_store) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#pragma once | ||
|
||
/// @file userver/server/handlers/restart.hpp | ||
/// @brief @copybrief server::handlers::Restart | ||
|
||
#include <atomic> | ||
|
||
#include <userver/server/handlers/http_handler_base.hpp> | ||
|
||
USERVER_NAMESPACE_BEGIN | ||
|
||
namespace server::handlers { | ||
|
||
// clang-format off | ||
|
||
/// @ingroup userver_components userver_http_handlers | ||
/// | ||
/// @brief Handler that stops the service. It is expected that an external system | ||
/// (e.g. systemd or supervisord) restarts the service afterwards. | ||
/// | ||
/// The handler uses monitor port. | ||
/// | ||
/// The component has no service configuration except the | ||
/// @ref userver_http_handlers "common handler options". | ||
|
||
// clang-format on | ||
|
||
class Restart final : public HttpHandlerBase { | ||
public: | ||
Restart(const components::ComponentConfig&, const components::ComponentContext&); | ||
|
||
/// @ingroup userver_component_names | ||
/// @brief The default name of server::handlers::Restart | ||
static constexpr std::string_view kName = "handler-restart"; | ||
|
||
std::string HandleRequestThrow(const http::HttpRequest&, request::RequestContext&) const override; | ||
|
||
components::ComponentHealth GetComponentHealth() const override; | ||
|
||
private: | ||
mutable std::atomic<components::ComponentHealth> health_; | ||
}; | ||
|
||
} // namespace server::handlers | ||
|
||
USERVER_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <userver/server/handlers/restart.hpp> | ||
|
||
#include <userver/components/run.hpp> | ||
#include <userver/engine/sleep.hpp> | ||
#include <userver/utils/async.hpp> | ||
#include <userver/utils/from_string.hpp> | ||
|
||
USERVER_NAMESPACE_BEGIN | ||
|
||
namespace server::handlers { | ||
|
||
namespace { | ||
constexpr std::string_view kDelay = "delay"; | ||
constexpr std::chrono::seconds kDelayDefault{20}; | ||
} // namespace | ||
|
||
Restart::Restart(const components::ComponentConfig& config, const components::ComponentContext& context) | ||
: HttpHandlerBase(config, context, true), health_{components::ComponentHealth::kOk} {} | ||
|
||
std::string Restart::HandleRequestThrow(const http::HttpRequest& request, request::RequestContext&) const { | ||
auto delay = kDelayDefault; | ||
if (request.HasArg(kDelay)) { | ||
delay = std::chrono::seconds(utils::FromString<int>(request.GetArg(kDelay))); | ||
} | ||
|
||
health_ = components::ComponentHealth::kFatal; | ||
engine::CriticalAsyncNoSpan([delay] { | ||
engine::InterruptibleSleepFor(std::chrono::seconds(delay)); | ||
if (engine::current_task::ShouldCancel()) return; | ||
components::RequestStop(); | ||
}).Detach(); | ||
|
||
return "OK"; | ||
} | ||
|
||
components::ComponentHealth Restart::GetComponentHealth() const { return health_; } | ||
|
||
} // namespace server::handlers | ||
|
||
USERVER_NAMESPACE_END |