fix: restart-interval

pull/676/head
Alex 2020-05-24 21:33:38 +02:00
parent 49ae944d65
commit 7b4ded306b
2 changed files with 22 additions and 20 deletions

View File

@ -1,9 +1,10 @@
#pragma once #pragma once
#include <giomm.h> #include <giomm.h>
#include <sys/wait.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#include <array> #include <array>
namespace waybar::util::command { namespace waybar::util::command {
@ -51,7 +52,7 @@ inline int close(FILE* fp, pid_t pid) {
return stat; return stat;
} }
inline FILE* open(const std::string cmd, int& pid) { inline FILE* open(const std::string& cmd, int& pid) {
if (cmd == "") return nullptr; if (cmd == "") return nullptr;
int fd[2]; int fd[2];
pipe(fd); pipe(fd);
@ -76,7 +77,7 @@ inline FILE* open(const std::string cmd, int& pid) {
return fdopen(fd[0], "r"); return fdopen(fd[0], "r");
} }
inline struct res exec(std::string cmd) { inline struct res exec(const std::string& cmd) {
int pid; int pid;
auto fp = command::open(cmd, pid); auto fp = command::open(cmd, pid);
if (!fp) return {-1, ""}; if (!fp) return {-1, ""};
@ -85,7 +86,7 @@ inline struct res exec(std::string cmd) {
return {WEXITSTATUS(stat), output}; return {WEXITSTATUS(stat), output};
} }
inline struct res execNoRead(std::string cmd) { inline struct res execNoRead(const std::string& cmd) {
int pid; int pid;
auto fp = command::open(cmd, pid); auto fp = command::open(cmd, pid);
if (!fp) return {-1, ""}; if (!fp) return {-1, ""};
@ -93,7 +94,7 @@ inline struct res execNoRead(std::string cmd) {
return {WEXITSTATUS(stat), ""}; return {WEXITSTATUS(stat), ""};
} }
inline int32_t forkExec(std::string cmd) { inline int32_t forkExec(const std::string& cmd) {
if (cmd == "") return -1; if (cmd == "") return -1;
int32_t pid = fork(); int32_t pid = fork();
@ -110,7 +111,7 @@ inline int32_t forkExec(std::string cmd) {
execl("/bin/sh", "sh", "-c", cmd.c_str(), (char*)0); execl("/bin/sh", "sh", "-c", cmd.c_str(), (char*)0);
exit(0); exit(0);
} else { } else {
signal(SIGCHLD,SIG_IGN); signal(SIGCHLD, SIG_IGN);
} }
return pid; return pid;

View File

@ -50,7 +50,7 @@ void waybar::modules::Custom::continuousWorker() {
if (!fp_) { if (!fp_) {
throw std::runtime_error("Unable to open " + cmd); throw std::runtime_error("Unable to open " + cmd);
} }
thread_ = [&] { thread_ = [this, cmd] {
char* buff = nullptr; char* buff = nullptr;
size_t len = 0; size_t len = 0;
bool restart = false; bool restart = false;
@ -67,11 +67,16 @@ void waybar::modules::Custom::continuousWorker() {
} }
if (config_["restart-interval"].isUInt()) { if (config_["restart-interval"].isUInt()) {
restart = true; restart = true;
pid_ = -1;
fp_ = util::command::open(cmd, pid_);
if (!fp_) {
throw std::runtime_error("Unable to open " + cmd);
}
} else { } else {
thread_.stop(); thread_.stop();
return; return;
} }
} } else {
std::string output = buff; std::string output = buff;
// Remove last newline // Remove last newline
@ -80,12 +85,8 @@ void waybar::modules::Custom::continuousWorker() {
} }
output_ = {0, output}; output_ = {0, output};
dp.emit(); dp.emit();
if (restart) {
pid_ = -1;
fp_ = util::command::open(cmd, pid_);
if (!fp_) {
throw std::runtime_error("Unable to open " + cmd);
} }
if (restart) {
thread_.sleep_for(std::chrono::seconds(config_["restart-interval"].asUInt())); thread_.sleep_for(std::chrono::seconds(config_["restart-interval"].asUInt()));
} }
}; };