Skip to content

Commit

Permalink
Add restart for dbse,logger and webu
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Dave committed Aug 18, 2024
1 parent 9511892 commit 0115ed1
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 49 deletions.
17 changes: 17 additions & 0 deletions src/conf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4232,6 +4232,23 @@ void cls_config::parms_copy(cls_config *src)

}

void cls_config::parms_copy(cls_config *src, PARM_CAT p_cat)
{
int indx;
std::string parm_nm, parm_val;

indx = 0;
while (config_parms[indx].parm_name != "") {
if (config_parms[indx].parm_cat == p_cat) {
parm_nm =config_parms[indx].parm_name;
src->edit_get(parm_nm, parm_val, p_cat);
edit_set(parm_nm, parm_val);
}
indx++;
}

}

void cls_config::init(cls_motapp *app)
{
std::string filename;
Expand Down
1 change: 1 addition & 0 deletions src/conf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
void parms_log(cls_motapp *app);
void parms_write(cls_motapp *app);
void parms_copy(cls_config *src);
void parms_copy(cls_config *src, PARM_CAT p_cat);

private:
void cmdline(cls_motapp *app);
Expand Down
68 changes: 37 additions & 31 deletions src/dbse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ void cls_dbse::mariadb_exec (std::string sql)
, mysql_error(database_mariadb)
, retcd);
if (retcd >= 2000) {
dbse_close();
shutdown();
return;
}
}
Expand All @@ -427,7 +427,7 @@ void cls_dbse::mariadb_exec (std::string sql)
, _("MariaDB query commit failed. %s error code %d")
, mysql_error(database_mariadb), retcd);
if (retcd >= 2000) {
dbse_close();
shutdown();
return;
}
}
Expand All @@ -449,15 +449,15 @@ void cls_dbse::mariadb_recs (std::string sql)
if (retcd != 0){
MOTPLS_LOG(ERR, TYPE_DB, NO_ERRNO
, _("Query error: %s"),sql.c_str());
dbse_close();
shutdown();
return;
}

qry_result = mysql_store_result(database_mariadb);
if (qry_result == nullptr) {
MOTPLS_LOG(ERR, TYPE_DB, NO_ERRNO
, _("Query store error: %s"),sql.c_str());
dbse_close();
shutdown();
return;
}

Expand Down Expand Up @@ -591,7 +591,7 @@ void cls_dbse::mariadb_init()
MOTPLS_LOG(ERR, TYPE_DB, NO_ERRNO
, _("MariaDB error was %s")
, mysql_error(database_mariadb));
dbse_close();
shutdown();
return;
}
is_open = true;
Expand Down Expand Up @@ -658,7 +658,7 @@ void cls_dbse::pgsqldb_exec(std::string sql)
, app->cfg->database_dbname.c_str()
, PQerrorMessage(database_pgsqldb));
PQclear(res);
dbse_close();
shutdown();
return;
} else {
MOTPLS_LOG(INF, TYPE_DB, NO_ERRNO
Expand Down Expand Up @@ -814,7 +814,7 @@ void cls_dbse::pgsqldb_init()
, _("Connection to PostgreSQL database '%s' failed: %s")
, app->cfg->database_dbname.c_str()
, PQerrorMessage(database_pgsqldb));
dbse_close();
shutdown();
return;
}
is_open = true;
Expand Down Expand Up @@ -853,25 +853,23 @@ bool cls_dbse::dbse_open()
return false;
}

pthread_mutex_lock(&mutex_dbse);
MOTPLS_LOG(DBG, TYPE_DB, NO_ERRNO,_("Opening database"));
MOTPLS_LOG(DBG, TYPE_DB, NO_ERRNO,_("Opening database"));

