-
Notifications
You must be signed in to change notification settings - Fork 56
/
StaticPagesQueue.php
286 lines (250 loc) · 7.95 KB
/
StaticPagesQueue.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
<?php
/**
* This class responsibility is twofold:
* 1) Holding the data for a prioritized queue of URLs that needs to be static cached
* 2) Interaction with that queue
*
* @TODO: would be good to refactor this queue to hold not only URLSegment, but also ClassName and ID of the
* associated object (or any other metadata). This would allow FilesystemPublisher::publishPages and others
* to stop having to smuggle the metadata within the URL (see URLArrayData::get_object).
*
*/
class StaticPagesQueue extends DataObject {
/**
*
* @var array
*/
public static $create_table_options = array(
'MySQLDatabase' => 'ENGINE=InnoDB'
);
/**
*
* @var array
*/
public static $db = array(
'Priority' => 'Int',
'URLSegment' => 'Varchar(255)',
'Freshness' => "Enum('stale, regenerating, error', 'stale')"
);
/**
*
* @var array
*/
public static $defaults = array(
"Priority" => 3
);
/**
*
* @var array
*/
public static $default_sort = "\"Priority\"";
/**
* Sets database indexes
*
* @var array
*/
public static $indexes = array(
'freshness_priority_created' => '(Freshness, Priority, Created)',
);
/**
*
* @var boolean
*/
private static $disable_mysql_locks = false;
/**
*
* @var boolean
*/
private static $realtime = false;
/**
*
* @var int
*/
protected static $minutes_until_force_regeneration = 1;
/**
*
* @var array
*/
protected static $insert_statements = array();
/**
*
* @var array
*/
protected static $urls = array();
/**
*
* @return bool
*/
public static function is_realtime() {
return Config::inst()->get('StaticPagesQueue','realtime');
}
/**
*
* @param type $priority
* @param type $URLSegment
* @return type
*/
public static function add_to_queue($priority, $URLSegment) {
$now = date("Y-m-d H:i:s");
self::$insert_statements[$URLSegment] = '(\''.$now.'\',\''.$now.'\', \''.Convert::raw2sql($priority).'\',\''.Convert::raw2sql($URLSegment).'\')';
self::$urls[md5($URLSegment)] = $URLSegment;
}
/**
* This will push all the currently cached insert statements to be pushed
* into the database
*
* @return void
*/
public static function push_urls_to_db() {
foreach(self::$insert_statements as $stmt) {
$insertSQL = 'INSERT INTO "StaticPagesQueue" ("Created", "LastEdited", "Priority", "URLSegment") VALUES ' . $stmt;
DB::query($insertSQL);
}
self::remove_old_cache(self::$urls);
// Flush the cache so DataObject::get works correctly
if(!empty(self::$insert_statements) && DB::affectedRows()) {
singleton(__CLASS__)->flushCache();
}
self::$insert_statements = array();
}
/**
* Remove an object by the url
*
* @param string $URLSegment
* @return bool - if there was an queue item removed
*
*/
public static function delete_by_link($URLSegment) {
$object = self::get_by_link($URLSegment);
if(!$object) return false;
$object->delete();
unset($object);
return true;
}
/**
* Update the queue with the information that this url renders an error somehow
*
* @param string $url
*/
public static function has_error( $url ) {
if(!$url) return;
$existingObject = self::get_by_link($url);
if(!$existingObject) return;
$existingObject->Freshness = 'error';
$existingObject->write();
}
/**
* Returns a single queue object according to a particular priority and freshness measure.
* This method removes any duplicates and makes the object as "regenerating", so other calls to this method
* don't grab the same object.
* If we are using MySQLDatabase with InnoDB, we do row-level locking when updating the dataobject to allow for
* distributed cache rebuilds
* @static
* @param $freshness
* @param $sortOrder
*/
protected static function get_queue_object($freshness, $interval = null, $sortOrder = array('Priority'=>'DESC', 'ID'=>'ASC')) {
$className = __CLASS__;
$queueObject = null;
$filterQuery = array("Freshness" => $freshness);
if ($interval) $filterQuery["LastEdited:LessThan"] = $interval;
$offset = 0;
$filteredQuery = self::get()->filter($filterQuery)->sort($sortOrder);
$queueObject = $filteredQuery->first();
if ($queueObject) {
if (!self::config()->disable_mysql_locks && DB::getConn() instanceof MySQLDatabase) { //locking currently only works on MySQL
do {
$queueObject = $filteredQuery->limit(1, $offset)->first(); //get first item
if ($queueObject) $lockName = md5($queueObject->URLSegment . $className);
//try to locking the item's URL, keep trying new URLs until we find one that is free to lock
$offset++;
} while($queueObject && !LockMySQL::isFreeToLock($lockName));
if ($queueObject) {
$lockSuccess = LockMySQL::getLock($lockName); //acquire a lock with the URL of the queue item we have just fetched
if ($lockSuccess) {
self::remove_duplicates($queueObject->ID); //remove any duplicates
self::mark_as_regenerating($queueObject); //mark as regenerating so nothing else grabs it
LockMySQL::releaseLock($lockName); //return the object and release the lock
}
}
} else {
self::remove_duplicates($queueObject->ID);
self::mark_as_regenerating($queueObject);
}
}
return $queueObject; //return the object or null
}
/**
* Finds the next most prioritized url that needs recaching
*
* @return string
*/
public static function get_next_url() {
$object = self::get_queue_object('stale');
if($object) return $object->URLSegment;
$interval = date('Y-m-d H:i:s', strtotime('-'.self::$minutes_until_force_regeneration.' minutes'));
// Find URLs that has been stuck in regeneration
$object = self::get_queue_object('regenerating', $interval);
if($object) return $object->URLSegment;
// Find URLs that is erronous and might work now (flush issues etc)
$object = self::get_queue_object('error', $interval);
if($object) return $object->URLSegment;
return '';
}
/**
* Removes the .html fresh copy of the cache.
* Keeps the *.stale.html copy in place,
* in order to notify the user of the stale content.
*
* @param array $URLSegments
*/
protected static function remove_old_cache( array $URLSegments ) {
$publisher = singleton('SiteTree')->getExtensionInstance('FilesystemPublisher');
if ($publisher) {
$paths = $publisher->urlsToPaths($URLSegments);
foreach($paths as $absolutePath) {
if(!file_exists($publisher->getDestDir().'/'.$absolutePath)) {
continue;
}
unlink($publisher->getDestDir().'/'.$absolutePath);
}
}
}
/**
* Mark this current StaticPagesQueue as a work in progress
*
* @param StaticPagesQueue $object
*/
protected static function mark_as_regenerating(StaticPagesQueue $object) {
$now = date('Y-m-d H:i:s');
DB::query('UPDATE "StaticPagesQueue" SET "LastEdited" = \''.$now.'\', "Freshness"=\'regenerating\' WHERE "ID" = '.$object->ID);
singleton(__CLASS__)->flushCache();
}
/**
* Removes all duplicates that has the same URLSegment as $ID
*
* @param int $ID - ID of the object whose duplicates we want to remove
* @return void
*/
static function remove_duplicates( $ID ) {
$obj = DataObject::get_by_id('StaticPagesQueue', $ID);
if(!$obj) return 0;
DB::query(
sprintf('DELETE FROM "StaticPagesQueue" WHERE "URLSegment" = \'%s\' AND "ID" != %d', $obj->URLSegment, (int)$ID)
);
}
/**
*
* @param string $url
* @param bool $onlyStale - Get only stale entries
* @return DataObject || false - The first item matching the query
*/
protected static function get_by_link($url) {
$filter = '"URLSegment" = \''.Convert::raw2sql($url).'\'';
$res = DB::query('SELECT * FROM "StaticPagesQueue" WHERE '.$filter.' LIMIT 1;');
if(!$res->numRecords()){
return false;
}
return new StaticPagesQueue($res->first());
}
}