1
+ use crate :: env_reader:: env_var_exists;
1
2
use log:: * ;
3
+ use std:: path:: Path ;
2
4
use std:: time:: Duration ;
3
5
4
6
pub mod env_reader;
5
7
pub mod sleeper;
6
8
7
9
pub struct Config {
8
10
pub hosts : String ,
11
+ pub paths : String ,
9
12
pub global_timeout : u64 ,
10
13
pub tcp_connection_timeout : u64 ,
11
14
pub wait_before : u64 ,
@@ -25,6 +28,7 @@ pub fn wait(
25
28
info ! ( "---------------------------" ) ;
26
29
debug ! ( "Starting with configuration:" ) ;
27
30
debug ! ( " - Hosts to be waiting for: [{}]" , config. hosts) ;
31
+ debug ! ( " - Paths to be waiting for: [{}]" , config. paths) ;
28
32
debug ! (
29
33
" - Timeout before failure: {} seconds " ,
30
34
config. global_timeout
@@ -34,11 +38,11 @@ pub fn wait(
34
38
config. tcp_connection_timeout
35
39
) ;
36
40
debug ! (
37
- " - Sleeping time before checking for hosts availability: {} seconds" ,
41
+ " - Sleeping time before checking for hosts/paths availability: {} seconds" ,
38
42
config. wait_before
39
43
) ;
40
44
debug ! (
41
- " - Sleeping time once all hosts are available: {} seconds" ,
45
+ " - Sleeping time once all hosts/paths are available: {} seconds" ,
42
46
config. wait_after
43
47
) ;
44
48
debug ! (
@@ -49,22 +53,23 @@ pub fn wait(
49
53
50
54
if config. wait_before > 0 {
51
55
info ! (
52
- "Waiting {} seconds before checking for hosts availability" ,
56
+ "Waiting {} seconds before checking for hosts/paths availability" ,
53
57
config. wait_before
54
58
) ;
55
59
info ! ( "{}" , LINE_SEPARATOR ) ;
56
60
sleep. sleep ( config. wait_before ) ;
57
61
}
58
62
63
+ sleep. reset ( ) ;
64
+
59
65
if !config. hosts . trim ( ) . is_empty ( ) {
60
- sleep. reset ( ) ;
61
66
for host in config. hosts . trim ( ) . split ( ',' ) {
62
- info ! ( "Checking availability of {} " , host) ;
67
+ info ! ( "Checking availability of host [{}] " , host) ;
63
68
while !port_check:: is_port_reachable_with_timeout (
64
69
& host. trim ( ) . to_string ( ) ,
65
70
Duration :: from_secs ( config. tcp_connection_timeout ) ,
66
71
) {
67
- info ! ( "Host {} not yet available..." , host) ;
72
+ info ! ( "Host [{}] not yet available..." , host) ;
68
73
if sleep. elapsed ( config. global_timeout ) {
69
74
error ! (
70
75
"Timeout! After {} seconds some hosts are still not reachable" ,
@@ -75,14 +80,34 @@ pub fn wait(
75
80
}
76
81
sleep. sleep ( config. wait_sleep_interval ) ;
77
82
}
78
- info ! ( "Host {} is now available!" , host) ;
83
+ info ! ( "Host [{}] is now available!" , host) ;
84
+ info ! ( "{}" , LINE_SEPARATOR ) ;
85
+ }
86
+ }
87
+
88
+ if !config. paths . trim ( ) . is_empty ( ) {
89
+ for path in config. paths . trim ( ) . split ( ',' ) {
90
+ info ! ( "Checking availability of path [{}]" , path) ;
91
+ while !Path :: new ( path. trim ( ) ) . exists ( ) {
92
+ info ! ( "Path {} not yet available..." , path) ;
93
+ if sleep. elapsed ( config. global_timeout ) {
94
+ error ! (
95
+ "Timeout! After [{}] seconds some paths are still not reachable" ,
96
+ config. global_timeout
97
+ ) ;
98
+ on_timeout ( ) ;
99
+ return ;
100
+ }
101
+ sleep. sleep ( config. wait_sleep_interval ) ;
102
+ }
103
+ info ! ( "Path [{}] is now available!" , path) ;
79
104
info ! ( "{}" , LINE_SEPARATOR ) ;
80
105
}
81
106
}
82
107
83
108
if config. wait_after > 0 {
84
109
info ! (
85
- "Waiting {} seconds after hosts availability" ,
110
+ "Waiting {} seconds after hosts/paths availability" ,
86
111
config. wait_after
87
112
) ;
88
113
info ! ( "{}" , LINE_SEPARATOR ) ;
@@ -95,30 +120,35 @@ pub fn wait(
95
120
96
121
pub fn config_from_env ( ) -> Config {
97
122
Config {
98
- hosts : crate :: env_reader:: env_var ( & "WAIT_HOSTS" . to_string ( ) , "" . to_string ( ) ) ,
99
- global_timeout : to_int (
100
- & crate :: env_reader:: env_var ( & "WAIT_HOSTS_TIMEOUT" . to_string ( ) , "" . to_string ( ) ) ,
101
- 30 ,
102
- ) ,
123
+ hosts : crate :: env_reader:: env_var ( "WAIT_HOSTS" , "" . to_string ( ) ) ,
124
+ paths : crate :: env_reader:: env_var ( "WAIT_PATHS" , "" . to_string ( ) ) ,
125
+ global_timeout : to_int ( & legacy_or_new ( "WAIT_HOSTS_TIMEOUT" , "WAIT_TIMEOUT" , "" ) , 30 ) ,
103
126
tcp_connection_timeout : to_int (
104
- & crate :: env_reader:: env_var ( & "WAIT_HOST_CONNECT_TIMEOUT" . to_string ( ) , "" . to_string ( ) ) ,
127
+ & crate :: env_reader:: env_var ( "WAIT_HOST_CONNECT_TIMEOUT" , "" . to_string ( ) ) ,
105
128
5 ,
106
129
) ,
107
- wait_before : to_int (
108
- & crate :: env_reader:: env_var ( & "WAIT_BEFORE_HOSTS" . to_string ( ) , "" . to_string ( ) ) ,
109
- 0 ,
110
- ) ,
111
- wait_after : to_int (
112
- & crate :: env_reader:: env_var ( & "WAIT_AFTER_HOSTS" . to_string ( ) , "" . to_string ( ) ) ,
113
- 0 ,
114
- ) ,
130
+ wait_before : to_int ( & legacy_or_new ( "WAIT_BEFORE_HOSTS" , "WAIT_BEFORE" , "" ) , 0 ) ,
131
+ wait_after : to_int ( & legacy_or_new ( "WAIT_AFTER_HOSTS" , "WAIT_AFTER" , "" ) , 0 ) ,
115
132
wait_sleep_interval : to_int (
116
- & crate :: env_reader:: env_var ( & "WAIT_SLEEP_INTERVAL" . to_string ( ) , "" . to_string ( ) ) ,
133
+ & crate :: env_reader:: env_var ( "WAIT_SLEEP_INTERVAL" , "" . to_string ( ) ) ,
117
134
1 ,
118
135
) ,
119
136
}
120
137
}
121
138
139
+ fn legacy_or_new ( legacy_var_name : & str , var_name : & str , default : & str ) -> String {
140
+ let mut temp_value = default. to_string ( ) ;
141
+ if env_var_exists ( legacy_var_name) {
142
+ warn ! (
143
+ "Environment variable [{}] is deprecated. Use [{}] instead." ,
144
+ legacy_var_name, var_name
145
+ ) ;
146
+ temp_value = crate :: env_reader:: env_var ( legacy_var_name, temp_value) ;
147
+ }
148
+ temp_value = crate :: env_reader:: env_var ( var_name, temp_value) ;
149
+ temp_value
150
+ }
151
+
122
152
fn to_int ( number : & str , default : u64 ) -> u64 {
123
153
match number. parse :: < u64 > ( ) {
124
154
Ok ( value) => value,
0 commit comments