#ifdef HAVE_MARIADB
if (app->cfg->database_type == "mariadb") {
mariadb_init();
}
#endif
#ifdef HAVE_PGSQLDB
if (app->cfg->database_type == "postgresql") {
pgsqldb_init();
}
#endif
#ifdef HAVE_SQLITE3DB
if (app->cfg->database_type == "sqlite3") {
sqlite3db_init();
}
#endif
pthread_mutex_unlock(&mutex_dbse);
#ifdef HAVE_MARIADB
if (app->cfg->database_type == "mariadb") {
mariadb_init();
}
#endif
#ifdef HAVE_PGSQLDB
if (app->cfg->database_type == "postgresql") {
pgsqldb_init();
}
#endif
#ifdef HAVE_SQLITE3DB
if (app->cfg->database_type == "sqlite3") {
sqlite3db_init();
}
#endif

return is_open;
}
Expand Down Expand Up @@ -907,7 +905,7 @@ void cls_dbse::movielist_get(int p_device_id, lst_movies *p_movielist)

}

void cls_dbse::dbse_close()
void cls_dbse::shutdown()
{
#ifdef HAVE_MARIADB
mariadb_close();
Expand Down Expand Up @@ -1083,22 +1081,30 @@ void cls_dbse::dbse_edits()

}

void cls_dbse::startup()
{
is_open = false;

dbse_edits();

dbse_open();
}

cls_dbse::cls_dbse(cls_motapp *p_app)
{
app = p_app;

is_open = false;

pthread_mutex_init(&mutex_dbse, nullptr);

dbse_edits();
pthread_mutex_lock(&mutex_dbse);
startup();
pthread_mutex_unlock(&mutex_dbse);

dbse_open();
}

cls_dbse::~cls_dbse()
{
dbse_close();
shutdown();

pthread_mutex_destroy(&mutex_dbse);
}
3 changes: 2 additions & 1 deletion src/dbse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ class cls_dbse {
void movielist_add(cls_camera *cam, cls_movie *movie, timespec *ts1);
void movielist_get(int p_device_id, lst_movies *p_movielist);
bool restart;
void shutdown();
void startup();
private:
#ifdef HAVE_SQLITE3DB
sqlite3 *database_sqlite3db;
Expand Down Expand Up @@ -137,7 +139,6 @@ class cls_dbse {

void dbse_edits();
bool dbse_open();
void dbse_close();
void exec_sql(std::string sql);

};
Expand Down
21 changes: 14 additions & 7 deletions src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void cls_log::write_msg(int loglvl, int msg_type, int flgerr, int flgfnc, ...)
return;
}

pthread_mutex_lock(&mtx);
pthread_mutex_lock(&mutex_log);

err_save = errno;
memset(msg_full, 0, sizeof(msg_full));
Expand Down Expand Up @@ -229,19 +229,19 @@ void cls_log::write_msg(int loglvl, int msg_type, int flgerr, int flgfnc, ...)
if ((flood_cnt <= 5000) &&
mystreq(msg_flood, &msg_full[prefixlen])) {
flood_cnt++;
pthread_mutex_unlock(&mtx);
pthread_mutex_unlock(&mutex_log);
return;
}

write_flood(loglvl);

write_norm(loglvl, prefixlen);

pthread_mutex_unlock(&mtx);
pthread_mutex_unlock(&mutex_log);

}

