userver: userver/engine/async.hpp Source File
Loading...
Searching...
No Matches
async.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/engine/async.hpp
4/// @brief Low-level TaskWithResult creation helpers
5
6#include <userver/engine/deadline.hpp>
7#include <userver/engine/impl/task_context_factory.hpp>
8#include <userver/engine/task/shared_task_with_result.hpp>
9#include <userver/engine/task/task_processor_fwd.hpp>
10#include <userver/engine/task/task_with_result.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace engine {
15
16/// @brief Runs an asynchronous function call using the specified task processor.
17///
18/// @warning If any logs are written in the task function (outside any manual tracing::Span scopes),
19/// then those logs will have no `span_id` (yikes!).
20/// If you create a span there manually, it will be disconnected from the outside trace.
21/// **Prefer utils::Async by default instead.**
22///
23/// @warning To hide a spammy span from traces, use @ref utils::AsyncHideSpan instead.
24/// Logs will then be linked to the nearest span that is written out.
25/// For complex cases, use @ref engine::TaskBuilder with @ref engine::TaskBuilder::HideSpan.
26///
27/// @warning Some clients may call tracing::Span::CurrentSpan unconditionally, so don't be too surprised
28/// if they won't work without a span scope. Do write tests.
29///
30/// `AsyncNoTracing` is useful for:
31/// * implementing periodic tasks (like utils::PeriodicTask);
32/// * worker tasks that typically run until service shutdown, and it makes no sense to have a span for the task itself;
33/// * some low-level (e.g. driver or filesystem-IO) code where logs are definitely not written;
34/// * breaking traces e.g. this is (rarely) wanted for some background tasks.
35///
36/// @see @ref utils::Async for main documentation on `Async` function family.
37/// @see @ref engine::TaskBuilder for more `Async` variants.
38template <typename Function, typename... Args>
39[[nodiscard]] auto AsyncNoTracing(TaskProcessor& task_processor, Function&& f, Args&&... args) {
40 return impl::MakeTaskWithResult<TaskWithResult>(
41 impl::TaskConfig{
42 .task_processor = &task_processor,
43 .inherited_variables_priority = TaskInheritedVariablePriority::kNone,
44 },
45 std::forward<Function>(f),
46 std::forward<Args>(args)...
47 );
48}
49
50/// @overload
51template <typename Function, typename... Args>
52[[nodiscard]] auto AsyncNoTracing(Function&& f, Args&&... args) {
53 return impl::MakeTaskWithResult<TaskWithResult>(
54 impl::TaskConfig{.inherited_variables_priority = TaskInheritedVariablePriority::kNone},
55 std::forward<Function>(f),
56 std::forward<Args>(args)...
57 );
58}
59
60/// @overload
61/// @see Task::Importance::Critical
62template <typename Function, typename... Args>
63[[nodiscard]] auto CriticalAsyncNoTracing(TaskProcessor& task_processor, Function&& f, Args&&... args) {
64 return impl::MakeTaskWithResult<TaskWithResult>(
65 impl::TaskConfig{
66 .task_processor = &task_processor,
67 .importance = Task::Importance::kCritical,
68 .inherited_variables_priority = TaskInheritedVariablePriority::kNone,
69 },
70 std::forward<Function>(f),
71 std::forward<Args>(args)...
72 );
73}
74
75/// @overload
76/// @see Task::Importance::Critical
77template <typename Function, typename... Args>
78[[nodiscard]] auto CriticalAsyncNoTracing(Function&& f, Args&&... args) {
79 return impl::MakeTaskWithResult<TaskWithResult>(
80 impl::TaskConfig{
81 .importance = Task::Importance::kCritical,
82 .inherited_variables_priority = TaskInheritedVariablePriority::kNone,
83 },
84 std::forward<Function>(f),
85 std::forward<Args>(args)...
86 );
87}
88
89#ifndef ARCADIA_ROOT
90
91/// @deprecated Use @ref engine::AsyncNoTracing instead.
92template <typename Function, typename... Args>
93[[nodiscard, deprecated("Use AsyncNoTracing instead")]] auto AsyncNoSpan(
94 TaskProcessor& task_processor,
95 Function&& f,
96 Args&&... args
97) {
98 return AsyncNoTracing(task_processor, std::forward<Function>(f), std::forward<Args>(args)...);
99}
100
101/// @deprecated Use @ref engine::AsyncNoTracing instead.
102template <typename Function, typename... Args>
103[[nodiscard, deprecated("Use AsyncNoTracing instead")]] auto AsyncNoSpan(Function&& f, Args&&... args) {
104 return AsyncNoTracing(std::forward<Function>(f), std::forward<Args>(args)...);
105}
106
107#endif
108
109} // namespace engine
110
111USERVER_NAMESPACE_END