# HG changeset patch # User Michiel Broek # Date 1658484905 -7200 # Node ID cf145b35d65f7268eb17f71fe5f6a5a161f4b990 # Parent 67e0384c0e6316e7a3fab464357aff9d4c784aa4 Version 0.3.41. Use INSERT IGNORE .. for insert log messages from fermenters, co2meters and ispindels. Connect to MySQL servers with MYSQL_OPT_RECONNECT instead of manual working around the connection lost problem if a mysql server is restarted. Removed unused bms_mysql_ping function. diff -r 67e0384c0e63 -r cf145b35d65f bmsd/co2meters.c --- a/bmsd/co2meters.c Tue Jul 19 13:55:53 2022 +0200 +++ b/bmsd/co2meters.c Fri Jul 22 12:15:05 2022 +0200 @@ -440,7 +440,7 @@ * Build the MySQL log */ if (trigger) { - snprintf(query, 511, "INSERT INTO log_co2pressure SET code='%s', datetime='%s', temperature='%.4f', " \ + snprintf(query, 511, "INSERT IGNORE INTO log_co2pressure SET code='%s', datetime='%s', temperature='%.4f', " \ "pressure='%.4f', uuid='%s'", log->product_code, log->datetime, log->temperature, log->pressure, log->uuid); //syslog(LOG_NOTICE, "%s", query); diff -r 67e0384c0e63 -r cf145b35d65f bmsd/fermenters.c --- a/bmsd/fermenters.c Tue Jul 19 13:55:53 2022 +0200 +++ b/bmsd/fermenters.c Fri Jul 22 12:15:05 2022 +0200 @@ -936,7 +936,7 @@ trigger = true; if (trigger) { snprintf(query, 511, - "INSERT INTO log_fermenter SET code='%s', datetime='%s', mode='%s', stage='%s', " \ + "INSERT IGNORE INTO log_fermenter SET code='%s', datetime='%s', mode='%s', stage='%s', " \ "temp_air='%.4f', temp_beer='%.4f', temp_chiller='%.4f', temp_room='%.3f', " \ "sp_low='%.3f', sp_high='%.3f', heater_power='%d', cooler_power='%d', " \ "event='%s', fermenter_uuid='%s'", diff -r 67e0384c0e63 -r cf145b35d65f bmsd/ispindels.c --- a/bmsd/ispindels.c Tue Jul 19 13:55:53 2022 +0200 +++ b/bmsd/ispindels.c Fri Jul 22 12:15:05 2022 +0200 @@ -327,7 +327,7 @@ line = xstrcat(line, (char *)","); line = xstrcat(line, ispindel->uuid); - snprintf(query, 511, "INSERT INTO log_ispindel SET code='%s', datetime='%s', " \ + snprintf(query, 511, "INSERT IGNORE INTO log_ispindel SET code='%s', datetime='%s', " \ "temperature='%.4f', plato='%.5f', sg='%.5f', battery='%.6f', " \ "angle='%.5f', refresh='%d', uuid='%s'", ispindel->beercode, datetime, ispindel->temperature, ispindel->gravity, diff -r 67e0384c0e63 -r cf145b35d65f bmsd/mysql.c --- a/bmsd/mysql.c Tue Jul 19 13:55:53 2022 +0200 +++ b/bmsd/mysql.c Fri Jul 22 12:15:05 2022 +0200 @@ -3,7 +3,7 @@ * @brief MySQL/MariaDB access. * @author Michiel Broek * - * Copyright (C) 2018-2021 + * Copyright (C) 2018-2022 * * This file is part of the bms (Brewery Management System) * @@ -32,6 +32,8 @@ MYSQL_RES *res_set; MYSQL_ROW row; +my_bool reconnect = 1; + extern sys_config Config; extern sys_node_list *nodes; extern sys_fermenter_list *fermenters; @@ -72,6 +74,8 @@ return 1; } + mysql_options(con, MYSQL_OPT_RECONNECT, &reconnect); + if (mysql_real_connect(con, Config.mysql_host, Config.mysql_user, Config.mysql_pass, Config.mysql_database, Config.mysql_port, NULL, 0) == NULL) { syslog(LOG_NOTICE, "MySQL: mysql_real_connect() %s", mysql_error(con)); return 2; @@ -475,34 +479,11 @@ return 0; } - /* Any error execpt server gone away */ - if (err != 2006) - return rc; - - syslog(LOG_NOTICE, "Trying to reconnect"); - /* Try to reconnect and do the query again */ - mysql_close(con); - if (mysql_real_connect(con, Config.mysql_host, Config.mysql_user, Config.mysql_pass, Config.mysql_database, Config.mysql_port, NULL, 0) == NULL) { - syslog(LOG_NOTICE, "MySQL: mysql_real_connect() %s", mysql_error(con)); - return 2; - } - syslog(LOG_NOTICE, "MySQL: reconnected."); - rc = mysql_query(con, query); - if (rc) { - syslog(LOG_NOTICE, "MySQL: error %u (%s)", mysql_errno(con), mysql_error(con)); - syslog(LOG_NOTICE, query); - } return rc; } -void bms_mysql_ping(void) -{ -} - - - void node_mysql_insert(sys_node_list *node) { char *query = malloc(1024), first[73], last[73]; diff -r 67e0384c0e63 -r cf145b35d65f bmsd/mysql.h --- a/bmsd/mysql.h Tue Jul 19 13:55:53 2022 +0200 +++ b/bmsd/mysql.h Fri Jul 22 12:15:05 2022 +0200 @@ -24,11 +24,6 @@ */ int bms_mysql_query(const char *query); -/** - * @brief Ping MySQL connection and try to reconnect if the connection is broken. - */ -void bms_mysql_ping(void); - void node_mysql_insert(sys_node_list *node); void node_mysql_update(sys_node_list *node); void node_mysql_death(char *node); diff -r 67e0384c0e63 -r cf145b35d65f configure --- a/configure Tue Jul 19 13:55:53 2022 +0200 +++ b/configure Fri Jul 22 12:15:05 2022 +0200 @@ -2031,7 +2031,7 @@ PACKAGE="bms" -VERSION="0.3.40" +VERSION="0.3.41" COPYRIGHT="Copyright (C) 2016-2022 Michiel Broek, All Rights Reserved" CYEARS="2016-2022" diff -r 67e0384c0e63 -r cf145b35d65f configure.ac --- a/configure.ac Tue Jul 19 13:55:53 2022 +0200 +++ b/configure.ac Fri Jul 22 12:15:05 2022 +0200 @@ -8,7 +8,7 @@ dnl General settings dnl After changeing the version number, run autoconf! PACKAGE="bms" -VERSION="0.3.40" +VERSION="0.3.41" COPYRIGHT="Copyright (C) 2016-2022 Michiel Broek, All Rights Reserved" CYEARS="2016-2022" AC_SUBST(PACKAGE)