mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-02-20 09:26:37 +00:00
Refactor nanosleep(2) calls into sleep_ms() function
This commit is contained in:
parent
83cb027484
commit
d8fd9fa411
@ -700,7 +700,6 @@ int app_server_stop(int argc, const char *const *argv, struct command_line_optio
|
|||||||
int pid, tries, running;
|
int pid, tries, running;
|
||||||
const char *instancepath;
|
const char *instancepath;
|
||||||
time_ms_t timeout;
|
time_ms_t timeout;
|
||||||
struct timespec delay;
|
|
||||||
|
|
||||||
if (cli_arg(argc, argv, o, "instance path", &instancepath, cli_absolute_path, NULL) == -1)
|
if (cli_arg(argc, argv, o, "instance path", &instancepath, cli_absolute_path, NULL) == -1)
|
||||||
return WHY("Unable to determine instance path");
|
return WHY("Unable to determine instance path");
|
||||||
@ -750,11 +749,9 @@ int app_server_stop(int argc, const char *const *argv, struct command_line_optio
|
|||||||
}
|
}
|
||||||
/* Allow a few seconds for the process to die. */
|
/* Allow a few seconds for the process to die. */
|
||||||
timeout = gettime_ms() + 2000;
|
timeout = gettime_ms() + 2000;
|
||||||
do {
|
do
|
||||||
delay.tv_sec = 0;
|
sleep_ms(200); // 5 Hz
|
||||||
delay.tv_nsec = 200000000; // 200 ms = 5 Hz
|
while ((running = server_pid()) == pid && gettime_ms() < timeout);
|
||||||
nanosleep(&delay, NULL);
|
|
||||||
} while ((running = server_pid()) == pid && gettime_ms() < timeout);
|
|
||||||
}
|
}
|
||||||
server_remove_stopfile();
|
server_remove_stopfile();
|
||||||
cli_puts("tries");
|
cli_puts("tries");
|
||||||
|
@ -251,12 +251,12 @@ int sqlite_exec_void_loglevel(int log_level, const char *sqlformat, ...)
|
|||||||
/*
|
/*
|
||||||
Same as sqlite_exec_void() but if the statement cannot be executed because the database is locked
|
Same as sqlite_exec_void() but if the statement cannot be executed because the database is locked
|
||||||
for updates, then will retry for at most the given number of milliseconds 'timeout_ms', sleeping
|
for updates, then will retry for at most the given number of milliseconds 'timeout_ms', sleeping
|
||||||
'sleep_ms' between tries (if sleep_ms == 0 then uses a default of 250ms).
|
'sleeptime_ms' between tries (if sleeptime_ms == 0 then uses a default of 250ms).
|
||||||
Returns -1 on error (logged as error), returns 0 on success, returns 1 if the database is still
|
Returns -1 on error (logged as error), returns 0 on success, returns 1 if the database is still
|
||||||
locked after all retries (logged as error).
|
locked after all retries (logged as error).
|
||||||
@author Andrew Bettison <andrew@servalproject.com>
|
@author Andrew Bettison <andrew@servalproject.com>
|
||||||
*/
|
*/
|
||||||
int sqlite_exec_void_retry(int timeout_ms, int sleep_ms, const char *sqlformat, ...)
|
int sqlite_exec_void_retry(int timeout_ms, int sleeptime_ms, const char *sqlformat, ...)
|
||||||
{
|
{
|
||||||
strbuf stmt = strbuf_alloca(8192);
|
strbuf stmt = strbuf_alloca(8192);
|
||||||
strbuf_va_printf(stmt, sqlformat);
|
strbuf_va_printf(stmt, sqlformat);
|
||||||
@ -264,8 +264,8 @@ int sqlite_exec_void_retry(int timeout_ms, int sleep_ms, const char *sqlformat,
|
|||||||
if (!statement)
|
if (!statement)
|
||||||
return -1;
|
return -1;
|
||||||
int ret;
|
int ret;
|
||||||
if (sleep_ms <= 0)
|
if (sleeptime_ms <= 0)
|
||||||
sleep_ms = 250;
|
sleeptime_ms = 250;
|
||||||
unsigned tries = 1;
|
unsigned tries = 1;
|
||||||
time_ms_t start = gettime_ms();
|
time_ms_t start = gettime_ms();
|
||||||
while ((ret = sqlite_exec_void_prepared_loglevel(LOG_LEVEL_SILENT, statement, 1)) == 1) {
|
while ((ret = sqlite_exec_void_prepared_loglevel(LOG_LEVEL_SILENT, statement, 1)) == 1) {
|
||||||
@ -283,10 +283,7 @@ int sqlite_exec_void_retry(int timeout_ms, int sleep_ms, const char *sqlformat,
|
|||||||
INFOF("database locked on try %u after %.3f seconds: %s",
|
INFOF("database locked on try %u after %.3f seconds: %s",
|
||||||
tries, (now - start) / 1e3, sqlite3_sql(statement)
|
tries, (now - start) / 1e3, sqlite3_sql(statement)
|
||||||
);
|
);
|
||||||
struct timespec delay;
|
sleep_ms(sleeptime_ms);
|
||||||
delay.tv_sec = sleep_ms / 1000;
|
|
||||||
delay.tv_nsec = (sleep_ms % 1000) * 1000000;
|
|
||||||
nanosleep(&delay, NULL);
|
|
||||||
++tries;
|
++tries;
|
||||||
}
|
}
|
||||||
if (tries > 1) {
|
if (tries > 1) {
|
||||||
|
1
serval.h
1
serval.h
@ -549,6 +549,7 @@ int respondSimple(keyring_identity *id,
|
|||||||
unsigned char *transaction_id,int recvttl,
|
unsigned char *transaction_id,int recvttl,
|
||||||
struct sockaddr *recvaddr,int cryptoFlags);
|
struct sockaddr *recvaddr,int cryptoFlags);
|
||||||
time_ms_t gettime_ms();
|
time_ms_t gettime_ms();
|
||||||
|
time_ms_t sleep_ms(time_ms_t milliseconds);
|
||||||
int server_pid();
|
int server_pid();
|
||||||
void server_save_argv(int argc, const char *const *argv);
|
void server_save_argv(int argc, const char *const *argv);
|
||||||
int server(char *backing_file);
|
int server(char *backing_file);
|
||||||
|
25
server.c
25
server.c
@ -123,6 +123,20 @@ time_ms_t gettime_ms()
|
|||||||
return nowtv.tv_sec * 1000LL + nowtv.tv_usec / 1000;
|
return nowtv.tv_sec * 1000LL + nowtv.tv_usec / 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns sleep time remaining.
|
||||||
|
time_ms_t sleep_ms(time_ms_t milliseconds)
|
||||||
|
{
|
||||||
|
if (milliseconds <= 0)
|
||||||
|
return 0;
|
||||||
|
struct timespec delay;
|
||||||
|
struct timespec remain;
|
||||||
|
delay.tv_sec = milliseconds / 1000;
|
||||||
|
delay.tv_nsec = (milliseconds % 1000) * 1000000;
|
||||||
|
if (nanosleep(&delay, &remain) == -1 && errno != EINTR)
|
||||||
|
FATALF_perror("nanosleep(tv_sec=%ld, tv_nsec=%ld)", delay.tv_sec, delay.tv_nsec);
|
||||||
|
return remain.tv_sec * 1000 + remain.tv_nsec / 1000000;
|
||||||
|
}
|
||||||
|
|
||||||
/** Return the PID of the currently running server process, return 0 if there is none.
|
/** Return the PID of the currently running server process, return 0 if there is none.
|
||||||
*/
|
*/
|
||||||
int server_pid()
|
int server_pid()
|
||||||
@ -168,15 +182,8 @@ int server(char *backing_file)
|
|||||||
process, for example to check that the start/stop logic is robust.
|
process, for example to check that the start/stop logic is robust.
|
||||||
*/
|
*/
|
||||||
const char *delay = getenv("SERVALD_SERVER_START_DELAY");
|
const char *delay = getenv("SERVALD_SERVER_START_DELAY");
|
||||||
if (delay) {
|
if (delay)
|
||||||
long ms = atoi(delay);
|
sleep_ms(atoi(delay));
|
||||||
if (ms > 0) {
|
|
||||||
struct timespec ts;
|
|
||||||
ts.tv_sec = ms / 1000;
|
|
||||||
ts.tv_nsec = (ms % 1000) * 1000000;
|
|
||||||
nanosleep(&ts, NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
serverMode = 1;
|
serverMode = 1;
|
||||||
serverRespawnOnCrash = confValueGetBoolean("server.respawn_on_crash", 0);
|
serverRespawnOnCrash = confValueGetBoolean("server.respawn_on_crash", 0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user