^^^^^^^^^^^^^^^^
Worker is a service over event loop that tracks and schedules outstanding queries,
-you can see the statistics or schedule new queries.
+you can see the statistics or schedule new queries. It also contains information about
+specified worker count and process rank.
+
+.. envvar:: worker.count
+
+ Return current total worker count (e.g. `1` for single-process)
+
+.. envvar:: worker.id
+
+ Return current worker ID (starting from `0` up to `worker.count - 1`)
.. function:: worker.stats()
" [rundir] Path to the working directory (default: .)\n");
}
-static struct worker_ctx *init_worker(uv_loop_t *loop, struct engine *engine, mm_ctx_t *pool, int worker_id)
+static struct worker_ctx *init_worker(uv_loop_t *loop, struct engine *engine, mm_ctx_t *pool, int worker_id, int worker_count)
{
/* Load bindings */
engine_lualib(engine, "modules", lib_modules);
return NULL;
}
memset(worker, 0, sizeof(*worker));
+ worker->id = worker_id;
+ worker->count = worker_count;
worker->engine = engine,
worker->loop = loop;
loop->data = worker;
lua_getglobal(engine->L, "worker");
lua_pushnumber(engine->L, worker_id);
lua_setfield(engine->L, -2, "id");
+ lua_pushnumber(engine->L, worker_count);
+ lua_setfield(engine->L, -2, "count");
lua_pop(engine->L, 1);
return worker;
}
kr_crypto_init();
/* Fork subprocesses if requested */
+ int fork_count = forks;
while (--forks > 0) {
int pid = fork();
if (pid < 0) {
return EXIT_FAILURE;
}
/* Create worker */
- struct worker_ctx *worker = init_worker(loop, &engine, &pool, forks);
+ struct worker_ctx *worker = init_worker(loop, &engine, &pool, forks, fork_count);
if (!worker) {
log_error("[system] not enough memory\n");
return EXIT_FAILURE;