PluginProbe
Disk Usage Insights / 1.2
Disk Usage Insights v1.2
trunk 1.0 1.10 1.11 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
disk-usage-insights / src / Frontend / Controller / ScanWorkerController.php

ScanWorkerController.php in Disk Usage Insights 1.2, at src/Frontend/Controller/ScanWorkerController.php

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