refactor(custom): move continuous exec onto GLib command stream
Replace the custom module's continuous getline() worker with the new GLib-backed command stream helper. This moves line delivery, child exit handling, and restart scheduling onto the main loop so continuous commands no longer depend on a blocking FILE* read inside SleeperThread. The behavior is kept aligned with the old module semantics: stdout lines still emit updates, non-zero exits still surface as errors, and restart-interval still respawns the command. Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
This commit is contained in:
committed by
Austin Horstman
parent
560f02509b
commit
5f4b96ad1d
@@ -3,10 +3,12 @@
|
|||||||
#include <fmt/format.h>
|
#include <fmt/format.h>
|
||||||
|
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "AIconLabel.hpp"
|
#include "AIconLabel.hpp"
|
||||||
#include "util/command.hpp"
|
#include "util/command.hpp"
|
||||||
|
#include "util/command_line_stream.hpp"
|
||||||
#include "util/json.hpp"
|
#include "util/json.hpp"
|
||||||
#include "util/sleeper_thread.hpp"
|
#include "util/sleeper_thread.hpp"
|
||||||
|
|
||||||
@@ -22,6 +24,9 @@ class Custom : public AIconLabel {
|
|||||||
private:
|
private:
|
||||||
void delayWorker();
|
void delayWorker();
|
||||||
void continuousWorker();
|
void continuousWorker();
|
||||||
|
void startContinuousProcess(bool throw_on_failure);
|
||||||
|
void handleContinuousProcessExit(int exit_code);
|
||||||
|
void scheduleContinuousRestart();
|
||||||
void waitingWorker();
|
void waitingWorker();
|
||||||
void parseOutputRaw();
|
void parseOutputRaw();
|
||||||
void parseOutputJson();
|
void parseOutputJson();
|
||||||
@@ -42,10 +47,10 @@ class Custom : public AIconLabel {
|
|||||||
const bool tooltip_format_enabled_;
|
const bool tooltip_format_enabled_;
|
||||||
std::vector<std::string> class_;
|
std::vector<std::string> class_;
|
||||||
int percentage_;
|
int percentage_;
|
||||||
FILE* fp_;
|
|
||||||
int pid_;
|
|
||||||
util::command::res output_;
|
util::command::res output_;
|
||||||
util::JsonParser parser_;
|
util::JsonParser parser_;
|
||||||
|
std::unique_ptr<util::command::LineStream> continuous_stream_;
|
||||||
|
sigc::connection restart_connection_;
|
||||||
|
|
||||||
util::SleeperThread thread_;
|
util::SleeperThread thread_;
|
||||||
};
|
};
|
||||||
|
|||||||
+49
-46
@@ -2,10 +2,9 @@
|
|||||||
|
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include "util/scope_guard.hpp"
|
|
||||||
|
|
||||||
waybar::modules::Custom::Custom(const std::string& name, const std::string& id,
|
waybar::modules::Custom::Custom(const std::string& name, const std::string& id,
|
||||||
const Json::Value& config, const std::string& output_name)
|
const Json::Value& config, const std::string& output_name)
|
||||||
: AIconLabel(config, "custom-" + name, id, "{}"),
|
: AIconLabel(config, "custom-" + name, id, "{}"),
|
||||||
@@ -13,9 +12,7 @@ waybar::modules::Custom::Custom(const std::string& name, const std::string& id,
|
|||||||
output_name_(output_name),
|
output_name_(output_name),
|
||||||
id_(id),
|
id_(id),
|
||||||
tooltip_format_enabled_{config_["tooltip-format"].isString()},
|
tooltip_format_enabled_{config_["tooltip-format"].isString()},
|
||||||
percentage_(0),
|
percentage_(0) {
|
||||||
fp_(nullptr),
|
|
||||||
pid_(-1) {
|
|
||||||
if (config.isNull()) {
|
if (config.isNull()) {
|
||||||
spdlog::warn("There is no configuration for 'custom/{}', element will be hidden", name);
|
spdlog::warn("There is no configuration for 'custom/{}', element will be hidden", name);
|
||||||
}
|
}
|
||||||
@@ -40,10 +37,9 @@ waybar::modules::Custom::Custom(const std::string& name, const std::string& id,
|
|||||||
}
|
}
|
||||||
|
|
||||||
waybar::modules::Custom::~Custom() {
|
waybar::modules::Custom::~Custom() {
|
||||||
if (pid_ != -1) {
|
restart_connection_.disconnect();
|
||||||
killpg(pid_, SIGTERM);
|
if (continuous_stream_) {
|
||||||
waitpid(pid_, NULL, 0);
|
continuous_stream_->stop();
|
||||||
pid_ = -1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,57 +76,64 @@ void waybar::modules::Custom::delayWorker() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void waybar::modules::Custom::continuousWorker() {
|
void waybar::modules::Custom::continuousWorker() {
|
||||||
auto cmd = config_["exec"].asString();
|
continuous_stream_ = std::make_unique<util::command::LineStream>(
|
||||||
pid_ = -1;
|
output_name_,
|
||||||
fp_ = util::command::open(cmd, pid_, output_name_);
|
[this](const std::string& output) {
|
||||||
if (!fp_) {
|
output_ = {0, output};
|
||||||
throw std::runtime_error("Unable to open " + cmd);
|
dp.emit();
|
||||||
|
},
|
||||||
|
[this](int exit_code) { handleContinuousProcessExit(exit_code); });
|
||||||
|
startContinuousProcess(true);
|
||||||
}
|
}
|
||||||
thread_ = [this, cmd] {
|
|
||||||
char* buff = nullptr;
|
void waybar::modules::Custom::startContinuousProcess(bool throw_on_failure) {
|
||||||
waybar::util::ScopeGuard buff_deleter([&buff]() {
|
const auto cmd = config_["exec"].asString();
|
||||||
if (buff) {
|
|
||||||
free(buff);
|
try {
|
||||||
|
continuous_stream_->start(cmd);
|
||||||
|
} catch (const Glib::SpawnError& e) {
|
||||||
|
if (throw_on_failure) {
|
||||||
|
throw std::runtime_error("Unable to open " + cmd + ": " + e.what().raw());
|
||||||
}
|
}
|
||||||
});
|
output_ = {1, ""};
|
||||||
size_t len = 0;
|
dp.emit();
|
||||||
if (getline(&buff, &len, fp_) == -1) {
|
spdlog::error("Unable to restart {}: {}", name_, e.what().raw());
|
||||||
int exit_code = 1;
|
scheduleContinuousRestart();
|
||||||
if (fp_) {
|
} catch (const std::exception& e) {
|
||||||
exit_code = WEXITSTATUS(util::command::close(fp_, pid_));
|
if (throw_on_failure) {
|
||||||
fp_ = nullptr;
|
throw;
|
||||||
}
|
}
|
||||||
|
output_ = {1, ""};
|
||||||
|
dp.emit();
|
||||||
|
spdlog::error("Unable to restart {}: {}", name_, e.what());
|
||||||
|
scheduleContinuousRestart();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void waybar::modules::Custom::handleContinuousProcessExit(int exit_code) {
|
||||||
if (exit_code != 0) {
|
if (exit_code != 0) {
|
||||||
output_ = {exit_code, ""};
|
output_ = {exit_code, ""};
|
||||||
dp.emit();
|
dp.emit();
|
||||||
spdlog::error("{} stopped unexpectedly, is it endless?", name_);
|
spdlog::error("{} stopped unexpectedly, is it endless?", name_);
|
||||||
}
|
}
|
||||||
if (config_["restart-interval"].isNumeric() && config_["restart-interval"].asDouble() > 0) {
|
|
||||||
pid_ = -1;
|
scheduleContinuousRestart();
|
||||||
thread_.sleep_for(std::chrono::milliseconds(
|
|
||||||
std::max(1L, // Minimum 1ms due to millisecond precision
|
|
||||||
static_cast<long>(config_["restart-interval"].asDouble() * 1000))));
|
|
||||||
fp_ = util::command::open(cmd, pid_, output_name_);
|
|
||||||
if (!fp_) {
|
|
||||||
throw std::runtime_error("Unable to open " + cmd);
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
|
void waybar::modules::Custom::scheduleContinuousRestart() {
|
||||||
|
restart_connection_.disconnect();
|
||||||
|
if (!config_["restart-interval"].isNumeric() || config_["restart-interval"].asDouble() <= 0) {
|
||||||
// A non-positive restart-interval must not busy-respawn the script
|
// A non-positive restart-interval must not busy-respawn the script
|
||||||
// (that starves the GTK main loop); treat it as "do not restart".
|
// (that starves the GTK main loop); treat it as "do not restart".
|
||||||
thread_.stop();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
std::string output = buff;
|
|
||||||
|
|
||||||
// Remove last newline
|
restart_connection_ = Glib::signal_timeout().connect(
|
||||||
if (!output.empty() && output[output.length() - 1] == '\n') {
|
[this] {
|
||||||
output.erase(output.length() - 1);
|
startContinuousProcess(false);
|
||||||
}
|
return false;
|
||||||
output_ = {0, output};
|
},
|
||||||
dp.emit();
|
std::max(1U, static_cast<unsigned>(config_["restart-interval"].asDouble() * 1000)));
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void waybar::modules::Custom::waitingWorker() {
|
void waybar::modules::Custom::waitingWorker() {
|
||||||
|
|||||||
Reference in New Issue
Block a user