From 3670f75448cef252d2b3d46fbd60f2ba4a59a52a Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Mon, 18 Aug 2025 10:43:38 -0400 Subject: [PATCH] MT#61977 turn DB writing into a notification Change-Id: I7a3d0ad048bc88aa638adfc618c60fe510f254c4 --- recording-daemon/db.c | 68 +++++++++++++++++++++++++++------------ recording-daemon/db.h | 2 +- recording-daemon/notify.c | 2 +- recording-daemon/notify.h | 2 ++ recording-daemon/output.c | 3 +- recording-daemon/types.h | 7 +++- 6 files changed, 59 insertions(+), 25 deletions(-) diff --git a/recording-daemon/db.c b/recording-daemon/db.c index 24ec5a4e0..ef4f2857f 100644 --- a/recording-daemon/db.c +++ b/recording-daemon/db.c @@ -10,6 +10,7 @@ #include "tag.h" #include "recaux.h" #include "output.h" +#include "notify.h" /* @@ -376,39 +377,66 @@ void db_close_call(metafile_t *mf) { } } -bool db_close_stream(output_t *op) { - if (check_conn() || op->db_id == 0) - return !(output_storage & OUTPUT_STORAGE_DB); // error if DB storage is requested - int64_t now = now_us(); +static bool do_notify(notif_req_t *req) { + if (check_conn()) + return false; - str stream = STR_NULL; MYSQL_BIND b[3]; - bool ok = true; - - content_t *content = NULL; - if ((output_storage & OUTPUT_STORAGE_DB)) { - content = output_get_content(op); - if (content) - stream = STR_GS(content->s); - else - ok = false; - } int par_idx = 0; double ts; - my_ts(&b[par_idx++], now, &ts); - if ((output_storage & OUTPUT_STORAGE_DB)) + my_ts(&b[par_idx++], req->end_time, &ts); + if (req->content) { + str stream = STR_GS(req->content->s); my_str(&b[par_idx++], &stream); - my_ull(&b[par_idx++], &op->db_id); + } + my_ull(&b[par_idx++], &req->db_id); - execute_wrap(&stm_close_stream, b, NULL); + bool ok = execute_wrap(&stm_close_stream, b, NULL); - obj_release(content); + // running in a thread pool, don't leave connection behind + reset_conn(); return ok; } + +static void setup_notify(notif_req_t *req, output_t *o, metafile_t *mf, tag_t *tag) { + req->end_time = now_us(); + if ((output_storage & OUTPUT_STORAGE_DB)) + req->content = output_get_content(o); +} + +static void cleanup_notify(notif_req_t *req) { + obj_release(req->content); +} + +static const notif_action_t db_action = { + .name = "DB", + .setup = setup_notify, + .perform = do_notify, + .cleanup = cleanup_notify, +}; + +void db_close_stream(output_t *op) { + if (!c_mysql_host || !c_mysql_db) + return; + + if (op->db_id == 0) { + if (!(output_storage & OUTPUT_STORAGE_DB)) + return; + ilog(LOG_ERR, "DB storage requested but no entry exists"); + content_t *content = output_get_content(op); + if (content) + output_content_failure(content); + obj_release(content); + } + + notify_push_setup(&db_action, op, NULL, NULL); +} + + void db_delete_stream(metafile_t *mf, output_t *op) { if (check_conn()) return; diff --git a/recording-daemon/db.h b/recording-daemon/db.h index ae6287376..7725b54b8 100644 --- a/recording-daemon/db.h +++ b/recording-daemon/db.h @@ -7,7 +7,7 @@ void db_do_call(metafile_t *); void db_close_call(metafile_t *); void db_do_stream(metafile_t *mf, output_t *op, stream_t *, unsigned long ssrc); -bool db_close_stream(output_t *op); +void db_close_stream(output_t *op); void db_delete_stream(metafile_t *, output_t *op); void db_config_stream(output_t *op); void db_thread_end(void); diff --git a/recording-daemon/notify.c b/recording-daemon/notify.c index b3ae3ace3..43ecaede8 100644 --- a/recording-daemon/notify.c +++ b/recording-daemon/notify.c @@ -372,7 +372,7 @@ static const notif_action_t command_action = { -static void notify_push_setup(const notif_action_t *action, output_t *o, metafile_t *mf, tag_t *tag) { +void notify_push_setup(const notif_action_t *action, output_t *o, metafile_t *mf, tag_t *tag) { notif_req_t *req = g_new0(__typeof(*req), 1); req->name = g_strdup_printf("%s for '%s'", action->name, o->file_name); diff --git a/recording-daemon/notify.h b/recording-daemon/notify.h index 3b465434a..f875307fc 100644 --- a/recording-daemon/notify.h +++ b/recording-daemon/notify.h @@ -7,6 +7,8 @@ void notify_setup(void); void notify_cleanup(void); +void notify_push_setup(const notif_action_t *action, output_t *o, metafile_t *mf, tag_t *tag); + void notify_push_output(output_t *, metafile_t *, tag_t *); void notify_push_call(metafile_t *); diff --git a/recording-daemon/output.c b/recording-daemon/output.c index 697e4f691..4ed0a134a 100644 --- a/recording-daemon/output.c +++ b/recording-daemon/output.c @@ -672,8 +672,7 @@ void output_close(metafile_t *mf, output_t *output, tag_t *tag, bool discard) { bool do_delete = !(output_storage & OUTPUT_STORAGE_FILE); if (!discard) { if (output_shutdown(output)) { - if (!db_close_stream(output)) - do_delete = false; + db_close_stream(output); notify_push_output(output, mf, tag); } else diff --git a/recording-daemon/types.h b/recording-daemon/types.h index 5c86dbf05..83f2d82d7 100644 --- a/recording-daemon/types.h +++ b/recording-daemon/types.h @@ -248,17 +248,22 @@ struct notif_req_s { // generic HTTP req struct { struct curl_slist *headers; - content_t *content; }; // notify command struct { char **argv; }; + + // db writer + struct { + int64_t end_time; + }; }; // used by multiple actions unsigned long long db_id; + content_t *content; const notif_action_t *action;