Skip to content

Commit 7393232

Browse files
committed
Don't move an error report unless it's actually sent.
1 parent 54b3004 commit 7393232

File tree

4 files changed

+40
-22
lines changed

4 files changed

+40
-22
lines changed

SerialPrograms/Source/CommonFramework/ErrorReports/ErrorReports.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,6 @@ void SendableErrorReport::save(Logger* logger) const{
279279
report.dump(m_directory + "Report.json");
280280
}
281281

282-
#ifndef PA_OFFICIAL
283-
bool SendableErrorReport::send(Logger& logger){
284-
return false;
285-
}
286-
#endif
287-
288282
void SendableErrorReport::move_to_sent(){
289283
// cout << "move_to_sent()" << endl;
290284
QDir().mkdir(QString::fromStdString(ERROR_PATH_SENT));
@@ -320,15 +314,22 @@ std::vector<std::string> SendableErrorReport::get_pending_reports(){
320314
return ret;
321315
}
322316

317+
#ifndef PA_OFFICIAL
318+
void SendableErrorReport::send(Logger& logger, std::shared_ptr<SendableErrorReport> report){}
319+
#endif
320+
323321

324322

323+
// Send all the reports. This function will return early and all the reports
324+
// will be sent asynchronously in the background.
325325
void send_reports(Logger& logger, const std::vector<std::string>& reports){
326326
for (const std::string& path : reports){
327327
try{
328328
// static int c = 0;
329329
// cout << "Sending... " << c++ << endl;
330-
SendableErrorReport report(path);
331-
report.send(logger);
330+
// std:::shared_ptr because it needs to take ownership for
331+
// destruction at a later time.
332+
SendableErrorReport::send(logger, std::make_shared<SendableErrorReport>(path));
332333
}catch (Exception& e){
333334
logger.log("Unable to send report: " + path + ", Message: " + e.to_str(), COLOR_RED);
334335
}catch (...){

SerialPrograms/Source/CommonFramework/ErrorReports/ErrorReports.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ class SendableErrorReport{
7878
void add_file(std::string filename);
7979

8080
void save(Logger* logger) const;
81-
bool send(Logger& logger);
8281
void move_to_sent();
8382

8483
static std::vector<std::string> get_pending_reports();
84+
static void send(Logger& logger, std::shared_ptr<SendableErrorReport> report);
8585

8686
private:
8787
std::string m_timestamp;

SerialPrograms/Source/Integrations/DiscordWebhook.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,21 @@ void DiscordWebhookSender::send(
6060
Logger& logger,
6161
const QUrl& url, std::chrono::milliseconds delay,
6262
const JsonObject& obj,
63-
std::shared_ptr<PendingFileSend> file
63+
std::shared_ptr<PendingFileSend> file,
64+
std::function<void()> finish_callback
6465
){
6566
cleanup_stuck_requests();
6667
std::shared_ptr<JsonValue> json(new JsonValue(obj.clone()));
68+
// cout << "Scheduling Webhook Message... (queue = " + tostr_u_commas(m_queue.size()) + ")" << endl;
69+
logger.log("Scheduling Webhook Message... (queue = " + tostr_u_commas(m_queue.size()) + ")", COLOR_PURPLE);
6770
m_queue.add_event(
6871
delay,
69-
[this, url, json = std::move(json), file = std::move(file)]{
72+
[
73+
this, url,
74+
json = std::move(json),
75+
file = std::move(file),
76+
finish_callback = std::move(finish_callback)
77+
]{
7078
throttle();
7179
std::vector<DiscordFileAttachment> attachments;
7280
if (file){
@@ -75,22 +83,31 @@ void DiscordWebhookSender::send(
7583
);
7684
}
7785
internal_send(url, *json, attachments);
86+
if (finish_callback){
87+
finish_callback();
88+
}
7889
}
7990
);
80-
// cout << "Scheduling Webhook Message... (queue = " + tostr_u_commas(m_queue.size()) + ")" << endl;
81-
logger.log("Scheduling Webhook Message... (queue = " + tostr_u_commas(m_queue.size()) + ")", COLOR_PURPLE);
8291
}
8392
void DiscordWebhookSender::send(
8493
Logger& logger,
8594
const QUrl& url, std::chrono::milliseconds delay,
8695
const JsonObject& obj,
87-
std::vector<std::shared_ptr<PendingFileSend>> files
96+
std::vector<std::shared_ptr<PendingFileSend>> files,
97+
std::function<void()> finish_callback
8898
){
8999
cleanup_stuck_requests();
90100
std::shared_ptr<JsonValue> json(new JsonValue(obj.clone()));
101+
// cout << "Scheduling Webhook Message... (queue = " + tostr_u_commas(m_queue.size()) + ")" << endl;
102+
logger.log("Scheduling Webhook Message... (queue = " + tostr_u_commas(m_queue.size()) + ")", COLOR_PURPLE);
91103
m_queue.add_event(
92104
delay,
93-
[this, url, json = std::move(json), files = std::move(files)]{
105+
[
106+
this, url,
107+
json = std::move(json),
108+
files = std::move(files),
109+
finish_callback = std::move(finish_callback)
110+
]{
94111
throttle();
95112
std::vector<DiscordFileAttachment> attachments;
96113
for (auto& file : files){
@@ -99,10 +116,11 @@ void DiscordWebhookSender::send(
99116
);
100117
}
101118
internal_send(url, *json, attachments);
119+
if (finish_callback){
120+
finish_callback();
121+
}
102122
}
103123
);
104-
// cout << "Scheduling Webhook Message... (queue = " + tostr_u_commas(m_queue.size()) + ")" << endl;
105-
logger.log("Scheduling Webhook Message... (queue = " + tostr_u_commas(m_queue.size()) + ")", COLOR_PURPLE);
106124
}
107125

108126
void DiscordWebhookSender::cleanup_stuck_requests(){

SerialPrograms/Source/Integrations/DiscordWebhook.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ class DiscordWebhookSender : public QObject{
4747
Logger& logger,
4848
const QUrl& url, std::chrono::milliseconds delay,
4949
const JsonObject& obj,
50-
std::shared_ptr<PendingFileSend> file
50+
std::shared_ptr<PendingFileSend> file,
51+
std::function<void()> finish_callback = nullptr
5152
);
5253
void send(
5354
Logger& logger,
5455
const QUrl& url, std::chrono::milliseconds delay,
5556
const JsonObject& obj,
56-
std::vector<std::shared_ptr<PendingFileSend>> files
57+
std::vector<std::shared_ptr<PendingFileSend>> files,
58+
std::function<void()> finish_callback = nullptr
5759
);
5860

5961
static DiscordWebhookSender& instance();
@@ -70,9 +72,6 @@ class DiscordWebhookSender : public QObject{
7072
const std::vector<DiscordFileAttachment>& files
7173
);
7274

73-
signals:
74-
void stop_event_loop();
75-
7675
private:
7776
TaggedLogger m_logger;
7877
std::atomic<bool> m_stopping;

0 commit comments

Comments
 (0)