PluginProbe
Disk Usage Insights / 1.8
Disk Usage Insights v1.8
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.8, at src/Frontend/Controller/ScanWorkerController.php

49 lines 1.6 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\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