int swAioGcc_init(int max_aio_events) { if (swPipeBase_create(&swoole_aio_pipe, 0) < 0) { return SW_ERR; } swSignal_set(SIGIO, swAioGcc_signal_handler, 1, 0); swAioGcc_pipe_read = swoole_aio_pipe.getFd(&swoole_aio_pipe, 0); swAioGcc_pipe_write = swoole_aio_pipe.getFd(&swoole_aio_pipe, 1); SwooleAIO.reactor->setHandle(SwooleAIO.reactor, SW_FD_AIO, swAioGcc_onFinish); SwooleAIO.reactor->add(SwooleAIO.reactor, swAioGcc_pipe_read, SW_FD_AIO); SwooleAIO.callback = swAio_callback_test; SwooleAIO.read = swAioGcc_aio_read; SwooleAIO.write = swAioGcc_write; SwooleAIO.destroy = swAioGcc_destroy; return SW_OK; }
static int swFactoryProcess_manager_loop(swFactory *factory) { int pid, new_pid; int i; int reload_worker_i = 0; int reload_worker_num; int ret; int worker_exit_code; SwooleG.use_signalfd = 0; SwooleG.use_timerfd = 0; swServer *serv = factory->ptr; swWorker *reload_workers; swSignal_set(SIGTERM, swWorker_signal_handler, 1, 0); if (serv->onManagerStart) { serv->onManagerStart(serv); } reload_worker_num = serv->worker_num + SwooleG.task_worker_num; reload_workers = sw_calloc(reload_worker_num, sizeof(swWorker)); if (reload_workers == NULL) { swError("[manager] malloc[reload_workers] failed"); return SW_ERR; } //for reload swSignal_add(SIGUSR1, swManager_signal_handle); //swSignal_add(SIGINT, swManager_signal_handle); while (SwooleG.running > 0) { pid = wait(&worker_exit_code); if (pid < 0) { if (manager_worker_reloading == 0) { swTrace("[Manager] wait failed. Error: %s [%d]", strerror(errno), errno); } else if (manager_reload_flag == 0) { memcpy(reload_workers, serv->workers, sizeof(swWorker) * serv->worker_num); if (SwooleG.task_worker_num > 0) { memcpy(reload_workers + serv->worker_num, SwooleG.task_workers.workers, sizeof(swWorker) * SwooleG.task_worker_num); } manager_reload_flag = 1; goto kill_worker; } } if (SwooleG.running == 1) { for (i = 0; i < serv->worker_num; i++) { //compare PID if (pid != serv->workers[i].pid) { continue; } else { if (serv->onWorkerError!=NULL && WEXITSTATUS(worker_exit_code) > 0) { serv->onWorkerError(serv, i, pid, WEXITSTATUS(worker_exit_code)); } pid = 0; while (1) { new_pid = swFactoryProcess_worker_spawn(factory, i); if (new_pid < 0) { usleep(100000); continue; } else { serv->workers[i].pid = new_pid; break; } } } } //task worker if (pid > 0) { swWorker *exit_worker = swHashMap_find_int(SwooleG.task_workers.map, pid); if (exit_worker != NULL) { swProcessPool_spawn(exit_worker); } } } //reload worker kill_worker: if (manager_worker_reloading == 1) { //reload finish if (reload_worker_i >= reload_worker_num) { manager_worker_reloading = 0; reload_worker_i = 0; continue; } ret = kill(reload_workers[reload_worker_i].pid, SIGTERM); if (ret < 0) { swWarn("[Manager]kill() failed, pid=%d. Error: %s [%d]", reload_workers[reload_worker_i].pid, strerror(errno), errno); continue; } reload_worker_i++; } } sw_free(reload_workers); if (serv->onManagerStop) { serv->onManagerStop(serv); } return SW_OK; }
static int swFactoryProcess_manager_loop(swFactory *factory) { int pid, new_pid; int i; int reload_worker_i = 0; int ret; int worker_exit_code; swFactoryProcess *object = factory->object; swServer *serv = factory->ptr; swWorker *reload_workers; if (serv->onManagerStart) { serv->onManagerStart(serv); } reload_workers = sw_calloc(object->worker_num, sizeof(swWorker)); if (reload_workers == NULL) { swError("[manager] malloc[reload_workers] failed"); return SW_ERR; } //for reload swSignal_set(SIGUSR1, swManagerSignalHanlde, 1, 0); while (SwooleG.running > 0) { pid = wait(&worker_exit_code); swTrace("[manager] worker stop.pid=%d\n", pid); if (pid < 0) { if (manager_worker_reloading == 0) { swTrace("[Manager] wait failed. Error: %s [%d]", strerror(errno), errno); } else if (manager_reload_flag == 0) { memcpy(reload_workers, object->workers, sizeof(swWorker) * object->worker_num); manager_reload_flag = 1; goto kill_worker; } } if (SwooleG.running == 1) { for (i = 0; i < object->worker_num; i++) { //对比pid if (pid != object->workers[i].pid) { continue; } else { if(serv->onWorkerError!=NULL && WEXITSTATUS(worker_exit_code) > 0) { serv->onWorkerError(serv, i, pid, WEXITSTATUS(worker_exit_code)); } pid = 0; new_pid = swFactoryProcess_worker_spawn(factory, i); if (new_pid < 0) { swWarn("Fork worker process failed. Error: %s [%d]", strerror(errno), errno); return SW_ERR; } else { object->workers[i].pid = new_pid; } } } //task worker if(pid > 0) { swWorker *exit_worker = swHashMap_find_int(&SwooleG.task_workers.map, pid); if (exit_worker != NULL) { swProcessPool_spawn(exit_worker); } } } //reload worker kill_worker: if (manager_worker_reloading == 1) { //reload finish if (reload_worker_i >= object->worker_num) { manager_worker_reloading = 0; reload_worker_i = 0; continue; } ret = kill(reload_workers[reload_worker_i].pid, SIGTERM); if (ret < 0) { swWarn("[Manager]kill failed, pid=%d. Error: %s [%d]", reload_workers[reload_worker_i].pid, strerror(errno), errno); continue; } reload_worker_i++; } } sw_free(reload_workers); if (serv->onManagerStop) { serv->onManagerStop(serv); } return SW_OK; }