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{.task_processor = &task_processor},
42 std::forward<Function>(f),
43 std::forward<Args>(args)...
44 );
45}
46
47/// @overload
48template <typename Function, typename... Args>
49[[nodiscard]] auto AsyncNoTracing(Function&& f, Args&&... args) {
50 return impl::MakeTaskWithResult<
51 TaskWithResult>(impl::TaskConfig{}, std::forward<Function>(f), std::forward<Args>(args)...);
52}
53
54/// @overload
55/// @see Task::Importance::Critical
56template <typename Function, typename... Args>
57[[nodiscard]] auto CriticalAsyncNoTracing(TaskProcessor& task_processor, Function&& f, Args&&... args) {
58 return impl::MakeTaskWithResult<TaskWithResult>(
59 impl::TaskConfig{
60 .task_processor = &task_processor,
61 .importance = Task::Importance::kCritical,
62 },
63 std::forward<Function>(f),
64 std::forward<Args>(args)...
65 );
66}
67
68/// @overload
69/// @see Task::Importance::Critical
70template <typename Function, typename... Args>
71[[nodiscard]] auto CriticalAsyncNoTracing(Function&& f, Args&&... args) {
72 return impl::MakeTaskWithResult<TaskWithResult>(
73 impl::TaskConfig{.importance = Task::Importance::kCritical},
74 std::forward<Function>(f),
75 std::forward<Args>(args)...
76 );
77}
78
79#ifndef ARCADIA_ROOT
80
81/// @deprecated Use @ref engine::AsyncNoTracing instead.
82template <typename Function, typename... Args>
83[[nodiscard, deprecated("Use AsyncNoTracing instead")]] auto AsyncNoSpan(
84 TaskProcessor& task_processor,
85 Function&& f,
86 Args&&... args
87) {
88 return AsyncNoTracing(task_processor, std::forward<Function>(f), std::forward<Args>(args)...);
89}
90
91/// @deprecated Use @ref engine::AsyncNoTracing instead.
92template <typename Function, typename... Args>
93[[nodiscard, deprecated("Use AsyncNoTracing instead")]] auto AsyncNoSpan(Function&& f, Args&&... args) {
94 return AsyncNoTracing(std::forward<Function>(f), std::forward<Args>(args)...);
95}
96
97#endif
98
99} // namespace engine
100
101USERVER_NAMESPACE_END