@@ -19,8 +19,9 @@ use std::future::Future;
1919use crate :: cube_ext:: catch_unwind:: {
2020 async_try_with_catch_unwind, try_with_catch_unwind, PanicError ,
2121} ;
22+ use datafusion_common_runtime:: SpawnedTask ;
2223use futures:: sink:: SinkExt ;
23- use tokio:: task:: JoinHandle ;
24+ use tokio:: task:: { AbortHandle , JoinHandle , JoinSet } ;
2425use tracing_futures:: Instrument ;
2526
2627/// Calls [tokio::spawn] and additionally enables tracing of the spawned task as part of the current
5758 }
5859}
5960
61+ /// Propagates current span to the spawned task. See [spawn] for details.
62+ pub fn spawn_on_joinset < F , T > ( join_set : & mut JoinSet < T > , task : F ) -> AbortHandle
63+ where
64+ F : Future < Output = T > ,
65+ F : Send + ' static ,
66+ T : Send + ' static ,
67+ {
68+ if let Some ( s) = new_subtask_span ( ) {
69+ join_set. spawn ( async move {
70+ let _p = s. parent ; // ensure parent stays alive.
71+ task. instrument ( s. child ) . await
72+ } )
73+ } else {
74+ join_set. spawn ( task)
75+ }
76+ }
77+
78+ /// Propagates current span to the blocking operation. See [spawn] for details.
79+ pub fn spawn_blocking_on_joinset < F , T > ( join_set : & mut JoinSet < T > , f : F ) -> AbortHandle
80+ where
81+ F : FnOnce ( ) -> T ,
82+ F : Send + ' static ,
83+ T : Send + ' static ,
84+ {
85+ if let Some ( s) = new_subtask_span ( ) {
86+ join_set. spawn_blocking ( move || {
87+ let _p = s. parent ; // ensure parent stays alive.
88+ s. child . in_scope ( f)
89+ } )
90+ } else {
91+ join_set. spawn_blocking ( f)
92+ }
93+ }
94+
95+ /// Propagates current span to the spawned task. See [spawn] for details.
96+ pub fn spawn_spawned_task < T > ( task : T ) -> SpawnedTask < T :: Output >
97+ where
98+ T : Future + Send + ' static ,
99+ T :: Output : Send + ' static ,
100+ {
101+ if let Some ( s) = new_subtask_span ( ) {
102+ SpawnedTask :: spawn ( async move {
103+ let _p = s. parent ; // ensure parent stays alive.
104+ task. instrument ( s. child ) . await
105+ } )
106+ } else {
107+ SpawnedTask :: spawn ( task)
108+ }
109+ }
110+
60111struct SpawnSpans {
61112 parent : tracing:: Span ,
62113 child : tracing:: Span ,
0 commit comments