@@ -42,7 +42,6 @@ static nrh_status_codes_t health_statuses[NRH_MAX_STATUS] = {
42
42
};
43
43
// clang-format on
44
44
45
- static int healthfile_fd = -1 ;
46
45
static struct timespec start_time = {0 , 0 };
47
46
static nrhealth_t last_error_code = NRH_HEALTHY ;
48
47
static char health_filename [] = "health-bc21b5891f5e44fc9272caef924611a8.yml" ;
@@ -145,15 +144,6 @@ char* nrh_get_health_filepath(char* filedir) {
145
144
return filepath ;
146
145
}
147
146
148
- void nrh_close_health_file (void ) {
149
- if (-1 == healthfile_fd ) {
150
- return ;
151
- }
152
-
153
- nr_close (healthfile_fd );
154
- healthfile_fd = -1 ;
155
- }
156
-
157
147
nr_status_t nrh_set_start_time (void ) {
158
148
clock_gettime (CLOCK_REALTIME , & start_time );
159
149
@@ -176,10 +166,6 @@ long long nrh_get_current_time_ns(void) {
176
166
return (long long )(ts .tv_sec * BILLION + ts .tv_nsec );
177
167
}
178
168
179
- int nrh_get_healthfile_fd (void ) {
180
- return healthfile_fd ;
181
- }
182
-
183
169
nr_status_t nrh_set_last_error (nrhealth_t status ) {
184
170
if (status < NRH_HEALTHY || status >= NRH_MAX_STATUS ) {
185
171
return NR_FAILURE ;
@@ -198,32 +184,32 @@ nrhealth_t nrh_get_last_error(void) {
198
184
return last_error_code ;
199
185
}
200
186
201
- #ifndef HEALTH_STATUS_LINE
202
- #define HEALTH_STATUS_LINE (field , value ) \
203
- nr_write(healthfile_fd, nr_formatf("%s: %s\n", field, value), \
204
- nr_strlen(nr_formatf("%s: %s\n", field, value)));
205
-
206
- nr_status_t nrh_write_health (void ) {
187
+ nr_status_t nrh_write_health (char * uri ) {
207
188
nrhealth_t status = last_error_code ;
189
+ FILE * fp = NULL ;
190
+ char * filepath = NULL ;
208
191
209
- if (-1 == healthfile_fd ) {
210
- // healthfile not initialized
192
+ if (NULL == uri ) {
193
+ // invalid uri
211
194
return NR_FAILURE ;
212
195
}
213
196
214
- HEALTH_STATUS_LINE ("healthy" , NRH_HEALTHY == status ? "true" : "false" );
215
-
216
- HEALTH_STATUS_LINE ("status" , health_statuses [status ].description );
197
+ filepath = nrh_get_health_filepath (uri );
217
198
218
- HEALTH_STATUS_LINE ("last_error_code" , health_statuses [status ].code );
199
+ fp = fopen (filepath , "w" );
200
+ if (NULL == fp ) {
201
+ // unable to open healthfile
202
+ return NR_FAILURE ;
203
+ }
219
204
220
- HEALTH_STATUS_LINE ("status_time_unix_nano" ,
221
- nr_formatf ("%lld" , nrh_get_current_time_ns ()));
205
+ fprintf (fp , "healthy: %s\n" , NRH_HEALTHY == status ? "true" : "false" );
206
+ fprintf (fp , "status: %s\n" , health_statuses [status ].description );
207
+ fprintf (fp , "last_error_code: %s\n" , health_statuses [status ].code );
208
+ fprintf (fp , "status_time_unix_nano: %lld\n" , nrh_get_current_time_ns ());
209
+ fprintf (fp , "status_time_unix_nano: %lld\n" , nrh_get_start_time_ns ());
222
210
223
- HEALTH_STATUS_LINE ( "start_time_unix_nano" ,
224
- nr_formatf ( "%lld" , nrh_get_start_time_ns ()) );
211
+ fclose ( fp );
212
+ nr_free ( filepath );
225
213
226
214
return NR_SUCCESS ;
227
215
}
228
- #undef HEALTH_STATUS_LINE
229
- #endif
0 commit comments