// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/sentinel.c
// Function: sentinelRunPendingScripts
void sentinelRunPendingScripts(void) {
    listNode *ln;
    listIter li;
    mstime_t now = mstime();

    /* Find jobs that are not running and run them, from the top to the
     * tail of the queue, so we run older jobs first. */
    // 如果运行的脚本数量未超过最大值，
    // 那么从 FIFO 队列中取出未运行的脚本，并运行该脚本
    listRewind(sentinel.scripts_queue,&li);
    while (sentinel.running_scripts < SENTINEL_SCRIPT_MAX_RUNNING &&
           (ln = listNext(&li)) != NULL)
    {
        sentinelScriptJob *sj = ln->value;
        pid_t pid;

        /* Skip if already running. */
        // 跳过已运行脚本
        if (sj->flags & SENTINEL_SCRIPT_RUNNING) continue;

        /* Skip if it's a retry, but not enough time has elapsed. */
        // 这是一个重试脚本，但它刚刚执行完，稍后再重试
        if (sj->start_time && sj->start_time > now) continue;

        // 打开运行标记
        sj->flags |= SENTINEL_SCRIPT_RUNNING;
        // 记录开始时间
        sj->start_time = mstime();
        // 增加重试计数器
        sj->retry_num++;

        // 创建子进程
        pid = fork();

        if (pid == -1) {
            
            // 创建子进程失败

            /* Parent (fork error).
             * We report fork errors as signal 99, in order to unify the
             * reporting with other kind of errors. */
            sentinelEvent(REDIS_WARNING,"-script-error",NULL,
                          "%s %d %d", sj->argv[0], 99, 0);
            sj->flags &= ~SENTINEL_SCRIPT_RUNNING;
            sj->pid = 0;
        } else if (pid == 0) {

            // 子进程执行脚本

            /* Child */
            execve(sj->argv[0],sj->argv,environ);
            /* If we are here an error occurred. */
            _exit(2); /* Don't retry execution. */
        } else {

            // 父进程
            
            // 增加运行脚本计数器
            sentinel.running_scripts++;

            // 记录 pid
            sj->pid = pid;

            // 发送脚本运行信号
            sentinelEvent(REDIS_DEBUG,"+script-child",NULL,"%ld",(long)pid);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
callq 0x232a3
movq %rax, %rbx
movq 0x82d49(%rip), %rdi     # 0xde910
leaq 0x8(%rsp), %rsi
callq 0x205d2
cmpl $0xf, 0x82d24(%rip)     # 0xde8fc
jg 0x5bcb4
leaq 0x8(%rsp), %r14
xorl %r15d, %r15d
leaq 0x432d9(%rip), %r13     # 0x9eec6
leaq 0x38bd3(%rip), %rbp     # 0x947c7
movq %r14, %rdi
callq 0x205ef
testq %rax, %rax
je 0x5bcb4
movq 0x10(%rax), %r12
movl (%r12), %eax
testb $0x1, %al
jne 0x5bca7
movq 0x10(%r12), %rcx
testq %rcx, %rcx
setne %dl
cmpq %rbx, %rcx
setg %cl
testb %cl, %dl
jne 0x5bca7
orl $0x1, %eax
movl %eax, (%r12)
callq 0x232a3
movq %rax, 0x10(%r12)
incl 0x4(%r12)
callq 0x1fa30
cmpl $-0x1, %eax
je 0x5bc6f
testl %eax, %eax
je 0x5bcc3
incl 0x82ca8(%rip)           # 0xde8fc
movl %eax, 0x18(%r12)
movslq %eax, %r8
xorl %edi, %edi
movq %r13, %rsi
xorl %edx, %edx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x5b66f
jmp 0x5bca7
movq 0x8(%r12), %rax
movq (%rax), %r8
movl %r15d, (%rsp)
movl $0x3, %edi
leaq 0x43231(%rip), %rsi     # 0x9eeb8
xorl %edx, %edx
leaq 0x435cc(%rip), %rcx     # 0x9f25c
movl $0x63, %r9d
xorl %eax, %eax
callq 0x5b66f
andb $-0x2, (%r12)
movl %r15d, 0x18(%r12)
cmpl $0x10, 0x82c4e(%rip)    # 0xde8fc
jl 0x5bbf4
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r12), %rsi
movq (%rsi), %rdi
movq 0x6a22e(%rip), %rax     # 0xc5f00
movq (%rax), %rdx
callq 0x1f730
movl $0x2, %edi
callq 0x1f3b0