Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/include/userver/utils/periodic_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <functional>
#include <optional>
#include <string>
#include <string_view>

#include <userver/logging/level.hpp>
#include <userver/utils/assert.hpp>
Expand Down Expand Up @@ -209,6 +210,10 @@ class PeriodicTask final {
/// Get current settings. Note that they might become stale very quickly.
Settings GetCurrentSettings() const;

/// @brief Returns the task name, or "<name not set>" before a name is assigned.
/// The returned view remains valid until this object is destroyed.
std::string_view GetName() const;

private:
class Impl;
constexpr static std::size_t kSize = 448;
Expand Down
2 changes: 2 additions & 0 deletions core/src/utils/periodic_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ PeriodicTask::Settings PeriodicTask::GetCurrentSettings() const {
return *settings_ptr;
}

std::string_view PeriodicTask::GetName() const { return impl_->GetName(); }

void StartPeriodicTask(
PeriodicTask& periodic_task,
std::string name,
Expand Down
8 changes: 7 additions & 1 deletion core/src/utils/periodic_task_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ namespace {
const auto kSlowRatio = 10;
} // namespace

UTEST(PeriodicTask, Noop) { const utils::PeriodicTask task; }
UTEST(PeriodicTask, Noop) {
const utils::PeriodicTask task;

EXPECT_EQ(task.GetName(), "<name not set>");
}

UTEST(PeriodicTask, StopWithoutStart) {
utils::PeriodicTask task;
Expand All @@ -32,8 +36,10 @@ UTEST(PeriodicTask, StartStop) {
utils::PeriodicTask task("task", std::chrono::milliseconds(100), []() {});

EXPECT_TRUE(task.IsRunning());
EXPECT_EQ(task.GetName(), "task");
task.Stop();
EXPECT_FALSE(task.IsRunning());
EXPECT_EQ(task.GetName(), "task");
}

UTEST(PeriodicTask, StartNoStop) {
Expand Down
Loading