-
-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathFileStorage.php
More file actions
338 lines (264 loc) · 10.1 KB
/
Copy pathFileStorage.php
File metadata and controls
338 lines (264 loc) · 10.1 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php namespace Clockwork\Storage;
use Clockwork\Request\Request;
use Clockwork\Storage\Storage;
// File based storage for requests
class FileStorage extends Storage
{
// Path where files are stored
protected $path;
// Path permissions
protected $pathPermissions;
// Metadata expiration time in minutes
protected $expiration;
// Compress the files using gzip
protected $compress;
// Metadata cleanup chance
protected $cleanupChance = 100;
// Index file handle
protected $indexHandle;
// Return new storage, takes path where to store files as argument
public function __construct($path, $pathPermissions = null, $expiration = null, $compress = false)
{
$this->path = $path;
$this->pathPermissions = ($pathPermissions === null) ? 0700 : $pathPermissions;
$this->expiration = $expiration === null ? 60 * 24 * 7 : $expiration;
$this->compress = $compress;
}
// Returns all requests
public function all(?Search $search = null)
{
return $this->loadRequests($this->searchIndexForward($search));
}
// Return a single request by id
public function find($id)
{
return $this->loadRequest($id);
}
// Return the latest request
public function latest(?Search $search = null)
{
$requests = $this->loadRequests($this->searchIndexBackward($search, null, 1));
return reset($requests);
}
// Return requests received before specified id, optionally limited to specified count
public function previous($id, $count = null, ?Search $search = null)
{
return $this->loadRequests($this->searchIndexBackward($search, $id, $count));
}
// Return requests received after specified id, optionally limited to specified count
public function next($id, $count = null, ?Search $search = null)
{
return $this->loadRequests($this->searchIndexForward($search, $id, $count));
}
// Store request, requests are stored in JSON representation in files named <request id>.json in storage path,
// throws exception if path is not writable
public function store(Request $request, $skipIndex = false)
{
$this->ensurePathIsWritable();
$path = "{$this->path}/{$request->id}.json";
$data = @json_encode($request->toArray(), \JSON_PARTIAL_OUTPUT_ON_ERROR);
$this->compress
? file_put_contents("{$path}.gz", gzcompress($data))
: file_put_contents($path, $data . PHP_EOL);
if (! $skipIndex) $this->updateIndex($request);
$this->cleanup();
}
// Update existing request
public function update(Request $request)
{
return $this->store($request, true);
}
// Cleanup old requests
public function cleanup($force = false)
{
if ($this->expiration === false || (! $force && rand(1, $this->cleanupChance) != 1)) return;
$this->openIndex('start', true, true); // reopen index with lock
$expirationTime = time() - ($this->expiration * 60);
$old = $this->searchIndexForward(
new Search([ 'received' => [ '<' . date('c', $expirationTime) ] ], [ 'stopOnFirstMismatch' => true ])
);
if (! count($old)) return $this->closeIndex(true);
$this->readPreviousIndex();
$this->trimIndex();
$this->closeIndex(true); // explicitly close index to unlock asap
foreach ($old as $id) {
$path = "{$this->path}/{$id}.json";
@unlink($this->compress ? "{$path}.gz" : $path);
}
}
// Load a single request by id from filesystem
protected function loadRequest($id)
{
$path = "{$this->path}/{$id}.json";
if (! is_readable($this->compress ? "{$path}.gz" : $path)) return;
$data = file_get_contents($this->compress ? "{$path}.gz" : $path);
return new Request(json_decode($this->compress ? gzuncompress($data) : $data, true));
}
// Load multiple requests by ids from filesystem
protected function loadRequests($ids)
{
return array_filter(array_map(function ($id) { return $this->loadRequest($id); }, $ids));
}
// Search index backward from specified ID or last record, with optional results count limit
protected function searchIndexBackward(?Search $search = null, $id = null, $count = null)
{
return $this->searchIndex('previous', $search, $id, $count);
}
// Search index forward from specified ID or last record, with optional results count limit
protected function searchIndexForward(?Search $search = null, $id = null, $count = null)
{
return $this->searchIndex('next', $search, $id, $count);
}
// Search index in specified direction from specified ID or last record, with optional results count limit
protected function searchIndex($direction, ?Search $search = null, $id = null, $count = null)
{
$this->openIndex($direction == 'previous' ? 'end' : 'start', false, true);
if ($id) {
while ($request = $this->readIndex($direction)) { if ($request->id == $id) break; }
}
$found = [];
while ($request = $this->readIndex($direction)) {
if (! $search || $search->matches($request)) {
$found[] = $request->id;
} elseif ($search->stopOnFirstMismatch) {
break;
}
if ($count && count($found) == $count) break;
}
return $direction == 'next' ? $found : array_reverse($found);
}
// Open index file, optionally lock or move file pointer to the end, existing handle will be returned by default
protected function openIndex($position = 'start', $lock = false, $force = false)
{
if ($this->indexHandle) {
if (! $force) return;
$this->closeIndex();
}
$this->indexHandle = fopen("{$this->path}/index", 'r');
if ($lock) flock($this->indexHandle, LOCK_EX);
if ($position == 'end') fseek($this->indexHandle, 0, SEEK_END);
}
// Close index file, optionally unlock
protected function closeIndex($lock = false)
{
if ($lock) flock($this->indexHandle, LOCK_UN);
fclose($this->indexHandle);
$this->indexHandle = null;
}
// Read a line from index in the specified direction (next or previous)
protected function readIndex($direction)
{
return $direction == 'next' ? $this->readNextIndex() : $this->readPreviousIndex();
}
// Read previous line from index
protected function readPreviousIndex()
{
$position = ftell($this->indexHandle) - 1;
if ($position <= 0) return;
$line = '';
// File pointer is always kept at the start of the "next" record, scroll back to the end of "previous" record
fseek($this->indexHandle, $position - 1);
// reads 1024B chunks of the file backwards from the current position, until a newline is found or we reach the top
while ($position > 0) {
// find next position to read from, make sure we don't read beyond file boundary
$position -= $chunkSize = min($position, 1024);
// read the chunk from the position
fseek($this->indexHandle, $position);
$chunk = fread($this->indexHandle, $chunkSize);
// if a newline is found, append only the part after the last newline, otherwise we can append the whole chunk
$line = ($newline = strrpos($chunk, "\n")) === false
? $chunk . $line : substr($chunk, $newline + 1) . $line;
// if a newline was found, fix the position so we read from that newline next time
if ($newline !== false) $position += $newline + 1;
// move file pointer to the correct position (revert fread, apply newline fix)
fseek($this->indexHandle, $position);
// if we reached a newline and put together a non-empty line we are done
if ($newline !== false) break;
}
return $this->makeRequestFromIndex(str_getcsv($line, ',', '"', ''));
}
// Read next line from index
protected function readNextIndex()
{
if (feof($this->indexHandle)) return;
// File pointer is always at the start of the "next" record
$line = fgets($this->indexHandle);
// Check if we read an empty line or reached the end of file
if ($line === false) return;
return $this->makeRequestFromIndex(str_getcsv($line, ',', '"', ''));
}
// Trim index file from beginning to current position (including)
protected function trimIndex()
{
// Read the rest of the index file
$trimmedLength = filesize("{$this->path}/index") - ftell($this->indexHandle);
$trimmed = $trimmedLength > 0 ? fread($this->indexHandle, $trimmedLength) : '';
// Rewrite the index file with a trimmed version
file_put_contents("{$this->path}/index", $trimmed);
}
// Create an incomplete request from index data
protected function makeRequestFromIndex($record)
{
$type = $record[7] ?? 'response';
if ($type == 'command') {
$nameField = 'commandName';
} elseif ($type == 'queue-job') {
$nameField = 'jobName';
} elseif ($type == 'test') {
$nameField = 'testName';
} else {
$nameField = 'uri';
}
return new Request(array_combine(
[ 'id', 'time', 'method', $nameField, 'controller', 'responseStatus', 'responseDuration', 'type' ],
array_slice($record, 0, 8) + [ null, null, null, null, null, null, null, 'response' ]
));
}
// Update index with a new request
protected function updateIndex(Request $request)
{
$handle = fopen("{$this->path}/index", 'a');
if (! $handle) return;
if (! flock($handle, LOCK_EX)) return fclose($handle);
if ($request->type == 'command') {
$nameField = 'commandName';
} elseif ($request->type == 'queue-job') {
$nameField = 'jobName';
} elseif ($request->type == 'test') {
$nameField = 'testName';
} else {
$nameField = 'uri';
}
fputcsv($handle, [
$request->id,
$request->time,
$request->method,
$request->$nameField,
$request->controller,
$request->responseStatus,
$request->getResponseDuration(),
$request->type
], ',', '"', PHP_VERSION_ID >= 70400 ? '' : '\\');
flock($handle, LOCK_UN);
fclose($handle);
}
// Ensure the metadata path is writable and initialize it if it doesn't exist, throws exception if it is not writable
protected function ensurePathIsWritable()
{
if (! is_dir($this->path)) {
// directory doesn't exist, try to create one
if (! @mkdir($this->path, $this->pathPermissions, true)) {
throw new \Exception("Directory \"{$this->path}\" does not exist.");
}
// create default .gitignore, to ignore stored json files
file_put_contents("{$this->path}/.gitignore", "*.json\n*.json.gz\nindex\n");
} elseif (! is_writable($this->path)) {
throw new \Exception("Path \"{$this->path}\" is not writable.");
}
if (! is_file($indexFile = "{$this->path}/index")) {
file_put_contents($indexFile, '');
} elseif (! is_writable($indexFile)) {
throw new \Exception("Path \"{$indexFile}\" is not writable.");
}
}
}