Skip to content

Commit

Permalink
rtimcmd: Introduce grace period between execution of STOP_COMMANDs an…
Browse files Browse the repository at this point in the history
…d sending SIGTERM to all processes.
  • Loading branch information
Björn Kirchner authored and gmcn42 committed Jan 30, 2025
1 parent 579318a commit fa255b2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ ENV_SET = GREETING "Good morning!"
- **INCLUDEDIR** -- Where to find include files referenced from task configurations. Default: Same as **TASKDIR**.
- **INCLUDE_SUFFIX** -- Filename suffix of include files referenced from task configurations. Default: `.crincl`
- **DEBUG** -- If crinit should be verbose in its output. Either `YES` or `NO`. Default: `NO`
- **SHUTDOWN_GRACE_PERIOD_US** -- The amount of microseconds to wait between `SIGTERM` and `SIGKILL` on shutdown/reboot.
- **SHUTDOWN_GRACE_PERIOD_US** -- The amount of microseconds to wait both between `STOP_COMMAND` and `SIGTERM` as well as between`SIGTERM` and `SIGKILL` on shutdown/reboot.
Default: 100000
- **USE_SYSLOG** -- If syslog should be used for output if it is available. If set to `YES`, Crinit will switch to
syslog for output as soon as a task file `PROVIDES` the `syslog` feature. Ideally this should be
Expand Down
27 changes: 16 additions & 11 deletions src/rtimcmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1188,28 +1188,33 @@ static void *crinitShdnThread(void *args) {
int shutdownCmd = a->shutdownCmd;
free(args);

if (crinitTaskDBSetSpawnInhibit(ctx, true) == -1) {
crinitErrPrint("Could not inhibit spawning of new tasks. Continuing anyway.");
}

unsigned long long gpMicros = CRINIT_CONFIG_DEFAULT_SHDGRACEP;
if (crinitGlobOptGet(CRINIT_GLOBOPT_SHDGRACEP, &gpMicros) == -1) {
gpMicros = CRINIT_CONFIG_DEFAULT_SHDGRACEP;
crinitErrPrint("Could not read global option for shutdown grace period, using default: %lluus.", gpMicros);
}

bool haveStopCommands = false;
if ((errno = pthread_mutex_lock(&ctx->lock)) == 0) {
crinitTask_t *pTask;
crinitTaskDbForEach(ctx, pTask) {
if (pTask->stopCmdsSize > 0) {
haveStopCommands = true;
if (ctx->spawnFunc(ctx, pTask, CRINIT_DISPATCH_THREAD_MODE_STOP) == -1) {
crinitErrPrint("Could not spawn new thread for execution STOP_COMMAND of task \'%s\'.", pTask->name);
}
}
}
pthread_mutex_unlock(&ctx->lock);
}
// TODO: Should we insert a grace period here?

unsigned long long gpMicros = CRINIT_CONFIG_DEFAULT_SHDGRACEP;
if (crinitGlobOptGet(CRINIT_GLOBOPT_SHDGRACEP, &gpMicros) == -1) {
gpMicros = CRINIT_CONFIG_DEFAULT_SHDGRACEP;
crinitErrPrint("Could not read global option for shutdown grace period, using default: %lluus.", gpMicros);
}

if (haveStopCommands && crinitGracePeriod(gpMicros) == -1) {
crinitErrPrint("Could not wait out the shutdown grace period, continuing anyway.");
}

if (crinitTaskDBSetSpawnInhibit(ctx, true) == -1) {
crinitErrPrint("Could not inhibit spawning of new tasks. Continuing anyway.");
}

kill(-1, SIGCONT);
kill(-1, SIGTERM);
Expand Down

0 comments on commit fa255b2

Please sign in to comment.