Made it easier to run multiple instances.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3687 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2021-11-14 22:55:21 +00:00
parent 63c344112d
commit 9e1bab03eb
3 changed files with 113 additions and 50 deletions

View File

@ -298,34 +298,83 @@ xopt_help:
return 1;
}
typedef struct tf_run_args_t {
const char* script;
int ssb_port;
int http_port;
int https_port;
const char* db_path;
const char* secrets_path;
int count;
bool help;
} tf_run_args_t;
typedef struct _tf_run_thread_data_t
{
tf_run_args_t args;
int index;
int result;
} tf_run_thread_data_t;
static int _tf_run_task(const tf_run_args_t* args, int index)
{
int result = -1;
tf_task_t* task = tf_task_create();
tf_task_set_trusted(task, true);
tf_task_set_ssb_port(task, args->ssb_port ? args->ssb_port + index : 0);
tf_task_set_http_port(task, args->http_port ? args->http_port + index : 0);
tf_task_set_https_port(task, args->https_port ? args->https_port + index : 0);
const char* db_path = args->db_path;
const char* secrets_path = args->secrets_path;
char db_path_buffer[256];
char secrets_path_buffer[256];
if (index)
{
snprintf(db_path_buffer, sizeof(db_path_buffer), "%s.%d", args->db_path, index);
db_path = db_path_buffer;
snprintf(secrets_path_buffer, sizeof(secrets_path_buffer), "%s.%d", args->secrets_path, index);
secrets_path = secrets_path_buffer;
}
tf_task_set_db_path(task, db_path);
tf_task_set_secrets_path(task, secrets_path);
tf_task_activate(task);
if (tf_task_execute(task, args->script))
{
tf_task_run(task);
result = 0;
}
tf_task_destroy(task);
return result;
}
static void _tf_run_task_thread(void* data)
{
tf_run_thread_data_t* info = data;
info->result = _tf_run_task(&info->args, info->index);
}
static int _tf_command_run(const char* file, int argc, char* argv[])
{
typedef struct args_t {
const char* script;
int ssb_port;
int http_port;
int https_port;
const char* db_path;
const char* secrets_path;
bool help;
} args_t;
xoptOption options[] = {
{ "script", 's', offsetof(args_t, script), NULL, XOPT_TYPE_STRING, NULL, "Script to run (default: core/core.js)." },
{ "ssb-port", 'b', offsetof(args_t, ssb_port), NULL, XOPT_TYPE_INT, NULL, "Port on which to run SSB (default: 8009)." },
{ "http-port", 'p', offsetof(args_t, http_port), NULL, XOPT_TYPE_INT, NULL, "Port on which to run Tilde Friends web server (default: 12345)." },
{ "https-port", 'q', offsetof(args_t, https_port), NULL, XOPT_TYPE_INT, NULL, "Port on which to run secure Tilde Friends web server (default: 12346)." },
{ "db-path", 'd', offsetof(args_t, db_path), NULL, XOPT_TYPE_STRING, NULL, "Sqlite database path (default: db.sqlite)." },
{ "secrets-path", 'i', offsetof(args_t, secrets_path), NULL, XOPT_TYPE_STRING, NULL, "Secrets/identity path." },
{ "help", 'h', offsetof(args_t, help), NULL, XOPT_TYPE_BOOL, NULL, "Shows this help message." },
{ "script", 's', offsetof(tf_run_args_t, script), NULL, XOPT_TYPE_STRING, NULL, "Script to run (default: core/core.js)." },
{ "ssb-port", 'b', offsetof(tf_run_args_t, ssb_port), NULL, XOPT_TYPE_INT, NULL, "Port on which to run SSB (default: 8009)." },
{ "http-port", 'p', offsetof(tf_run_args_t, http_port), NULL, XOPT_TYPE_INT, NULL, "Port on which to run Tilde Friends web server (default: 12345)." },
{ "https-port", 'q', offsetof(tf_run_args_t, https_port), NULL, XOPT_TYPE_INT, NULL, "Port on which to run secure Tilde Friends web server (default: 12346)." },
{ "db-path", 'd', offsetof(tf_run_args_t, db_path), NULL, XOPT_TYPE_STRING, NULL, "Sqlite database path (default: db.sqlite)." },
{ "secrets-path", 'i', offsetof(tf_run_args_t, secrets_path), NULL, XOPT_TYPE_STRING, NULL, "Secrets/identity path." },
{ "count", 'n', offsetof(tf_run_args_t, count), NULL, XOPT_TYPE_INT, NULL, "Number of instances to run." },
{ "help", 'h', offsetof(tf_run_args_t, help), NULL, XOPT_TYPE_BOOL, NULL, "Shows this help message." },
XOPT_NULLOPTION,
};
args_t args = {
tf_run_args_t args =
{
.count = 1,
.script = "core/core.js",
.http_port = 12345,
.ssb_port = 8009,
.db_path = "db.sqlite",
.secrets_path = "/.config/tildefriends/secret",
};
const char** extras = NULL;
int extra_count = 0;
@ -350,24 +399,36 @@ static int _tf_command_run(const char* file, int argc, char* argv[])
#if !defined (_WIN32) && !defined (__MACH__)
setpgid(0, 0);
#endif
tf_task_t* task = tf_task_create();
tf_task_set_trusted(task, true);
tf_task_set_ssb_port(task, args.ssb_port);
tf_task_set_http_port(task, args.http_port);
tf_task_set_https_port(task, args.https_port);
tf_task_set_db_path(task, args.db_path);
tf_task_set_secrets_path(task, args.secrets_path);
tf_task_activate(task);
if (!tf_task_execute(task, args.script))
if (args.count == 1)
{
result = -1;
_tf_run_task(&args, 0);
}
if (args.count > 1)
{
uv_thread_t* threads = malloc(sizeof(uv_thread_t) * args.count);
tf_run_thread_data_t* data = malloc(sizeof(tf_run_thread_data_t) * args.count);
for (int i = 0 ; i < args.count; i++)
{
data[i] = (tf_run_thread_data_t)
{
.args = args,
.index = i,
};
uv_thread_create(&threads[i], _tf_run_task_thread, &data[i]);
}
for (int i = 0; i < args.count; i++)
{
uv_thread_join(&threads[i]);
if (data[i].result != 0)
{
result = data[i].result;
}
}
free(data);
free(threads);
}
if (result == 0)
{
tf_task_run(task);
}
tf_task_destroy(task);
return result;
xopt_help: