6#include <userver/compiler/impl/lifetime.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_with_result.hpp>
10#include <userver/utils/impl/span_wrap_call.hpp>
12USERVER_NAMESPACE_BEGIN
18struct TaskBuilderOptions
final {
19 engine::TaskProcessor* task_processor{
nullptr};
21 engine::Deadline deadline;
22 bool inherit_variables{
true};
25struct TaskBuilderWithoutSelectedSpanOptions
final {};
27struct TaskBuilderWithSpanOptions
final {
28 std::string span_name;
31struct TaskBuilderHideSpanOptions
final {};
33struct TaskBuilderNoTracingOptions
final {};
35template <
typename Task>
36engine::
impl::TaskConfig MakeTaskConfig(
const TaskBuilderOptions& options) {
38 .task_processor = options.task_processor,
39 .importance = options.importance,
40 .wait_mode = Task::kWaitMode,
41 .deadline = options.deadline,
42 .inherited_variables_priority =
43 options.inherit_variables
49template <
typename Task,
typename Function,
typename... Args>
51 const TaskBuilderOptions&,
52 const TaskBuilderWithoutSelectedSpanOptions&,
53 const utils::impl::SourceLocation&,
59 "Exactly one of the following methods of TaskBuilder must be called: SpanName(), NoTracing(), HideSpan()"
63template <
typename Task,
typename Function,
typename... Args>
65 const TaskBuilderOptions& options,
66 const TaskBuilderWithSpanOptions& options_ext,
67 const utils::impl::SourceLocation& source_location,
71 return Task{engine::
impl::MakeTask(
72 impl::MakeTaskConfig<Task>(options),
73 utils::impl::SpanLazyPrvalue(
74 std::string{options_ext.span_name},
75 utils::impl::SpanWrapCall::HideSpan::kNo,
78 std::forward<Function>(f),
79 std::forward<Args>(args)...
83template <
typename Task,
typename Function,
typename... Args>
85 const TaskBuilderOptions& options,
86 const TaskBuilderHideSpanOptions&,
87 const utils::impl::SourceLocation& source_location,
91 return Task{engine::
impl::MakeTask(
92 impl::MakeTaskConfig<Task>(options),
93 utils::impl::SpanLazyPrvalue(std::string{},
utils::impl::SpanWrapCall::HideSpan::kYes, source_location),
94 std::forward<Function>(f),
95 std::forward<Args>(args)...
99template <
typename Task,
typename Function,
typename... Args>
101 const TaskBuilderOptions& options,
102 const TaskBuilderNoTracingOptions&,
103 const utils::impl::SourceLocation&,
108 UINVARIANT(!options.inherit_variables,
"Task-inherited variables without span are not supported at the moment");
110 auto config = impl::MakeTaskConfig<Task>(options);
113 return Task{engine::
impl::MakeTask(std::move(config), std::forward<Function>(f), std::forward<Args>(args)...)};
118template <
typename OptionsImpl>
153template <
typename OptionsImpl>
170 return TaskBuilderWithSpan{*
this, impl::TaskBuilderWithSpanOptions{.span_name = std::move(name)}};
203 options_.task_processor = &tp;
211 options_.deadline = deadline;
219 options_.inherit_variables =
false;
230 template <
typename Function,
typename... Args>
231 [[nodiscard]]
auto Build(Function&& f, Args&&... args)
const;
240 template <
typename Function,
typename... Args>
241 [[nodiscard]]
auto BuildShared(Function&& f, Args&&... args)
const;
244 template <
typename OtherOptions>
247 template <
typename OtherOptions>
248 TaskBuilder(
const TaskBuilder<OtherOptions>& other, OptionsImpl&& options_ext)
249 : options_(other.options_),
250 options_ext_(std::move(options_ext))
253 impl::TaskBuilderOptions options_{};
261template <
typename Function,
typename... Args>
263 using Task = engine::
TaskWithResult<std::invoke_result_t<Function, Args...>>;
264 return impl::BuildTask<Task>(
267 utils::impl::SourceLocation::Current(),
268 std::forward<Function>(f),
269 std::forward<Args>(args)...
274template <
typename Function,
typename... Args>
277 return impl::BuildTask<Task>(
280 utils::impl::SourceLocation::Current(),
281 std::forward<Function>(f),
282 std::forward<Args>(args)...
286extern template class TaskBuilder<impl::TaskBuilderWithoutSelectedSpanOptions>;
287extern template class TaskBuilder<impl::TaskBuilderWithSpanOptions>;
288extern template class TaskBuilder<impl::TaskBuilderHideSpanOptions>;
289extern template class TaskBuilder<impl::TaskBuilderNoTracingOptions>;