コード例 #1
0
ファイル: worker.c プロジェクト: tooooots/mod_gearman
/* start new worker if needed */
void check_worker_population() {
    int x, now, target_number_of_workers;

    gm_log( GM_LOG_TRACE3, "check_worker_population()\n");

    /* set current worker number */
    count_current_worker(GM_ENABLED);

    /* check if status worker died */
    if( shm[3] == -1 ) {
        make_new_child(GM_WORKER_STATUS);
    }

    /* keep up minimum population */
    for (x = current_number_of_workers; x < mod_gm_opt->min_worker; x++) {
        make_new_child(GM_WORKER_MULTI);
        current_number_of_workers++;
    }

    /* check every second */
    now = (int)time(NULL);
    if(last_time_increased >= now)
        return;

    target_number_of_workers = adjust_number_of_worker(mod_gm_opt->min_worker, mod_gm_opt->max_worker, current_number_of_workers, current_number_of_jobs);
    for (x = current_number_of_workers; x < target_number_of_workers; x++) {
        last_time_increased = now;
        /* top up the worker pool */
        make_new_child(GM_WORKER_MULTI);
    }
    return;
}
コード例 #2
0
/* start new worker if needed */
void check_worker_population() {
    int x, now, status, target_number_of_workers;

    gm_log( GM_LOG_TRACE3, "check_worker_population()\n");

    now = (int)time(NULL);

    /* collect finished workers */
    while(waitpid(-1, &status, WNOHANG) > 0)
        gm_log( GM_LOG_TRACE, "waitpid() worker exited with: %d\n", status);

    /* set current worker number */
    count_current_worker(GM_ENABLED);

    /* check last check time, force restart all worker if there is no result in 2 minutes */
    if( shm[SHM_WORKER_LAST_CHECK] < (now - 120) ) {
        gm_log( GM_LOG_INFO, "no checks in 2minutes, restarting all workers\n", shm[SHM_WORKER_LAST_CHECK]);
        shm[SHM_WORKER_LAST_CHECK] = now;
        for(x=SHM_SHIFT; x < mod_gm_opt->max_worker+SHM_SHIFT; x++) {
            save_kill(shm[x], SIGINT);
        }
        sleep(3);
        for(x=SHM_SHIFT; x < mod_gm_opt->max_worker+SHM_SHIFT; x++) {
            save_kill(shm[x], SIGKILL);
            shm[x] = -1;
        }
    }

    /* check if status worker died */
    if( shm[SHM_STATUS_WORKER_PID] == -1 ) {
        make_new_child(GM_WORKER_STATUS);
    }

    /* keep up minimum population */
    for (x = current_number_of_workers; x < mod_gm_opt->min_worker; x++) {
        make_new_child(GM_WORKER_MULTI);
        current_number_of_workers++;
    }

    /* check every second if we need to increase worker population */
    if(last_time_increased >= now)
        return;

    target_number_of_workers = adjust_number_of_worker(mod_gm_opt->min_worker, mod_gm_opt->max_worker, current_number_of_workers, current_number_of_jobs);
    for (x = current_number_of_workers; x < target_number_of_workers; x++) {
        last_time_increased = now;
        /* top up the worker pool */
        make_new_child(GM_WORKER_MULTI);
    }
    return;
}