@@ -5,13 +5,13 @@ use chrono::{DateTime, Local};
55use serde:: { Deserialize , Serialize } ;
66use serde_json:: json;
77use std:: collections:: HashMap ;
8- use std:: { fs, io} ;
98use std:: fs:: { File , read_to_string} ;
109use std:: io:: { Error , ErrorKind , Write } ;
1110use std:: path:: PathBuf ;
1211use std:: process:: { Child , Command , Stdio } ;
1312use std:: sync:: Arc ;
1413use std:: time:: Duration ;
14+ use std:: { fs, io} ;
1515use tokio:: sync:: RwLock ;
1616use tokio:: time:: timeout;
1717use uuid:: Uuid ;
@@ -22,12 +22,12 @@ use crate::core::task::Task;
2222use crate :: node_api:: node_service:: ServiceInfoResponse ;
2323use crate :: sys_config:: cloud_config:: CloudConfig ;
2424use crate :: sys_config:: software_config:: SoftwareName ;
25+ use crate :: utils:: error:: CloudError ;
26+ use crate :: utils:: error_kind:: CloudErrorKind :: * ;
2527use crate :: utils:: logger:: Logger ;
2628use crate :: utils:: service_status:: ServiceStatus ;
2729use crate :: utils:: utils:: Utils ;
2830use crate :: { error, log_error, log_info, log_warning} ;
29- use crate :: utils:: error:: CloudError ;
30- use crate :: utils:: error_kind:: CloudErrorKind :: * ;
3131
3232#[ derive( Serialize ) ]
3333struct RegisterServerData {
@@ -56,7 +56,7 @@ impl Service {
5656 & CloudConfig :: get ( ) . get_server_host ( ) ,
5757 & task. get_start_port ( ) ,
5858 ) ) {
59- Some ( port ) => port,
59+ Some ( port) => port,
6060 None => return Err ( error ! ( NextFreePortNotFound ) ) ,
6161 } ;
6262 let server_address = Address :: new ( & CloudConfig :: get ( ) . get_server_host ( ) , & port) ;
@@ -192,7 +192,8 @@ impl Service {
192192 return Err ( error ! ( CantFindIPConfigFilePath ) ) ;
193193 }
194194
195- let file_content_ip = read_to_string ( & path_ip) . map_err ( |e| error ! ( CantReadFileToString , e) ) ?;
195+ let file_content_ip =
196+ read_to_string ( & path_ip) . map_err ( |e| error ! ( CantReadFileToString , e) ) ?;
196197 let edit_file_ip = file_content_ip. replace ( "%ip%" , & * address. get_ip ( ) ) ;
197198 fs:: write ( & path_ip, edit_file_ip) . map_err ( |e| error ! ( CantWriteIP , e) ) ?;
198199
@@ -203,7 +204,8 @@ impl Service {
203204 return Err ( error ! ( CantFindPortConfigFilePath ) ) ;
204205 }
205206
206- let file_content_port = read_to_string ( & path_port) . map_err ( |e| error ! ( CantReadFileToString , e) ) ?;
207+ let file_content_port =
208+ read_to_string ( & path_port) . map_err ( |e| error ! ( CantReadFileToString , e) ) ?;
207209 let edit_file_port =
208210 file_content_port. replace ( "%port%" , address. get_port ( ) . to_string ( ) . as_str ( ) ) ;
209211 fs:: write ( & path_port, edit_file_port) . map_err ( |e| error ! ( CantWritePort , e) ) ?;
@@ -264,10 +266,10 @@ impl Service {
264266 }
265267 Err ( e) => {
266268 log_error ! (
267- "Stop command nicht senden an {} \n Error: {}" ,
268- self . get_name( ) ,
269- e. to_string( )
270- ) ;
269+ "Stop command nicht senden an {} \n Error: {}" ,
270+ self . get_name( ) ,
271+ e. to_string( )
272+ ) ;
271273
272274 if self . get_process ( ) . is_none ( ) {
273275 self . set_status ( ServiceStatus :: Stop ) ;
@@ -283,19 +285,18 @@ impl Service {
283285 Err ( ..) => log_warning ! ( "Service konnte nicht gekillt werden" ) ,
284286 }
285287 }
286-
288+
287289 if self . is_delete ( ) {
288- self . delete_files ( ) ;
290+ self . delete_files ( ) ;
289291 } else {
290292 self . set_status ( ServiceStatus :: Stop ) ;
291- self . save_to_file ( ) ;
293+ self . save_to_file ( ) ;
292294 }
293-
294295 } else {
295296 // TODO: Remote/Cluster shutdown
296297 }
297298 }
298-
299+
299300 pub fn is_delete ( & self ) -> bool {
300301 !self . get_task ( ) . is_static_service ( ) && self . get_task ( ) . is_delete_on_stop ( )
301302 }
@@ -502,23 +503,22 @@ impl Service {
502503
503504 match url. post ( & body, Duration :: from_secs ( 3 ) ) . await {
504505 Ok ( _) => log_info ! (
505- "Service {} successfully connected to Proxy [{}]" ,
506- self . get_name( ) ,
506+ "Service {} successfully connected to Proxy [{}]" ,
507+ self . get_name( ) ,
507508 service_proxy. get_name( )
508- ) ,
509+ ) ,
509510 Err ( e) => log_warning ! (
510- "Service | {} | can't send request connect to Network \n Error: {}" ,
511- self . get_name( ) ,
512- e. to_string( )
513- ) ,
511+ "Service | {} | can't send request connect to Network \n Error: {}" ,
512+ self . get_name( ) ,
513+ e. to_string( )
514+ ) ,
514515 }
515516 }
516517
517518 // TODO: Send New Started Service To Cluster
518519 Ok ( ( ) )
519520 }
520521
521-
522522 pub async fn disconnect_from_network ( & self , cloud : Arc < RwLock < Cloud > > ) -> Result < ( ) , Error > {
523523 if self . is_proxy ( ) {
524524 return Ok ( ( ) ) ;
@@ -552,13 +552,15 @@ impl Service {
552552 self . prepare_to_start ( ) ?;
553553 let server_file_path = match self . get_path_with_server_file ( ) . to_str ( ) {
554554 Some ( server_file_path) => server_file_path. to_string ( ) ,
555- None => return Err ( error ! ( CantConvertServerFilePathToString ) )
555+ None => return Err ( error ! ( CantConvertServerFilePathToString ) ) ,
556556 } ;
557557
558558 let software_name = self . get_software_name ( ) ;
559559 let mut placeholders = HashMap :: new ( ) ;
560- let stdout_file = File :: create ( self . get_path_stdout_file ( ) ) . map_err ( |e| error ! ( CantCreateSTDOUTFile , e) ) ?;
561- let stderr_file = File :: create ( self . get_path_stderr_file ( ) ) . map_err ( |e| error ! ( CantCreateSTDERRFile , e) ) ?;
560+ let stdout_file = File :: create ( self . get_path_stdout_file ( ) )
561+ . map_err ( |e| error ! ( CantCreateSTDOUTFile , e) ) ?;
562+ let stderr_file = File :: create ( self . get_path_stderr_file ( ) )
563+ . map_err ( |e| error ! ( CantCreateSTDERRFile , e) ) ?;
562564
563565 placeholders. insert ( "ip" , self . get_server_address ( ) . get_ip ( ) . to_string ( ) ) ;
564566 placeholders. insert ( "port" , self . get_server_address ( ) . get_port ( ) . to_string ( ) ) ;
@@ -576,7 +578,8 @@ impl Service {
576578 . stdout ( Stdio :: from ( stdout_file) )
577579 . stderr ( Stdio :: from ( stderr_file) )
578580 . stdin ( Stdio :: piped ( ) )
579- . spawn ( ) . map_err ( |e| error ! ( CantStartServer , e) ) ?;
581+ . spawn ( )
582+ . map_err ( |e| error ! ( CantStartServer , e) ) ?;
580583
581584 self . set_process ( Some ( child) ) ;
582585 Ok ( self )
@@ -635,7 +638,8 @@ impl Service {
635638 . join ( self . get_task ( ) . get_software ( ) . get_software_type ( ) )
636639 . join ( self . get_task ( ) . get_software ( ) . get_name ( ) ) ;
637640
638- Directory :: copy_folder_contents ( & software_lib_path, & self . get_path ( ) ) . map_err ( |e| error ! ( Internal , e) )
641+ Directory :: copy_folder_contents ( & software_lib_path, & self . get_path ( ) )
642+ . map_err ( |e| error ! ( Internal , e) )
639643 }
640644
641645 pub fn is_proxy ( & self ) -> bool {
@@ -651,7 +655,8 @@ impl Service {
651655
652656fn find_port ( ports : Vec < u32 > , mut port : u32 , server_host : & String ) -> u32 {
653657 while ports. contains ( & port) || !Address :: is_port_available ( & Address :: new ( & server_host, & port) ) {
654- port = Address :: find_next_port ( & mut Address :: new ( & server_host, & ( port + 1 ) ) ) . unwrap_or_else ( || 0 ) ;
658+ port = Address :: find_next_port ( & mut Address :: new ( & server_host, & ( port + 1 ) ) )
659+ . unwrap_or_else ( || 0 ) ;
655660 }
656661 port
657662}
0 commit comments