33 #ifndef MODULE_THREAD_HPP_ 34 #define MODULE_THREAD_HPP_ 41 #include "../Helper/DateTime.hpp" 42 #include "../Main/Database.hpp" 43 #include "../Main/Exception.hpp" 44 #include "../Struct/ModuleOptions.hpp" 45 #include "../Struct/ThreadOptions.hpp" 46 #include "../Struct/ThreadStatus.hpp" 47 #include "../Wrapper/DatabaseLock.hpp" 52 #include <condition_variable> 59 #include <string_view> 69 using std::string_view_literals::operator
""sv;
125 std::uint64_t
getId()
const;
150 void warpTo(std::uint64_t target);
215 void setLast(std::uint64_t lastId);
223 void sleep(std::uint64_t ms)
const;
233 void log(std::uint8_t level,
const std::string& logEntry);
234 void log(std::uint8_t level, std::queue<std::string>& logEntries);
245 virtual void onInit() = 0;
252 virtual void onTick() = 0;
287 std::atomic<bool> pausable{
true};
288 std::atomic<bool> running{
true};
289 std::atomic<bool> paused{
false};
290 std::atomic<bool> toReset{
false};
291 std::atomic<bool> interrupted{
false};
292 std::atomic<bool> terminated{
false};
293 std::atomic<bool> shutdown{
false};
294 std::atomic<bool> finished{
false};
300 std::uint64_t last{};
301 std::atomic<std::uint64_t> overwriteLast{};
302 std::int64_t warpedOver{};
303 std::uint64_t processed{};
305 std::condition_variable pauseCondition;
306 mutable std::mutex pauseLock;
309 mutable std::mutex statusLock;
312 mutable std::mutex progressLock;
317 std::chrono::steady_clock::time_point startTimePoint{std::chrono::steady_clock::time_point::min()};
318 std::chrono::steady_clock::time_point pauseTimePoint{std::chrono::steady_clock::time_point::min()};
319 std::chrono::duration<std::uint64_t> runTime{std::chrono::duration<std::uint64_t>::zero()};
320 std::chrono::duration<std::uint64_t> pauseTime{std::chrono::duration<std::uint64_t>::zero()};
323 std::uint64_t getRunTime()
const;
324 void updateRunTime();
325 void updatePauseTime();
334 bool isUnpaused()
const;
338 void clearException(
const std::exception& e,
const std::string& inFunction);
339 void clearException(
const std::string& inFunction);
bool isInterrupted() const
Checks whether the thread has been interrupted.
Definition: Thread.cpp:458
bool pause()
Pauses the thread.
Definition: Thread.cpp:279
void stop()
Shuts down the thread.
Definition: Thread.cpp:326
virtual void onInit()=0
Initializes the module.
constexpr auto sleepMs
Number of milliseconds to sleep before checking whether the thread is still running.
Definition: Thread.hpp:78
std::string getStatusMessage() const
Gets the current status message.
Definition: Thread.cpp:470
virtual ~Thread()=default
Default destructor.
float getProgress() const
Gets the current progress, in percent.
Definition: Thread.cpp:485
std::uint64_t getUrlList() const
Gets the ID of the URL list used by the thread.
Definition: Thread.cpp:177
Thread status containing its ID, status message, pause state, and progress.
Definition: ThreadStatus.hpp:54
void allowPausing()
Allows the thread to be paused.
Definition: Thread.cpp:693
Class handling database access for threads.
Definition: Database.hpp:91
bool isFinished() const
Checks whether the shutdown of the thread has been finished.
Definition: Thread.cpp:233
std::uint64_t getLast() const
Gets the value of the last ID processed by the thread.
Definition: Thread.cpp:499
bool isRunning() const
Checks whether the thread is still supposed to run.
Definition: Thread.cpp:221
#define MAIN_EXCEPTION_CLASS()
Macro used to easily define classes for general exceptions.
Definition: Exception.hpp:50
virtual void onReset()=0
Resets the module.
Thread options containing the name of the module run, as well as the IDs of the website, URL list, and configuration used.
Definition: ThreadOptions.hpp:40
Abstract class providing module-independent thread functionality.
Definition: Thread.hpp:93
constexpr auto sleepOnConnectionErrorS
Number of seconds to sleep on connection errors.
Definition: Thread.hpp:75
bool isLogLevel(std::uint8_t level) const
Checks whether a certain logging level is enabled.
Definition: Thread.cpp:741
void unpause()
Unpauses the thread.
Definition: Thread.cpp:302
Class handling database access for the command-and-control and its threads.
Definition: Database.hpp:366
virtual void onClear()=0
Clears the module.
Module options containing the thread ID, as well as ID and namespace of website and URL list used by ...
Definition: ModuleOptions.hpp:40
std::string urlListNamespace
Namespace of the URL list used by the thread.
Definition: Thread.hpp:191
bool isPaused() const
Checks whether the thread has been paused.
Definition: Thread.cpp:245
void incrementProcessed()
Increments the number of IDs processed by the thread.
Definition: Thread.cpp:648
std::uint64_t getConfig() const
Gets the ID of the configuration used by the thread.
Definition: Thread.cpp:192
std::uint64_t getWebsite() const
Gets the ID of the website used by the thread.
Definition: Thread.cpp:163
void pauseByThread()
Forces the thread to pause.
Definition: Thread.cpp:712
void interrupt()
Interrupts the thread due to an exception.
Definition: Thread.cpp:359
virtual void onUnpause()=0
Unpauses the module.
void log(std::uint8_t level, const std::string &logEntry)
Adds a thread-specific log entry to the database, if the current logging level is high enough...
Definition: Thread.cpp:779
std::string websiteNamespace
Namespace of the website used by the thread.
Definition: Thread.hpp:188
void sleep(std::uint64_t ms) const
Lets the thread sleep for the specified number of milliseconds.
Definition: Thread.cpp:667
Thread & operator=(Thread &)=delete
Deleted copy assignment operator.
void setStatusMessage(const std::string &statusMessage)
Sets the status message of the thread.
Definition: Thread.cpp:544
std::int64_t getWarpedOverAndReset()
Gets the number of IDs that have been jumped over, and resets them.
Definition: Thread.cpp:517
void warpTo(std::uint64_t target)
Jumps to the specified target ID ("time travel").
Definition: Thread.cpp:433
void incrementLast()
Increments the last ID processed by the thread.
Definition: Thread.cpp:633
void reset()
Will reset the thread before the next tick.
Definition: Thread.cpp:409
void start()
Starts running the thread.
Definition: Thread.cpp:257
Thread(Main::Database &dbBase, const ThreadOptions &threadOptions, const ThreadStatus &threadStatus)
Constructor initializing a previously interrupted thread.
Definition: Thread.cpp:52
Class for database connection exceptions.
Definition: Database.hpp:749
void setLast(std::uint64_t lastId)
Sets the last ID processed by the thread.
Definition: Thread.cpp:611
bool isShutdown() const
Checks whether the thread is shutting down or has shut down.
Definition: Thread.cpp:207
constexpr auto statusPrefixInterrupted
Status message prefix for interrupted threads.
Definition: Thread.hpp:81
constexpr auto statusPrefixPaused
Status message prefix for paused threads.
Definition: Thread.hpp:84
std::uint64_t getId() const
Gets the ID of the thread.
Definition: Thread.cpp:149
virtual void onPause()=0
Pauses the module.
void disallowPausing()
Disallows the thread to be paused.
Definition: Thread.cpp:703
void setProgress(float newProgress)
Sets the progress of the thread.
Definition: Thread.cpp:580
virtual void onTick()=0
Performs a module tick.
Namespace for the different modules run by threads.
Template class for safe in-scope database locks.
Definition: DatabaseLock.hpp:54
void end()
Waits for the thread until shutdown is completed.
Definition: Thread.cpp:390
std::string configuration
JSON string of the configuration used by the thread.
Definition: Thread.hpp:197
Database database
Database connection for the thread.
Definition: Thread.hpp:181