2016-01-15 03:55:07 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2020-02-17 14:27:09 +00:00
|
|
|
|
#include <unistd.h>
|
2016-01-15 03:55:07 +00:00
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <memory>
|
2016-12-12 03:33:34 +00:00
|
|
|
|
#include <functional>
|
2017-11-20 04:15:43 +00:00
|
|
|
|
#include <optional>
|
2017-03-17 00:44:00 +00:00
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <condition_variable>
|
2017-03-20 19:14:00 +00:00
|
|
|
|
#include <atomic>
|
2017-11-23 09:08:52 +00:00
|
|
|
|
#include <chrono>
|
2016-01-15 03:55:07 +00:00
|
|
|
|
#include <Poco/Process.h>
|
|
|
|
|
#include <Poco/ThreadPool.h>
|
|
|
|
|
#include <Poco/TaskNotification.h>
|
|
|
|
|
#include <Poco/Util/Application.h>
|
|
|
|
|
#include <Poco/Util/ServerApplication.h>
|
|
|
|
|
#include <Poco/Net/SocketAddress.h>
|
2017-01-19 18:53:29 +00:00
|
|
|
|
#include <Poco/Version.h>
|
2020-03-19 10:38:34 +00:00
|
|
|
|
#include <common/types.h>
|
2016-01-15 03:55:07 +00:00
|
|
|
|
#include <common/logger_useful.h>
|
2020-02-02 02:35:47 +00:00
|
|
|
|
#include <common/getThreadId.h>
|
2016-01-15 04:13:00 +00:00
|
|
|
|
#include <daemon/GraphiteWriter.h>
|
2018-02-28 20:34:25 +00:00
|
|
|
|
#include <Common/Config/ConfigProcessor.h>
|
2019-06-14 14:00:37 +00:00
|
|
|
|
#include <loggers/Loggers.h>
|
2016-01-15 03:55:07 +00:00
|
|
|
|
|
2019-12-22 17:20:33 +00:00
|
|
|
|
|
2016-01-15 03:55:07 +00:00
|
|
|
|
namespace Poco { class TaskManager; }
|
|
|
|
|
|
|
|
|
|
|
2018-11-21 17:27:13 +00:00
|
|
|
|
/// \brief Base class for applications that can run as deamons.
|
2016-01-15 03:55:07 +00:00
|
|
|
|
///
|
|
|
|
|
/// \code
|
2018-11-21 17:27:13 +00:00
|
|
|
|
/// # Some possible command line options:
|
|
|
|
|
/// # --config-file, -C or --config - path to configuration file. By default - config.xml in the current directory.
|
|
|
|
|
/// # --log-file
|
|
|
|
|
/// # --errorlog-file
|
|
|
|
|
/// # --daemon - run as daemon; without this option, the program will be attached to the terminal and also output logs to stderr.
|
|
|
|
|
/// <daemon_name> --daemon --config-file=localfile.xml --log-file=log.log --errorlog-file=error.log
|
2016-01-15 03:55:07 +00:00
|
|
|
|
/// \endcode
|
|
|
|
|
///
|
2018-11-21 17:27:13 +00:00
|
|
|
|
/// You can configure different log options for different loggers used inside program
|
|
|
|
|
/// by providing subsections to "logger" in configuration file.
|
2019-06-14 14:00:37 +00:00
|
|
|
|
class BaseDaemon : public Poco::Util::ServerApplication, public Loggers
|
2016-01-15 03:55:07 +00:00
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
|
friend class SignalListener;
|
2016-06-08 14:39:19 +00:00
|
|
|
|
|
2016-01-15 03:55:07 +00:00
|
|
|
|
public:
|
2018-11-21 20:56:37 +00:00
|
|
|
|
static inline constexpr char DEFAULT_GRAPHITE_CONFIG_NAME[] = "graphite";
|
2017-08-23 12:05:15 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
BaseDaemon();
|
2018-08-26 01:30:13 +00:00
|
|
|
|
~BaseDaemon() override;
|
2016-01-15 03:55:07 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
/// Загружает конфигурацию и "строит" логгеры на запись в файлы
|
|
|
|
|
void initialize(Poco::Util::Application &) override;
|
|
|
|
|
|
|
|
|
|
/// Читает конфигурацию
|
|
|
|
|
void reloadConfiguration();
|
|
|
|
|
|
|
|
|
|
/// Определяет параметр командной строки
|
2017-08-18 01:00:13 +00:00
|
|
|
|
void defineOptions(Poco::Util::OptionSet & _options) override;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
|
|
/// Заставляет демон завершаться, если хотя бы одна задача завершилась неудачно
|
|
|
|
|
void exitOnTaskError();
|
|
|
|
|
|
|
|
|
|
/// Завершение демона ("мягкое")
|
|
|
|
|
void terminate();
|
|
|
|
|
|
|
|
|
|
/// Завершение демона ("жёсткое")
|
|
|
|
|
void kill();
|
|
|
|
|
|
|
|
|
|
/// Получен ли сигнал на завершение?
|
2017-08-09 14:33:07 +00:00
|
|
|
|
bool isCancelled() const
|
2017-04-01 07:20:54 +00:00
|
|
|
|
{
|
|
|
|
|
return is_cancelled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получение ссылки на экземпляр демона
|
|
|
|
|
static BaseDaemon & instance()
|
|
|
|
|
{
|
|
|
|
|
return dynamic_cast<BaseDaemon &>(Poco::Util::Application::instance());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// return none if daemon doesn't exist, reference to the daemon otherwise
|
2017-11-20 04:15:43 +00:00
|
|
|
|
static std::optional<std::reference_wrapper<BaseDaemon>> tryGetInstance() { return tryGetInstance<BaseDaemon>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
|
|
/// Спит заданное количество секунд или до события wakeup
|
|
|
|
|
void sleep(double seconds);
|
|
|
|
|
|
|
|
|
|
/// Разбудить
|
|
|
|
|
void wakeup();
|
|
|
|
|
|
|
|
|
|
/// В Graphite компоненты пути(папки) разделяются точкой.
|
|
|
|
|
/// У нас принят путь формата root_path.hostname_yandex_ru.key
|
|
|
|
|
/// root_path по умолчанию one_min
|
|
|
|
|
/// key - лучше группировать по смыслу. Например "meminfo.cached" или "meminfo.free", "meminfo.total"
|
|
|
|
|
template <class T>
|
2017-08-23 12:05:15 +00:00
|
|
|
|
void writeToGraphite(const std::string & key, const T & value, const std::string & config_name = DEFAULT_GRAPHITE_CONFIG_NAME, time_t timestamp = 0, const std::string & custom_root_path = "")
|
2017-04-01 07:20:54 +00:00
|
|
|
|
{
|
|
|
|
|
auto writer = getGraphiteWriter(config_name);
|
|
|
|
|
if (writer)
|
|
|
|
|
writer->write(key, value, timestamp, custom_root_path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T>
|
2017-08-23 12:05:15 +00:00
|
|
|
|
void writeToGraphite(const GraphiteWriter::KeyValueVector<T> & key_vals, const std::string & config_name = DEFAULT_GRAPHITE_CONFIG_NAME, time_t timestamp = 0, const std::string & custom_root_path = "")
|
2017-04-01 07:20:54 +00:00
|
|
|
|
{
|
|
|
|
|
auto writer = getGraphiteWriter(config_name);
|
|
|
|
|
if (writer)
|
|
|
|
|
writer->write(key_vals, timestamp, custom_root_path);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-23 09:08:52 +00:00
|
|
|
|
template <class T>
|
|
|
|
|
void writeToGraphite(const GraphiteWriter::KeyValueVector<T> & key_vals, const std::chrono::system_clock::time_point & current_time, const std::string & custom_root_path)
|
|
|
|
|
{
|
|
|
|
|
auto writer = getGraphiteWriter();
|
|
|
|
|
if (writer)
|
|
|
|
|
writer->write(key_vals, std::chrono::system_clock::to_time_t(current_time), custom_root_path);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-23 12:05:15 +00:00
|
|
|
|
GraphiteWriter * getGraphiteWriter(const std::string & config_name = DEFAULT_GRAPHITE_CONFIG_NAME)
|
2017-04-01 07:20:54 +00:00
|
|
|
|
{
|
|
|
|
|
if (graphite_writers.count(config_name))
|
|
|
|
|
return graphite_writers[config_name].get();
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-12 12:23:22 +00:00
|
|
|
|
/// close all process FDs except
|
|
|
|
|
/// 0-2 -- stdin, stdout, stderr
|
|
|
|
|
/// also doesn't close global internal pipes for signal handling
|
2020-03-18 00:57:00 +00:00
|
|
|
|
static void closeFDs();
|
2018-08-12 12:23:22 +00:00
|
|
|
|
|
2016-01-15 03:55:07 +00:00
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
|
/// Возвращает TaskManager приложения
|
|
|
|
|
/// все методы task_manager следует вызывать из одного потока
|
|
|
|
|
/// иначе возможен deadlock, т.к. joinAll выполняется под локом, а любой метод тоже берет лок
|
|
|
|
|
Poco::TaskManager & getTaskManager() { return *task_manager; }
|
2016-02-19 11:07:11 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
virtual void logRevision() const;
|
2016-01-15 03:55:07 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
/// Используется при exitOnTaskError()
|
|
|
|
|
void handleNotification(Poco::TaskFailedNotification *);
|
2016-01-15 03:55:07 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
/// thread safe
|
|
|
|
|
virtual void handleSignal(int signal_id);
|
2016-06-08 14:39:19 +00:00
|
|
|
|
|
2018-08-07 17:57:44 +00:00
|
|
|
|
/// initialize termination process and signal handlers
|
|
|
|
|
virtual void initializeTerminationAndSignalProcessing();
|
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
/// реализация обработки сигналов завершения через pipe не требует блокировки сигнала с помощью sigprocmask во всех потоках
|
|
|
|
|
void waitForTerminationRequest()
|
2019-02-15 11:46:07 +00:00
|
|
|
|
#if defined(POCO_CLICKHOUSE_PATCH) || POCO_VERSION >= 0x02000000 // in old upstream poco not vitrual
|
2017-04-01 07:20:54 +00:00
|
|
|
|
override
|
2017-01-13 11:25:44 +00:00
|
|
|
|
#endif
|
2017-04-01 07:20:54 +00:00
|
|
|
|
;
|
|
|
|
|
/// thread safe
|
|
|
|
|
virtual void onInterruptSignals(int signal_id);
|
2016-06-08 14:39:19 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
template <class Daemon>
|
2017-11-20 04:15:43 +00:00
|
|
|
|
static std::optional<std::reference_wrapper<Daemon>> tryGetInstance();
|
2016-09-05 16:29:21 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
virtual std::string getDefaultCorePath() const;
|
2017-01-09 13:42:29 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
std::unique_ptr<Poco::TaskManager> task_manager;
|
2016-01-15 03:55:07 +00:00
|
|
|
|
|
2018-11-21 17:27:13 +00:00
|
|
|
|
/// RAII wrapper for pid file.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
struct PID
|
|
|
|
|
{
|
|
|
|
|
std::string file;
|
2016-01-15 03:55:07 +00:00
|
|
|
|
|
2020-01-10 16:47:56 +00:00
|
|
|
|
PID(const std::string & file_);
|
|
|
|
|
~PID();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
};
|
2016-01-15 03:55:07 +00:00
|
|
|
|
|
2020-01-10 16:47:56 +00:00
|
|
|
|
std::optional<PID> pid;
|
2016-01-15 03:55:07 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
std::atomic_bool is_cancelled{false};
|
2016-01-15 03:55:07 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
/// Флаг устанавливается по сообщению из Task (при аварийном завершении).
|
|
|
|
|
bool task_failed = false;
|
2016-05-12 19:40:23 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
bool log_to_console = false;
|
2016-01-15 03:55:07 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
/// Событие, чтобы проснуться во время ожидания
|
|
|
|
|
Poco::Event wakeup_event;
|
2016-01-15 03:55:07 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
/// Поток, в котором принимается сигнал HUP/USR1 для закрытия логов.
|
|
|
|
|
Poco::Thread signal_listener_thread;
|
|
|
|
|
std::unique_ptr<Poco::Runnable> signal_listener;
|
2016-01-15 03:55:07 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
std::map<std::string, std::unique_ptr<GraphiteWriter>> graphite_writers;
|
2016-01-15 03:55:07 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
std::mutex signal_handler_mutex;
|
|
|
|
|
std::condition_variable signal_event;
|
|
|
|
|
std::atomic_size_t terminate_signals_counter{0};
|
|
|
|
|
std::atomic_size_t sigint_signals_counter{0};
|
2017-03-17 00:44:00 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
std::string config_path;
|
2018-11-27 16:11:46 +00:00
|
|
|
|
DB::ConfigProcessor::LoadedConfig loaded_config;
|
2017-06-19 13:31:55 +00:00
|
|
|
|
Poco::Util::AbstractConfiguration * last_configuration = nullptr;
|
2016-01-15 03:55:07 +00:00
|
|
|
|
};
|
2016-09-05 16:29:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class Daemon>
|
2017-11-20 04:15:43 +00:00
|
|
|
|
std::optional<std::reference_wrapper<Daemon>> BaseDaemon::tryGetInstance()
|
2016-09-05 16:29:21 +00:00
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
|
Daemon * ptr = nullptr;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ptr = dynamic_cast<Daemon *>(&Poco::Util::Application::instance());
|
|
|
|
|
}
|
|
|
|
|
catch (const Poco::NullPointerException &)
|
|
|
|
|
{
|
|
|
|
|
/// if daemon doesn't exist than instance() throw NullPointerException
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ptr)
|
2017-11-20 04:15:43 +00:00
|
|
|
|
return std::optional<std::reference_wrapper<Daemon>>(*ptr);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
else
|
|
|
|
|
return {};
|
2016-09-05 16:29:21 +00:00
|
|
|
|
}
|