-
Notifications
You must be signed in to change notification settings - Fork 0
/
cacheprocess_update_read.php
309 lines (237 loc) · 10.5 KB
/
cacheprocess_update_read.php
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
/*
* Cache Process Update
* Updates Queued Cache Requests
*/
namespace Fizzik;
require_once 'includes/include.php';
require_once 'includes/HotstatusResponse/include.php';
use Fizzik\Database\MySqlDatabase;
use Fizzik\Database\RedisDatabase;
use Fizzik\Utility\Console;
use Fizzik\Utility\SleepHandler;
set_time_limit(0);
date_default_timezone_set(HotstatusPipeline::REPLAY_TIMEZONE);
$db = new MysqlDatabase();
$creds = Credentials::getCredentialsForUser(Credentials::USER_REPLAYPROCESS);
HotstatusPipeline::hotstatus_mysql_connect($db, $creds);
$db->setEncoding(HotstatusPipeline::DATABASE_CHARSET);
//Constants and qol
const UNLOCK_UPDATE_DURATION = 900; //Seconds
const UNLOCK_DEFAULT_DURATION = 5; //Must be unlocked for atleast 5 seconds
const NORMAL_EXECUTION_SLEEP_DURATION = 1000; //microseconds (1ms = 1000)
const SLEEP_DURATION = 5; //seconds
const MINI_SLEEP_DURATION = 1; //seconds
const E = PHP_EOL;
$sleep = new SleepHandler();
$console = new Console();
//Prepare statements
$db->prepare("get_var",
"SELECT * FROM `pipeline_variables` WHERE `key_name` = ? LIMIT 1");
$db->bind("get_var", "s", $r_key_name);
$db->prepare("+=Squawk",
"INSERT INTO `pipeline_instances` "
. "(`id`, `type`, `state`, `lastused`) "
. "VALUES (?, ?, ?, ?) "
. "ON DUPLICATE KEY UPDATE "
. "`state` = ?, `lastused` = ?");
$db->bind("+=Squawk",
"ssiiii",
$r_instance_id, $r_instance_type, $r_instance_state, $r_instance_lastused,
$r_instance_state, $r_instance_lastused);
$db->prepare("TouchRequest",
"UPDATE `pipeline_cache_requests` SET `lastused` = ? WHERE `id` = ?");
$db->bind("TouchRequest", "ii", $r_timestamp, $r_id);
$db->prepare("UpdateRequestStatus",
"UPDATE `pipeline_cache_requests` SET lastused = ?, status = ? WHERE id = ? LIMIT 1");
$db->bind("UpdateRequestStatus", "iii", $r_timestamp, $r_status, $r_id);
$db->prepare("SelectNextRequestWithStatus-Unlocked",
"SELECT `id`, `action`, `cache_id`, `payload` FROM `pipeline_cache_requests` WHERE `lastused` <= ? AND `status` = ? ORDER BY `id` ASC LIMIT 1");
$db->bind("SelectNextRequestWithStatus-Unlocked", "ii", $r_timestamp, $r_status);
$db->prepare("SelectNextRequestWithStatusPriority-Unlocked",
"SELECT `id`, `action`, `cache_id`, `payload` FROM `pipeline_cache_requests` WHERE `lastused` <= ? AND `status` = ? ORDER BY `priority` DESC, `id` ASC LIMIT 1");
$db->bind("SelectNextRequestWithStatusPriority-Unlocked", "ii", $r_timestamp, $r_status);
$db->prepare("DeleteRequest",
"DELETE FROM `pipeline_cache_requests` WHERE `id` = ? LIMIT 1");
$db->bind("DeleteRequest", "i", $r_id);
$db->prepare("stats_cache_requests_updated_total",
"UPDATE `pipeline_analytics` SET `val_int` = `val_int` + ? WHERE `key_name` = 'cache_requests_updated_total' LIMIT 1");
$db->bind("stats_cache_requests_updated_total", "i", $r_cache_requests_updated_total);
$db->prepare("QueueCacheWrite", "INSERT INTO `pipeline_cache_writes` (`action`, `cache_id`, `payload`, `lastused`, `status`) VALUES (?, ?, ?, ?, ?)");
$db->bind("QueueCacheWrite", "sssii", $r_action, $r_cache_id, $r_payload, $r_lastused, $r_status);
//QOL Functions
function queueCacheWrite($functionId, $cache_id, $payload) {
global $db, $r_action, $r_cache_id, $r_payload, $r_lastused, $r_status;
$r_action = $functionId;
$r_cache_id = $cache_id;
$r_payload = $payload;
$r_lastused = time();
$r_status = HotstatusCache::QUEUE_CACHE_STATUS_QUEUED;
$db->execute("QueueCacheWrite");
}
/*
* Map actions to functions
*/
$actionMap = [
"getDataTableHeroesStatsListAction" => function($cache_id, $payload, MySqlDatabase &$db, $creds) {
$_TYPE = GetDataTableHeroesStatsListAction::_TYPE();
$_ID = GetDataTableHeroesStatsListAction::_ID();
$_VERSION = GetDataTableHeroesStatsListAction::_VERSION();
GetDataTableHeroesStatsListAction::generateFilters();
$CACHE_ID = $cache_id;
/*
* Begin building response
*/
//Set up main vars
$pagedata = [];
$datatable = [];
//Execute response
GetDataTableHeroesStatsListAction::execute($payload, $db, $pagedata, true);
$datatable['data'] = $pagedata;
//Store value in database to be written to cache by AWS later
$encoded = json_encode($datatable);
queueCacheWrite($_ID, $CACHE_ID, $encoded);
unset($encoded);
unset($datatable);
},
"getPageDataHeroAction" => function($cache_id, $payload, MySqlDatabase &$db, $creds) {
$_TYPE = GetPageDataHeroAction::_TYPE();
$_ID = GetPageDataHeroAction::_ID();
$_VERSION = GetPageDataHeroAction::_VERSION();
GetPageDataHeroAction::generateFilters();
$CACHE_ID = $cache_id;
/*
* Begin building response
*/
//Set up main vars
$pagedata = [];
$datatable = [];
//Execute response
GetPageDataHeroAction::execute($payload, $db, true, $pagedata, true);
$datatable['data'] = $pagedata;
//Store value in database to be written to cache by AWS later
$encoded = json_encode($datatable);
queueCacheWrite($_ID, $CACHE_ID, $encoded);
unset($encoded);
unset($datatable);
},
"getPageDataRankingsAction" => function($cache_id, $payload, MySqlDatabase &$db, $creds) {
$_TYPE = GetPageDataRankingsAction::_TYPE();
$_ID = GetPageDataRankingsAction::_ID();
$_VERSION = GetPageDataRankingsAction::_VERSION();
GetPageDataRankingsAction::generateFilters();
$CACHE_ID = $cache_id;
/*
* Begin building response
*/
//Set up main vars
$pagedata = [];
$datatable = [];
//Execute response
GetPageDataRankingsAction::execute($payload, $db, $pagedata, true);
$datatable['data'] = $pagedata;
//Store value in database to be written to cache by AWS later
$encoded = json_encode($datatable);
queueCacheWrite($_ID, $CACHE_ID, $encoded);
unset($encoded);
unset($datatable);
},
];
//Begin main script
echo '--------------------------------------'.E
.'Cache process <<UPDATE - READ>> has started'.E
.'--------------------------------------'.E;
//Generate Instance info
$r_instance_id = md5(time() . "fizzik" . mt_rand() . "kizzif" . rand() . uniqid("fizzik", true));
$r_instance_type = "Cache Update Read";
//Look for requests to update cache with
while (true) {
//Check shutoff state
$shutoff = 0;
$r_key_name = "instance_safe_shutoff";
$shutoffResult = $db->execute("get_var");
$shutoffResultRows = $db->countResultRows($shutoffResult);
if ($shutoffResultRows > 0) {
$shutoffRow = $db->fetchArray($shutoffResult);
$shutoff = intval($shutoffRow['val_int']);
}
if ($shutoff === 0) {
$r_instance_state = HotstatusPipeline::INSTANCE_STATE_PROCESSING;
$r_instance_lastused = time();
$db->execute("+=Squawk");
//Check for unlocked failed cache updating
$r_timestamp = time() - UNLOCK_UPDATE_DURATION;
$r_status = HotstatusCache::QUEUE_CACHE_STATUS_UPDATING;
$result = $db->execute("SelectNextRequestWithStatus-Unlocked");
$resrows = $db->countResultRows($result);
if ($resrows > 0) {
//Found a failed cache update process, reset it to queued
$row = $db->fetchArray($result);
echo 'Found a failed cache update at #' . $row['id'] . ', resetting status to \'' . HotstatusCache::QUEUE_CACHE_STATUS_QUEUED . '\'...' . E;
$r_id = $row['id'];
$r_timestamp = time();
$r_status = HotstatusCache::QUEUE_CACHE_STATUS_QUEUED;
$db->execute("UpdateRequestStatus");
}
else {
//No Cache Updating previously failed, look for unlocked queued request to update
$r_timestamp = time() - UNLOCK_DEFAULT_DURATION;
$r_status = HotstatusCache::QUEUE_CACHE_STATUS_QUEUED;
$queuedResult = $db->execute("SelectNextRequestWithStatusPriority-Unlocked");
$queuedResultRows = $db->countResultRows($queuedResult);
if ($queuedResultRows > 0) {
//Found a queued unlocked request for update, softlock for updating and update it
$row = $db->fetchArray($queuedResult);
$r_id = $row['id'];
$r_timestamp = time();
$r_status = HotstatusCache::QUEUE_CACHE_STATUS_UPDATING;
$db->execute("UpdateRequestStatus");
//Set lock id
$requestLockId = "hotstatus_updateCacheRequest_$r_id";
//Obtain lock
$requestLocked = $db->lock($requestLockId, 0);
if ($requestLocked) {
echo 'Update Cache Request #' . $r_id . '... ' . E;
$action = $row['action'];
$cache_id = $row['cache_id'];
$payload = json_decode($row['payload'], true);
//Execute request
$func = $actionMap[$action];
$func($cache_id, $payload, $db, $creds);
//Delete request after update
$db->execute("DeleteRequest");
//Inc updated total
$r_cache_requests_updated_total = 1;
$db->execute("stats_cache_requests_updated_total");
//Release lock
$db->unlock($requestLockId);
echo 'Cache Request #' . $r_id . ' Updated. ' . E . E;
}
else {
//Could not attain lock on request, immediately continue
}
}
else {
//No unlocked queued requests to update, sleep
$dots = $console->animateDotDotDot();
echo "No unlocked queued requests found$dots \r";
$sleep->add(SLEEP_DURATION);
}
$db->freeResult($queuedResult);
}
$db->freeResult($result);
}
else {
$r_instance_state = HotstatusPipeline::INSTANCE_STATE_SAFESHUTOFF;
$r_instance_lastused = time();
$db->execute("+=Squawk");
//Safe shutoff has been flagged
$dots = $console->animateDotDotDot();
echo "Safe Shutdown$dots \r";
$sleep->add(SLEEP_DURATION);
}
//Default sleep
$sleep->add(NORMAL_EXECUTION_SLEEP_DURATION, true, true);
$sleep->execute();
}
?>