Skip to content

Commit 90720c3

Browse files
carykees98aHalliday13
authored andcommitted
log stderr for failing rsync processes
(cherry picked from commit 20a49bb)
1 parent d83280a commit 90720c3

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

module/sync/include/mirror/sync_scheduler/JobManager.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class JobManager
5353
auto kill_all_jobs() -> void;
5454
auto reap_processes() -> std::vector<::pid_t>;
5555
auto deregister_jobs(const std::vector<::pid_t>& completedJobs) -> void;
56+
auto JobManager::write_stderr_to_file(const ::pid_t processID)
57+
-> std::string;
5658
auto process_reaper(const std::stop_token& stopToken) -> void;
5759

5860
private: // Static Methods

module/sync/src/JobManager.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <mirror/sync_scheduler/JobManager.hpp>
99

1010
// System Includes
11+
#include <sys/ioctl.h>
1112
#include <sys/types.h>
1213
#include <sys/wait.h>
1314
#include <unistd.h>
@@ -26,6 +27,7 @@
2627
#include <fstream>
2728
#include <iterator>
2829
#include <mutex>
30+
#include <random>
2931
#include <stop_token>
3032
#include <string>
3133
#include <thread>
@@ -196,9 +198,14 @@ auto JobManager::reap_processes() -> std::vector<::pid_t>
196198
}
197199
else if (isKnownJob && exitStatus != EXIT_SUCCESS)
198200
{
201+
const auto logFileName
202+
= this->write_stderr_to_file(childProcessID);
203+
199204
spdlog::warn(
200-
"Project {} failed to sync! Exit code: {} (pid: {})",
205+
"Project {} failed to sync! The contents of STDERR for the "
206+
"process have been written to {}! Exit code: {} (pid: {})",
201207
m_ActiveJobs.at(childProcessID).jobName,
208+
logFileName,
202209
exitStatus,
203210
childProcessID
204211
);
@@ -225,6 +232,40 @@ auto JobManager::reap_processes() -> std::vector<::pid_t>
225232
return completedJobs;
226233
}
227234

235+
auto JobManager::write_stderr_to_file(const ::pid_t processID) -> std::string
236+
{
237+
static std::random_device randomDevice;
238+
static std::mt19937 randomGenerator(randomDevice());
239+
static std::uniform_int_distribution distribution(1, 10000);
240+
241+
const auto logNumber = distribution(randomGenerator);
242+
const auto errorLogPath = std::filesystem::relative("error-logs");
243+
244+
auto logFileName = std::format(
245+
"{}-{}-stderr.log",
246+
m_ActiveJobs.at(processID).jobName,
247+
logNumber
248+
);
249+
250+
std::ofstream logFile(errorLogPath / logFileName);
251+
252+
int bytesAvailable = 0;
253+
::ioctl(m_ActiveJobs.at(processID).stderrPipe, FIONREAD, &bytesAvailable);
254+
255+
std::string stderrContents;
256+
stderrContents.resize(bytesAvailable + 1);
257+
258+
::read(
259+
m_ActiveJobs.at(processID).stderrPipe,
260+
stderrContents.data(),
261+
bytesAvailable
262+
);
263+
264+
logFile << stderrContents;
265+
266+
return logFileName;
267+
}
268+
228269
// NOLINTNEXTLINE(*-no-recursion)
229270
auto JobManager::interrupt_job(const ::pid_t processID) -> void
230271
{

0 commit comments

Comments
 (0)