(FIX): Simplified the watchdog.

Now it runs the way its expected to, just needs to be wired up into the
parser somehow.
This commit is contained in:
Hayden Hargreaves 2025-10-28 18:45:18 -07:00
parent 3f76dafbc1
commit 862c6e7c92
3 changed files with 132 additions and 174 deletions

View File

@ -1,114 +1,76 @@
#include "watchdog.h" #include "watchdog.h"
#include <chrono>
#include <stdexcept>
#include <thread>
namespace fs = std::filesystem; // makes it easier to read namespace fs = std::filesystem;
using namespace std::chrono;
void Watchdog::Start() { void Watchdog::Start() {
// checks if file exist // checks if file exist
if (!fs::exists(path)) { if (!fs::exists(this->path))
// returns and sets parameters to false if file doesnt exist throw std::runtime_error("File does not exist.");
this->watching = false;
this->has_initial_time = false; // Loop forever and check the file.
std::cout << "Watchdog: File does not exists: " << path << std::endl; while (true) {
return; CheckFile();
std::this_thread::sleep_for(this->POLLING_INTERVAL);
} }
// grabs intial write time
this->last_write_time = fs::last_write_time(path);
this->watching = true;
this->has_initial_time = true;
std::cout << "Watchdog: Started" << std::endl;
} }
bool Watchdog::CheckFile() { void Watchdog::CheckFile() {
// If not watching returns false // LONG STORY SHORT:
if (!watching) // When a file is written to, it aquires an OS lock, which means our program
return false; // cannot access it. So when we request, it fails. So we should basically just
// keep trying until we get access.
//
// After implementing some retry logic, I was unable to replicate the error.
try { for (int attempt = 0; attempt < this->MAX_RETRIES; ++attempt) {
// Checking if file was deleted try {
if (!fs::exists(path)) { // Checking if file was deleted
if (this->has_initial_time) { if (!fs::exists(this->path))
std::cout << "Watchdog: File was delete: " << path << std::endl; throw std::runtime_error(
this->has_initial_time = false; "File could not be found. Maybe it was deleted?");
return true;
// Built in function with file system to check last write time
fs::file_time_type currentWriteTime = fs::last_write_time(path);
// File modified
if (currentWriteTime != this->last_write_time) {
this->last_write_time = currentWriteTime;
time_point before = high_resolution_clock::now();
// DO SOMETHING
std::this_thread::sleep_for(this->POLLING_INTERVAL);
time_point after = high_resolution_clock::now();
duration dur = after - before;
long ms = std::chrono::duration_cast<milliseconds>(dur).count();
std::cout << std::endl
<< "Recompiled in \033[36m" << ms << "ms\033[0m" << std::endl;
} }
return false; } catch (const std::exception &ex) {
} // On last attempt, bubble the error outward
if (attempt == this->MAX_RETRIES - 1)
throw std::runtime_error("Watchdog failed after multiple retries: " +
std::string(ex.what()));
// Built in function with file system to check last write tim // Wait and then try again
fs::file_time_type currentWriteTime = fs::last_write_time(path); std::this_thread::sleep_for(this->RETRY_DELAY);
// File was just created
if (!this->has_initial_time) {
this->last_write_time = currentWriteTime;
this->has_initial_time = true;
std::cout << "Watchdog: File created: " << path << std::endl;
return true;
} }
// File modified
if (currentWriteTime != this->last_write_time) {
this->last_write_time = currentWriteTime;
std::cout << "Watchdog: File modifed at "
<< TimePointToString(this->last_write_time) << std::endl;
return true;
}
} catch (const fs::filesystem_error &e) {
// File deleted, inaccessible, or path invalid
if (this->has_initial_time) {
std::cout << "Watchdog: File deleted or inaccessible: " << path
<< std::endl;
this->has_initial_time = false;
return true;
}
return false;
} catch (const std::exception &e) {
std::cerr << "Watchdog: Unexpected error checking file: " << e.what()
<< std::endl;
return false;
} }
// No change
return false;
} }
std::string Watchdog::TimePointToString(const fs::file_time_type &timePoint) { std::string Watchdog::TimePointToString(const fs::file_time_type &timePoint) {
/** system_clock::time_point systemTimePoint =
* Step 1: timePoint time_point_cast<system_clock::duration>(
* - This is the last write time of the file, returned by std::filesystem. timePoint - fs::file_time_type::clock::now() + system_clock::now());
* - Its clock is platform-dependent (filesystem clock).
*
* Step 2: fs::file_time_type::clock::now()
* - Current time according to the filesystem clock.
*
* Step 3: std::chrono::system_clock::now()
* - Current time according to the system clock (standard C++ clock).
*
* Conversion formula:
* timePoint - fs::file_time_type::clock::now() +
* std::chrono::system_clock::now()
*
* Explanation:
* a) timePoint - fs::file_time_type::clock::now()
* - Calculates the duration between the file's last write time and "now"
* according to the filesystem clock.
* b) + std::chrono::system_clock::now()
* - Shifts that duration to align with the system clock timeline.
* c) std::chrono::time_point_cast<std::chrono::system_clock::duration>(...)
* - Ensures the resulting time_point uses the correct duration type
* for std::chrono::system_clock.
*
* Result:
* - systemTime is a std::chrono::system_clock::time_point representing
* the same instant as timePoint, but compatible with system_clock.
*/
std::chrono::system_clock::time_point systemTimePoint =
std::chrono::time_point_cast<std::chrono::system_clock::duration>(
timePoint - fs::file_time_type::clock::now() +
std::chrono::system_clock::now());
// Converts to seconds // Converts to seconds
std::time_t timeInSeconds = std::time_t timeInSeconds = system_clock::to_time_t(systemTimePoint);
std::chrono::system_clock::to_time_t(systemTimePoint);
// Converts to local time, built in function // Converts to local time, built in function
std::tm localTime = *std::localtime(&timeInSeconds); std::tm localTime = *std::localtime(&timeInSeconds);

View File

@ -1,24 +1,23 @@
/** /**
* @file watchdog.h * @file watchdog.h
* @brief file watcher * @brief file watcher
* @author Preston Shultz * @author Preston Shultz
* Sources: * Sources:
* https://en.cppreference.com/w/cpp/filesystem.html * https://en.cppreference.com/w/cpp/filesystem.html
* https://en.cppreference.com/w/cpp/chrono.html * https://en.cppreference.com/w/cpp/chrono.html
* Format the time into a string using strftime * Format the time into a string using strftime
* https://en.cppreference.com/w/cpp/chrono/c/strftime.html * https://en.cppreference.com/w/cpp/chrono/c/strftime.html
*/ */
#ifndef WATCHDOG_H #ifndef WATCHDOG_H
#define WATCHDOG_H #define WATCHDOG_H
#include <iostream> #include <chrono> //makes timestamps easier
#include <string> #include <ctime> //Convert time_t to std::tm for local time
#include <filesystem> //allow access to files platform independent #include <filesystem> //allow access to files platform independent
#include <chrono> //makes timestamps easier #include <iomanip> //Formats std::tim into "YYYY-MM-DD HH-MM-SS"
#include <ctime> //Convert time_t to std::tm for local time #include <iostream>
#include <iomanip> //Formats std::tim into "YYYY-MM-DD HH-MM-SS" #include <string>
/** /**
* @brief watchdog class. * @brief watchdog class.
@ -27,70 +26,79 @@
* *
* @author Preston Shultz (shultzp1@my.erau.edu) * @author Preston Shultz (shultzp1@my.erau.edu)
*/ */
class Watchdog{ class Watchdog {
protected:
/**
* @brief watchdog class.
*
* Checks if a file has been modified
*
* @author Preston Shultz (shultzp1@my.erau.edu)
* @author Hayden Hargreaves (hhargreaves2006@gmail.com)
*/
void CheckFile();
public: public:
Watchdog(const std::string& path) : Watchdog(const std::string &path) : path(path) {}
path(path), watching(false), has_initial_time(false) {}
/** /**
* @brief watchdog class. * @brief watchdog class.
* *
* Starts the watchdog to check of a file is modified * Starts the watchdog to check of a file is modified
* *
* @author Preston Shultz (shultzp1@my.erau.edu) * @author Preston Shultz (shultzp1@my.erau.edu)
*/ */
void Start(); void Start();
/**
* @brief watchdog class.
*
* Stops the watchdog
*
* @author Preston Shultz (shultzp1@my.erau.edu)
*/
void Stop() {watching = false;} //Disable
/** /**
* @brief watchdog class. * @brief watchdog class.
* *
* Checks if a file has been modified * Converts time point into a string
* *
* @author Preston Shultz (shultzp1@my.erau.edu) * Details below:
*/ *
bool CheckFile(); * Step 1: timePoint
* - This is the last write time of the file, returned by std::filesystem.
/** * - Its clock is platform-dependent (filesystem clock).
* @brief watchdog class. *
* * Step 2: fs::file_time_type::clock::now()
* Returns files last modified date * - Current time according to the filesystem clock.
* *
* @author Preston Shultz (shultzp1@my.erau.edu) * Step 3: std::chrono::system_clock::now()
*/ * - Current time according to the system clock (standard C++ clock).
std::filesystem::file_time_type GetLastWriteTime() *
const {return last_write_time;} * Conversion formula:
* timePoint - fs::file_time_type::clock::now() +
/** * std::chrono::system_clock::now()
* @brief watchdog class. *
* * Explanation:
* Converts time point into a string * a) timePoint - fs::file_time_type::clock::now()
* * - Calculates the duration between the file's last write time and "now"
* @author Preston Shultz (shultzp1@my.erau.edu) * according to the filesystem clock.
*/ * b) + std::chrono::system_clock::now()
static std::string TimePointToString(const std::filesystem::file_time_type& timePoint); * - Shifts that duration to align with the system clock timeline.
* c) std::chrono::time_point_cast<std::chrono::system_clock::duration>(...)
/** * - Ensures the resulting time_point uses the correct duration type
* @brief watchdog class. * for std::chrono::system_clock.
* *
* Returns if the watchDog is on * Result:
* * - systemTime is a std::chrono::system_clock::time_point representing
* @author Preston Shultz (shultzp1@my.erau.edu) * the same instant as timePoint, but compatible with system_clock.
*/ *
bool IsWatching() const{return watching;} * @author Preston Shultz (shultzp1@my.erau.edu)
*/
static std::string
TimePointToString(const std::filesystem::file_time_type &timePoint);
private: private:
std::string path; //file path std::string path;
bool watching; //Is watchdog on? std::filesystem::file_time_type last_write_time;
std::filesystem::file_time_type last_write_time;
bool has_initial_time; //checks if initial time is given // Timing values for the watchdog
const std::chrono::milliseconds POLLING_INTERVAL =
std::chrono::milliseconds(100);
const int MAX_RETRIES = 20;
const std::chrono::milliseconds RETRY_DELAY = std::chrono::milliseconds(20);
}; };
#endif #endif

View File

@ -41,18 +41,6 @@ void test_nodes() {
void test_watchdog() { void test_watchdog() {
Watchdog wd("test/input.md"); Watchdog wd("test/input.md");
wd.Start(); wd.Start();
std::cout << "Initial check (should do nothing if file unchanged):\n";
wd.CheckFile();
std::cout << "Now, modify or create the file 'example.txt' manually and "
"press Enter:\n";
std::cin.get(); // Wait for user to press Enter
// Check again after manual change
wd.CheckFile();
std::cout << "Done testing.\n";
} }
void test_input(int argc, char **argv) { void test_input(int argc, char **argv) {