2015-07-07 10:17:21 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <pqxx/pqxx>
|
|
|
|
|
|
2025-04-07 11:36:59 -04:00
|
|
|
#include <nix/util/environment-variables.hh>
|
|
|
|
|
#include <nix/util/util.hh>
|
2015-07-07 10:17:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Connection : pqxx::connection
|
|
|
|
|
{
|
|
|
|
|
Connection() : pqxx::connection(getFlags()) { };
|
|
|
|
|
|
2015-07-07 10:29:43 +02:00
|
|
|
std::string getFlags()
|
2015-07-07 10:17:21 +02:00
|
|
|
{
|
2015-07-07 10:29:43 +02:00
|
|
|
using namespace nix;
|
2019-12-30 22:49:26 +01:00
|
|
|
auto s = getEnv("HYDRA_DBI").value_or("dbi:Pg:dbname=hydra;");
|
2021-02-22 13:47:41 -05:00
|
|
|
|
|
|
|
|
std::string lower_prefix = "dbi:Pg:";
|
|
|
|
|
std::string upper_prefix = "DBI:Pg:";
|
|
|
|
|
|
2021-02-24 07:00:26 -05:00
|
|
|
if (hasPrefix(s, lower_prefix) || hasPrefix(s, upper_prefix)) {
|
2022-03-09 23:50:30 +01:00
|
|
|
return concatStringsSep(" ", tokenizeString<Strings>(std::string(s, lower_prefix.size()), ";"));
|
2021-02-22 13:47:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw Error("$HYDRA_DBI does not denote a PostgreSQL database");
|
2015-07-07 10:17:21 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2025-08-03 12:03:00 +02:00
|
|
|
class receiver
|
2015-07-07 10:17:21 +02:00
|
|
|
{
|
2020-01-07 12:38:06 +13:00
|
|
|
std::optional<std::string> status;
|
2025-08-03 12:03:00 +02:00
|
|
|
pqxx::connection & conn;
|
2017-07-21 14:25:33 +02:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2015-07-07 10:17:21 +02:00
|
|
|
receiver(pqxx::connection_base & c, const std::string & channel)
|
2025-08-03 12:03:00 +02:00
|
|
|
: conn(static_cast<pqxx::connection &>(c))
|
2015-07-07 10:17:21 +02:00
|
|
|
{
|
2025-08-03 12:03:00 +02:00
|
|
|
conn.listen(channel, [this](pqxx::notification n) {
|
|
|
|
|
status = std::string(n.payload);
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-07-21 14:25:33 +02:00
|
|
|
|
2020-01-07 12:38:06 +13:00
|
|
|
std::optional<std::string> get() {
|
2017-07-21 14:25:33 +02:00
|
|
|
auto s = status;
|
2020-01-07 12:38:06 +13:00
|
|
|
status = std::nullopt;
|
2017-07-21 14:25:33 +02:00
|
|
|
return s;
|
2015-07-07 10:17:21 +02:00
|
|
|
}
|
|
|
|
|
};
|