@@ -11,17 +11,18 @@ use crate::common::asserts::GracefulUnwrap;
1111use crate :: common:: consts:: {
1212 FINISH_RESULT_FAILED , FINISH_RESULT_START_FAILED , FINISH_RESULT_SUCCESS ,
1313 FINISH_RESULT_TERMINATED , FINISH_RESULT_TIMEOUT , METADATA_API , OUTPUT_BYTE_LIMIT_EACH_REPORT ,
14- TASK_LOG_PATH , TASK_STORE_PATH ,
14+ TASK_STORE_PATH ,
1515} ;
1616use crate :: cos:: ObjectAPI ;
1717use crate :: cos:: COS ;
1818use crate :: http:: MetadataAPIAdapter ;
1919use crate :: ontime:: timer:: Timer ;
2020use crate :: start_failed_err_info;
2121use async_trait:: async_trait;
22- use log:: { debug, error, info} ;
22+ use log:: { debug, error, info, warn } ;
2323use std:: fmt;
24- use std:: fs:: { File , OpenOptions } ;
24+ use std:: fs:: { create_dir_all, File , OpenOptions } ;
25+ use std:: path:: Path ;
2526use std:: sync:: atomic:: Ordering :: SeqCst ;
2627use std:: sync:: atomic:: { AtomicBool , AtomicU32 , AtomicU64 , Ordering } ;
2728use std:: sync:: { Arc , Mutex } ;
@@ -468,17 +469,32 @@ impl BaseCommand {
468469 start_failed_err_info ! ( ERR_SCRIPT_FILE_STORE_FAILED , TASK_STORE_PATH ) ;
469470 return Err ( ret) ;
470471 }
471- if self . log_file_path . is_empty ( ) {
472- let ret = format ! ( "start fail because log file store failed." ) ;
473- * self . err_info . lock ( ) . unwrap ( ) =
474- start_failed_err_info ! ( ERR_LOG_FILE_STORE_FAILED , TASK_LOG_PATH ) ;
475- return Err ( ret) ;
476- }
477472 Ok ( ( ) )
478473 }
479474
480475 fn open_log_file ( & self ) -> Result < File , String > {
476+ let parent = Path :: new ( self . log_file_path . as_str ( ) ) . parent ( ) ;
477+ match parent {
478+ Some ( parent) => {
479+ if let Err ( e) = create_dir_all ( parent) {
480+ return Err ( format ! (
481+ "fail to open task log file {}: create parent dir fail {}: {:?}" ,
482+ self . log_file_path,
483+ parent. display( ) ,
484+ e
485+ ) ) ;
486+ }
487+ }
488+ None => {
489+ warn ! (
490+ "parent dir not found, skip: {}" ,
491+ self . log_file_path. as_str( )
492+ )
493+ }
494+ }
495+
481496 let res = OpenOptions :: new ( )
497+ . create ( true )
482498 . write ( true )
483499 . open ( self . log_file_path . clone ( ) ) ;
484500 match res {
@@ -596,10 +612,10 @@ impl BaseCommand {
596612}
597613#[ cfg( test) ]
598614mod tests {
615+ use std:: fs;
599616 use std:: fs:: File ;
600617 use std:: io:: Write ;
601618 use std:: time:: { Duration , Instant , SystemTime } ;
602- use std:: fs;
603619
604620 use log:: info;
605621 use rand:: distributions:: Alphanumeric ;
@@ -683,7 +699,6 @@ mod tests {
683699 init_log ( ) ;
684700 // it doesn't matter even if ./a.sh not exist
685701 let log_path = format ! ( "./{}.log" , gen_rand_str( ) ) ;
686- File :: create ( log_path. as_str ( ) ) . unwrap_or_exit ( "create log path fail." ) ;
687702 let ret = new (
688703 CMD_PATH ,
689704 & username ( ) ,
@@ -700,7 +715,7 @@ mod tests {
700715 let ret = cmd. run ( ) . await ;
701716 assert ! ( ret. is_ok( ) ) ;
702717 info ! ( "cmd running, pid:{}" , cmd. pid( ) ) ;
703- tokio:: time:: delay_for ( Duration :: from_secs ( 4 ) ) . await ;
718+ tokio:: time:: delay_for ( Duration :: from_secs ( 10 ) ) . await ;
704719 // now it's NOT a defunct process, cmd will be auto-waited
705720 assert ! ( !is_process_exist( cmd. pid( ) ) . await ) ;
706721 //thread::sleep(Duration::new(10, 0));
@@ -824,7 +839,6 @@ mod tests {
824839 }
825840 }
826841 let log_path = format ! ( "./{}.log" , gen_rand_str( ) ) ;
827- File :: create ( log_path. as_str ( ) ) . unwrap_or_exit ( "create log path fail." ) ;
828842 let ret = new (
829843 filename. as_str ( ) ,
830844 & username ( ) ,
@@ -879,7 +893,6 @@ mod tests {
879893 }
880894 }
881895 let log_path = format ! ( "./{}.log" , gen_rand_str( ) ) ;
882- File :: create ( log_path. as_str ( ) ) . unwrap_or_exit ( "create log path fail." ) ;
883896 let ret = new (
884897 filename. as_str ( ) ,
885898 & username ( ) ,
@@ -965,7 +978,6 @@ mod tests {
965978 }
966979 }
967980 let log_path = format ! ( "./{}.log" , gen_rand_str( ) ) ;
968- File :: create ( log_path. as_str ( ) ) . unwrap_or_exit ( "create log path fail." ) ;
969981 let ret = new (
970982 filename. as_str ( ) ,
971983 & username ( ) ,
@@ -1020,9 +1032,8 @@ mod tests {
10201032 . duration_since ( SystemTime :: UNIX_EPOCH )
10211033 . unwrap ( )
10221034 . as_secs ( ) ;
1023- info ! ( "script {} start_time:{}" , filename, start_time) ;
1035+ info ! ( "script {} start_time:{}" , filename, start_time) ;
10241036 let log_path = format ! ( "./{}.log" , gen_rand_str( ) ) ;
1025- File :: create ( log_path. as_str ( ) ) . unwrap_or_exit ( "create log path fail." ) ;
10261037 let ret = new (
10271038 filename. as_str ( ) ,
10281039 & username ( ) ,
@@ -1076,7 +1087,6 @@ mod tests {
10761087 let filename = format ! ( "./.{}.sh" , gen_rand_str( ) ) ;
10771088 create_file ( "echo 'hello world'\n sleep 10 &\n date" , filename. as_str ( ) ) ;
10781089 let log_path = format ! ( "./{}.log" , gen_rand_str( ) ) ;
1079- File :: create ( log_path. as_str ( ) ) . unwrap_or_exit ( "create log path fail." ) ;
10801090 let ret = new (
10811091 filename. as_str ( ) ,
10821092 & username ( ) ,
@@ -1123,7 +1133,6 @@ mod tests {
11231133 ) ;
11241134
11251135 let log_path = format ! ( "./{}.log" , gen_rand_str( ) ) ;
1126- File :: create ( log_path. as_str ( ) ) . unwrap_or_exit ( "create log path fail." ) ;
11271136 let ret = new (
11281137 filename. as_str ( ) ,
11291138 & username ( ) ,
0 commit comments