forked from Expensify/Bedrock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBedrockCore.cpp
More file actions
175 lines (154 loc) · 6.61 KB
/
Copy pathBedrockCore.cpp
File metadata and controls
175 lines (154 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "BedrockCore.h"
#include "BedrockPlugin.h"
#include "BedrockServer.h"
BedrockCore::BedrockCore(SQLite& db, const BedrockServer& server) :
SQLiteCore(db),
_server(server)
{ }
bool BedrockCore::peekCommand(BedrockCommand& command) {
AutoTimer timer(command, BedrockCommand::PEEK);
// Convenience references to commonly used properties.
SData& request = command.request;
SData& response = command.response;
STable& content = command.jsonContent;
SDEBUG("Peeking at '" << request.methodLine << "'");
command.peekCount++;
// We catch any exception and handle in `_handleCommandException`.
try {
// Try each plugin, and go with the first one that says it succeeded.
bool pluginPeeked = false;
for (auto plugin : _server.plugins) {
// Try to peek the command.
if (plugin->peekCommand(_db, command)) {
SINFO("Plugin '" << plugin->getName() << "' peeked command '" << request.methodLine << "'");
pluginPeeked = true;
break;
}
}
// If nobody succeeded in peeking it, then we'll need to process it.
// TODO: Would be nice to be able to check if a plugin *can* handle a command, so that we can differentiate
// between "didn't peek" and "peeked but didn't complete".
if (!pluginPeeked) {
SINFO("Command '" << request.methodLine << "' is not peekable, queuing for processing.");
return false;
}
// If no response was set, assume 200 OK
if (response.methodLine == "") {
response.methodLine = "200 OK";
}
// Add the commitCount header to the response.
response["commitCount"] = to_string(_db.getCommitCount());
// Success. If a command has set "content", encode it in the response.
SINFO("Responding '" << response.methodLine << "' to read-only '" << request.methodLine << "'.");
if (!content.empty()) {
// Make sure we're not overwriting anything different.
string newContent = SComposeJSONObject(content);
if (response.content != newContent) {
if (!response.content.empty()) {
SWARN("Replacing existing response content in " << request.methodLine);
}
response.content = newContent;
}
}
} catch (const char* e) {
_handleCommandException(command, e, false);
} catch (const string e) {
_handleCommandException(command, e, false);
} catch (...) {
_handleCommandException(command, "", false);
}
// If we get here, it means the command is fully completed.
command.complete = true;
return true;
}
bool BedrockCore::processCommand(BedrockCommand& command) {
AutoTimer timer(command, BedrockCommand::PROCESS);
// Convenience references to commonly used properties.
SData& request = command.request;
SData& response = command.response;
STable& content = command.jsonContent;
SDEBUG("Processing '" << request.methodLine << "'");
command.processCount++;
// Keep track of whether we've modified the database and need to perform a `commit`.
bool needsCommit = false;
try {
if (!_db.beginConcurrentTransaction()) {
throw "501 Failed to begin concurrent transaction";
}
// Loop across the plugins to see which wants to take this.
bool pluginProcessed = false;
for (auto plugin : _server.plugins) {
// Try to process the command.
if (plugin->processCommand(_db, command)) {
SINFO("Plugin '" << plugin->getName() << "' processed command '" << request.methodLine << "'");
pluginProcessed = true;
break;
}
}
// If no plugin processed it, respond accordingly.
if (!pluginProcessed) {
SWARN("Command '" << request.methodLine << "' does not exist.");
throw "430 Unrecognized command";
}
// If we have no uncommitted query, just rollback the empty transaction. Otherwise, we need to commit.
if (_db.getUncommittedQuery().empty()) {
_db.rollback();
} else {
needsCommit = true;
}
// If no response was set, assume 200 OK
if (response.methodLine == "") {
response.methodLine = "200 OK";
}
// Add the commitCount header to the response.
response["commitCount"] = to_string(_db.getCommitCount());
// Success, this command will be committed.
SINFO("Processed '" << response.methodLine << "' for '" << request.methodLine << "'.");
// Finally, if a command has set "content", encode it in the response.
if (!content.empty()) {
// Make sure we're not overwriting anything different.
string newContent = SComposeJSONObject(content);
if (response.content != newContent) {
if (!response.content.empty()) {
SWARN("Replacing existing response content in " << request.methodLine);
}
response.content = newContent;
}
}
} catch (const char* e) {
_handleCommandException(command, e, true);
} catch (const string e) {
_handleCommandException(command, e, true);
} catch (...) {
_handleCommandException(command, "", true);
}
// Done, return whether or not we need the parent to commit our transaction.
command.complete = !needsCommit;
return needsCommit;
}
void BedrockCore::_handleCommandException(BedrockCommand& command, const string& e, bool wasProcessing) {
// If we were peeking, then we weren't in a transaction. But if we were processing, we need to roll it back.
if (wasProcessing) {
_db.rollback();
}
const string& msg = "Error processing command '" + command.request.methodLine + "' (" + e + "), ignoring: " +
command.request.serialize();
if (SContains(e, "_ALERT_")) {
SALERT(msg);
} else if (SContains(e, "_WARN_")) {
SWARN(msg);
} else if (SContains(e, "_HMMM_")) {
SHMMM(msg);
} else if (SStartsWith(e, "50")) {
SALERT(msg); // Alert on 500 level errors.
} else {
SINFO(msg);
}
// If the command set a response before throwing an exception, we'll keep that as our response to use. Otherwise,
// we'll use the text of the error.
if (command.response.methodLine.empty()) {
command.response.methodLine = e;
}
// Add the commitCount header to the response.
command.response["commitCount"] = to_string(_db.getCommitCount());
}