void cls_log::log_stop()
void cls_log::shutdown()
{
if (log_file_ptr != nullptr) {
MOTPLS_LOG(NTC, TYPE_ALL, NO_ERRNO, "Closing log_file (%s)."
Expand All @@ -251,6 +251,13 @@ void cls_log::log_stop()
}
}

void cls_log::startup()
{
motlog->log_level = app->cfg->log_level;
motlog->log_fflevel = app->cfg->log_fflevel;
motlog->set_log_file(app->cfg->log_file);
}

cls_log::cls_log(cls_motapp *p_app)
{
app = p_app;
Expand All @@ -261,7 +268,7 @@ cls_log::cls_log(cls_motapp *p_app)
log_file_name = "";
flood_cnt = 0;
set_mode(LOGMODE_SYSLOG);
pthread_mutex_init(&mtx, NULL);
pthread_mutex_init(&mutex_log, NULL);
memset(msg_prefix,0,sizeof(msg_prefix));
memset(msg_flood,0,sizeof(msg_flood));
memset(msg_full,0,sizeof(msg_full));
Expand All @@ -271,8 +278,8 @@ cls_log::cls_log(cls_motapp *p_app)

cls_log::~cls_log()
{
log_stop();
pthread_mutex_destroy(&mtx);
shutdown();
pthread_mutex_destroy(&mutex_log);
}


4 changes: 3 additions & 1 deletion src/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@
int log_fflevel;
void set_log_file(std::string pname);
void write_msg(int loglvl, int msg_type, int flgerr, int flgfnc, ...);
pthread_mutex_t mutex_log;
void shutdown();
void startup();
bool restart;
private:
cls_motapp *app;
pthread_mutex_t mtx;
int log_mode;
FILE *log_file_ptr;
std::string log_file_name;
Expand Down
47 changes: 44 additions & 3 deletions src/motionplus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,48 @@ void cls_motapp::watchdog(uint camindx)

}

void cls_motapp::check_restart()
{
std::string parm_pid_org, parm_pid_new;

if (motlog->restart == true) {
cfg->edit_get("pid_file",parm_pid_org, PARM_CAT_00);
conf_src->edit_get("pid_file",parm_pid_new, PARM_CAT_00);
if (parm_pid_org != parm_pid_new) {
pid_remove();
}

pthread_mutex_lock(&motlog->mutex_log);
motlog->shutdown();
cfg->parms_copy(conf_src, PARM_CAT_00);
motlog->startup();
pthread_mutex_unlock(&motlog->mutex_log);

mytranslate_text("",cfg->native_language);
if (parm_pid_org != parm_pid_new) {
pid_write();
}
motlog->restart = false;
}

if (dbse->restart == true) {
pthread_mutex_lock(&dbse->mutex_dbse);
dbse->shutdown();
cfg->parms_copy(conf_src, PARM_CAT_15);
dbse->startup();
pthread_mutex_lock(&dbse->mutex_dbse);
dbse->restart = false;
}

if (webu->restart == true) {
webu->shutdown();
cfg->parms_copy(conf_src, PARM_CAT_13);
webu->startup();
webu->restart = false;
}

}

bool cls_motapp::check_devices()
{
int indx;
Expand Down Expand Up @@ -664,9 +706,7 @@ void cls_motapp::init(int p_argc, char *p_argv[])
cfg = new cls_config;
cfg->parms_copy(conf_src);

motlog->log_level = cfg->log_level;
motlog->log_fflevel = cfg->log_fflevel;
motlog->set_log_file(cfg->log_file);
motlog->startup();

mytranslate_init();

Expand Down Expand Up @@ -813,6 +853,7 @@ int main (int p_argc, char **p_argv)
}
app->camera_add();
app->camera_delete();
app->check_restart();
}
MOTPLS_LOG(NTC, TYPE_ALL, NO_ERRNO, _("Motionplus devices finished"));
if (app->reload_all) {
Expand Down
1 change: 1 addition & 0 deletions src/motionplus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class cls_motapp {

void signal_process();
bool check_devices();
void check_restart();
void init(int p_argc, char *p_argv[]);
void deinit();
void camera_add();
Expand Down
8 changes: 4 additions & 4 deletions src/webu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ void cls_webu::start_daemon_port2()

}

void cls_webu::webu_start()
void cls_webu::startup()
{
unsigned int randnbr;
wb_daemon = nullptr;
Expand Down Expand Up @@ -506,7 +506,7 @@ void cls_webu::webu_start()

}

void cls_webu::webu_stop()
void cls_webu::shutdown()
{
int chkcnt;

Expand Down Expand Up @@ -542,10 +542,10 @@ void cls_webu::webu_stop()
cls_webu::cls_webu(cls_motapp *p_app)
{
app = p_app;
webu_start();
startup();
}

cls_webu::~cls_webu()
{
webu_stop();
shutdown();
}
Loading

0 comments on commit 0115ed1

Please sign in to comment.