Run pending scripts if we are not already at max number of running * scripts. */
| 843 | /* Run pending scripts if we are not already at max number of running |
| 844 | * scripts. */ |
| 845 | void sentinelRunPendingScripts(void) { |
| 846 | listNode *ln; |
| 847 | listIter li; |
| 848 | mstime_t now = mstime(); |
| 849 | |
| 850 | /* Find jobs that are not running and run them, from the top to the |
| 851 | * tail of the queue, so we run older jobs first. */ |
| 852 | listRewind(sentinel.scripts_queue,&li); |
| 853 | while (sentinel.running_scripts < SENTINEL_SCRIPT_MAX_RUNNING && |
| 854 | (ln = listNext(&li)) != NULL) |
| 855 | { |
| 856 | sentinelScriptJob *sj = (sentinelScriptJob*)ln->value; |
| 857 | pid_t pid; |
| 858 | |
| 859 | /* Skip if already running. */ |
| 860 | if (sj->flags & SENTINEL_SCRIPT_RUNNING) continue; |
| 861 | |
| 862 | /* Skip if it's a retry, but not enough time has elapsed. */ |
| 863 | if (sj->start_time && sj->start_time > now) continue; |
| 864 | |
| 865 | sj->flags |= SENTINEL_SCRIPT_RUNNING; |
| 866 | sj->start_time = mstime(); |
| 867 | sj->retry_num++; |
| 868 | pid = fork(); |
| 869 | |
| 870 | if (pid == -1) { |
| 871 | /* Parent (fork error). |
| 872 | * We report fork errors as signal 99, in order to unify the |
| 873 | * reporting with other kind of errors. */ |
| 874 | sentinelEvent(LL_WARNING,"-script-error",NULL, |
| 875 | "%s %d %d", sj->argv[0], 99, 0); |
| 876 | sj->flags &= ~SENTINEL_SCRIPT_RUNNING; |
| 877 | sj->pid = 0; |
| 878 | } else if (pid == 0) { |
| 879 | /* Child */ |
| 880 | tlsCleanup(); |
| 881 | for (int iel = 0; iel < cserver.cthreads; ++iel) { |
| 882 | aeClosePipesForForkChild(g_pserver->rgthreadvar[iel].el); |
| 883 | } |
| 884 | aeClosePipesForForkChild(g_pserver->modulethreadvar.el); |
| 885 | execve(sj->argv[0],sj->argv,environ); |
| 886 | /* If we are here an error occurred. */ |
| 887 | _exit(2); /* Don't retry execution. */ |
| 888 | } else { |
| 889 | sentinel.running_scripts++; |
| 890 | sj->pid = pid; |
| 891 | sentinelEvent(LL_DEBUG,"+script-child",NULL,"%ld",(long)pid); |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | /* How much to delay the execution of a script that we need to retry after |
| 897 | * an error? |
no test coverage detected