1
1
use anyhow:: Result ;
2
2
3
- async fn run ( ) -> Result < ( ) > {
3
+ /// The code called after we've done process global init and created
4
+ /// an async runtime.
5
+ async fn async_main ( ) -> Result < ( ) > {
4
6
// Don't include timestamps and such because they're not really useful and
5
7
// too verbose, and plus several log targets such as journald will already
6
8
// include timestamps.
@@ -15,12 +17,33 @@ async fn run() -> Result<()> {
15
17
. with_writer ( std:: io:: stderr)
16
18
. init ( ) ;
17
19
tracing:: trace!( "starting" ) ;
20
+ // As you can see, the role of this file is mostly to just be a shim
21
+ // to call into the code that lives in the internal shared library.
18
22
bootc_lib:: cli:: run_from_iter ( std:: env:: args ( ) ) . await
19
23
}
20
24
21
- #[ tokio:: main( flavor = "current_thread" ) ]
22
- async fn main ( ) {
23
- if let Err ( e) = run ( ) . await {
25
+ /// Perform process global initialization, then create an async runtime
26
+ /// and do the rest of the work there.
27
+ fn run ( ) -> Result < ( ) > {
28
+ // Initialize global state before we've possibly created other threads, etc.
29
+ bootc_lib:: cli:: global_init ( ) ?;
30
+ // We only use the "current thread" runtime because we don't perform
31
+ // a lot of CPU heavy work in async tasks. Where we do work on the CPU,
32
+ // or we do want explicit concurrency, we typically use
33
+ // tokio::task::spawn_blocking to create a new OS thread explicitly.
34
+ let runtime = tokio:: runtime:: Builder :: new_current_thread ( )
35
+ . enable_all ( )
36
+ . build ( )
37
+ . expect ( "Failed to build tokio runtime" ) ;
38
+ // And invoke the async_main
39
+ runtime. block_on ( async move { async_main ( ) . await } )
40
+ }
41
+
42
+ fn main ( ) {
43
+ // In order to print the error in a custom format (with :#) our
44
+ // main simply invokes a run() where all the work is done.
45
+ // This code just captures any errors.
46
+ if let Err ( e) = run ( ) {
24
47
tracing:: error!( "{:#}" , e) ;
25
48
std:: process:: exit ( 1 ) ;
26
49
}
0 commit comments