MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / sentinelCollectTerminatedScripts

Function sentinelCollectTerminatedScripts

src/sentinel.cpp:914–957  ·  view source on GitHub ↗

Check for scripts that terminated, and remove them from the queue if the * script terminated successfully. If instead the script was terminated by * a signal, or returned exit code "1", it is scheduled to run again if * the max number of retries did not already elapsed. */

Source from the content-addressed store, hash-verified

912 * a signal, or returned exit code "1", it is scheduled to run again if
913 * the max number of retries did not already elapsed. */
914void sentinelCollectTerminatedScripts(void) {
915 int statloc;
916 pid_t pid;
917
918 while ((pid = waitpid(-1, &statloc, WNOHANG)) > 0) {
919 int exitcode = WEXITSTATUS(statloc);
920 int bysignal = 0;
921 listNode *ln;
922 sentinelScriptJob *sj;
923
924 if (WIFSIGNALED(statloc)) bysignal = WTERMSIG(statloc);
925 sentinelEvent(LL_DEBUG,"-script-child",NULL,"%ld %d %d",
926 (long)pid, exitcode, bysignal);
927
928 ln = sentinelGetScriptListNodeByPid(pid);
929 if (ln == NULL) {
930 serverLog(LL_WARNING,"waitpid() returned a pid (%ld) we can't find in our scripts execution queue!", (long)pid);
931 continue;
932 }
933 sj = (sentinelScriptJob*)ln->value;
934
935 /* If the script was terminated by a signal or returns an
936 * exit code of "1" (that means: please retry), we reschedule it
937 * if the max number of retries is not already reached. */
938 if ((bysignal || exitcode == 1) &&
939 sj->retry_num != SENTINEL_SCRIPT_MAX_RETRY)
940 {
941 sj->flags &= ~SENTINEL_SCRIPT_RUNNING;
942 sj->pid = 0;
943 sj->start_time = mstime() +
944 sentinelScriptRetryDelay(sj->retry_num);
945 } else {
946 /* Otherwise let's remove the script, but log the event if the
947 * execution did not terminated in the best of the ways. */
948 if (bysignal || exitcode != 0) {
949 sentinelEvent(LL_WARNING,"-script-error",NULL,
950 "%s %d %d", sj->argv[0], bysignal, exitcode);
951 }
952 listDelNode(sentinel.scripts_queue,ln);
953 sentinelReleaseScriptJob(sj);
954 }
955 sentinel.running_scripts--;
956 }
957}
958
959/* Kill scripts in timeout, they'll be collected by the
960 * sentinelCollectTerminatedScripts() function. */

Callers 1

sentinelTimerFunction · 0.85

Calls 7

sentinelEventFunction · 0.85
serverLogFunction · 0.85
sentinelScriptRetryDelayFunction · 0.85
listDelNodeFunction · 0.85
sentinelReleaseScriptJobFunction · 0.85
mstimeFunction · 0.70

Tested by

no test coverage detected