1313// See the License for the specific language governing permissions and
1414// limitations under the License.
1515
16- use async_std:: future:: timeout;
1716use minio:: s3:: Client ;
1817
19- use std:: thread;
20-
2118/// Cleanup guard that removes the bucket when it is dropped
2219pub struct CleanupGuard {
2320 client : Client ,
@@ -32,41 +29,26 @@ impl CleanupGuard {
3229 bucket_name : bucket_name. into ( ) ,
3330 }
3431 }
35- }
36-
37- impl Drop for CleanupGuard {
38- fn drop ( & mut self ) {
39- let client = self . client . clone ( ) ;
40- let bucket_name = self . bucket_name . clone ( ) ;
41- //println!("Going to remove bucket {}", bucket_name);
4232
43- // Spawn the cleanup task in a way that detaches it from the current runtime
44- thread :: spawn ( move || {
45- // Create a new runtime for this thread
46- let rt = tokio :: runtime :: Runtime :: new ( ) . unwrap ( ) ;
33+ pub async fn cleanup ( & self ) {
34+ cleanup ( self . client . clone ( ) , & self . bucket_name ) . await ;
35+ }
36+ }
4737
48- // Execute the async cleanup in this new runtime
49- rt. block_on ( async {
50- // do the actual removal of the bucket
51- match timeout (
52- std:: time:: Duration :: from_secs ( 60 ) ,
53- client. delete_and_purge_bucket ( & bucket_name) ,
54- )
55- . await
56- {
57- Ok ( result) => match result {
38+ pub async fn cleanup ( client : Client , bucket_name : & str ) {
39+ tokio:: select!(
40+ _ = tokio:: time:: sleep( std:: time:: Duration :: from_secs( 60 ) ) => {
41+ eprintln!( "Cleanup timeout after 60s while removing bucket {}" , bucket_name) ;
42+ } ,
43+ outcome = client. delete_and_purge_bucket( bucket_name) => {
44+ match outcome {
5845 Ok ( _) => {
59- //println !("Bucket {} removed successfully", bucket_name),
46+ eprintln !( "Bucket {} removed successfully" , bucket_name) ;
6047 }
61- Err ( _e ) => {
62- //println !("Error removing bucket {}: {:?}", bucket_name, e)
48+ Err ( e ) => {
49+ eprintln !( "Error removing bucket {}: {:?}" , bucket_name, e) ;
6350 }
64- } ,
65- Err ( _) => println ! ( "Timeout after 60s while removing bucket {}" , bucket_name) ,
51+ }
6652 }
67- } ) ;
68- } )
69- . join ( )
70- . unwrap ( ) ; // This blocks the current thread until cleanup is done
71- }
53+ ) ;
7254}
0 commit comments