| 1 |
<?php |
| 2 |
namespace Mgleis\DiskUsageInsights\Frontend\Controller; |
| 3 |
|
| 4 |
use Mgleis\DiskUsageInsights\Domain\DatabaseRepository; |
| 5 |
use Mgleis\DiskUsageInsights\Plugin; |
| 6 |
use Mgleis\PhpSqliteJobQueue\Job; |
| 7 |
use Mgleis\PhpSqliteJobQueue\Worker; |
| 8 |
|
| 9 |
class ScanWorkerController { |
| 10 |
|
| 11 |
public function worker() { |
| 12 |
check_ajax_referer(Plugin::NONCE); |
| 13 |
|
| 14 |
// TODO validate value: ensure file exists in data directory |
| 15 |
$snapshotName = sanitize_file_name(wp_unslash($_POST['snapshot'] ?? '')); |
| 16 |
|
| 17 |
$database = (new DatabaseRepository())->loadDatabase($snapshotName); |
| 18 |
|
| 19 |
// if process finished = stop reloading |
| 20 |
$snapshot = $database->snapshotRepository->load(); |
| 21 |
if ($snapshot->collectPhaseFinished === 1) { |
| 22 |
http_response_code(286); // htmx stops timer |
| 23 |
echo "DONE"; |
| 24 |
exit; |
| 25 |
} |
| 26 |
|
| 27 |
$w = (new Worker($database->q)) |
| 28 |
->withMaxTotalRuntimeInSeconds(5) |
| 29 |
->withSleepTimeBetweenJobsInMilliseconds(0) |
| 30 |
->withSleepTimeOnEmptyQueueInMilliseconds(0) |
| 31 |
; |
| 32 |
|
| 33 |
$w->process(function(Job $job) use ($database) { |
| 34 |
$reflect = new \ReflectionClass($job->payload['type']); |
| 35 |
$instance = $reflect->newInstanceArgs($job->payload['args']); |
| 36 |
$instance->setQueue($database->q); |
| 37 |
$instance->setFileEntryRepository($database->fileEntryRepository); |
| 38 |
$instance->setSnapshotRepository($database->snapshotRepository); |
| 39 |
|
| 40 |
$database->q->db->beginTransaction(); |
| 41 |
$instance->work(); |
| 42 |
$database->q->db->commit(); |
| 43 |
}); |
| 44 |
|
| 45 |
wp_die(); // All ajax handlers should die when finished |
| 46 |
} |
| 47 |
|
| 48 |
} |
| 49 |
|