PluginProbe
Disk Usage Insights / 1.5
Disk Usage Insights v1.5
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 / Domain / Jobs / PhaseCoordinatorJob.php

PhaseCoordinatorJob.php in Disk Usage Insights 1.5, at src/Domain/Jobs/PhaseCoordinatorJob.php

101 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Mgleis\DiskUsageInsights\Domain\Jobs;
4
5 use Mgleis\DiskUsageInsights\Domain\Collect\DetermineDirCountJob;
6 use Mgleis\DiskUsageInsights\Domain\Collect\DetermineDirRecursiveCountJob;
7 use Mgleis\DiskUsageInsights\Domain\Collect\DetermineDirRecursiveSizesJob;
8 use Mgleis\DiskUsageInsights\Domain\Collect\DetermineDirSizesJob;
9 use Mgleis\DiskUsageInsights\Domain\Collect\DetermineFileSizesJob;
10 use Mgleis\DiskUsageInsights\Domain\Collect\DetermineLastModifiedDateJob;
11 use Mgleis\DiskUsageInsights\Domain\Collect\DetermineWpCoreFileJob;
12 use Mgleis\DiskUsageInsights\Domain\FileEntry;
13 use Mgleis\DiskUsageInsights\Domain\Jobs\BaseJob;
14 use Mgleis\DiskUsageInsights\Domain\Collect\ScanDirForFilesJob;
15 use Mgleis\DiskUsageInsights\Domain\Snapshot;
16
17 class PhaseCoordinatorJob extends BaseJob {
18
19 const CHUNK_SIZE = 250;
20 public function work() {
21
22 $snapshot = $this->snapshotRepository->load();
23 $phase = $snapshot->phase;
24
25 if ($this->queue->size() == 0) {
26 if ($phase == 0) {
27 $this->log("New Phase: Scan for Files");
28 $this->increasePhase($snapshot);
29 $this->chunk($this->fileEntryRepository->count(FileEntry::TYPE_DIR), self::CHUNK_SIZE, function(int $skip, int $count, int $totalCount) {
30 $this->queue->push((new ScanDirForFilesJob($skip, $count, $totalCount))->toArray());
31 });
32 $this->queue->push((new PhaseCoordinatorJob())->toArray());
33 } elseif ($phase == 1) {
34 $this->log("New Phase: Determine File Sizes");
35 $this->increasePhase($snapshot);
36 $this->chunk($this->fileEntryRepository->count(FileEntry::TYPE_FILE), self::CHUNK_SIZE, function(int $skip, int $count, int $totalCount) {
37 $this->queue->push((new DetermineFileSizesJob($skip, $count, $totalCount))->toArray());
38 });
39 $this->queue->push((new PhaseCoordinatorJob())->toArray());
40 } elseif ($phase == 2) {
41 $this->log("New Phase: Determine Dir Sizes");
42 $this->increasePhase($snapshot);
43 $this->queue->push((new DetermineDirSizesJob())->toArray());
44 } elseif ($phase == 3) {
45 $this->log("New Phase: Determine Dir Recursive Sizes");
46 $this->increasePhase($snapshot);
47 $this->queue->push((new DetermineDirRecursiveSizesJob())->toArray());
48 } elseif ($phase == 4) {
49 $this->log("New Phase: Determine Dir Counts");
50 $this->increasePhase($snapshot);
51 $this->queue->push((new DetermineDirCountJob())->toArray());
52 } elseif ($phase == 5) {
53 $this->log("New Phase: Determine Dir Recursive Counts");
54 $this->increasePhase($snapshot);
55 $this->queue->push((new DetermineDirRecursiveCountJob())->toArray());
56 } elseif ($phase == 6) {
57 $this->log("New Phase: Determine Last Modified Date");
58 $this->increasePhase($snapshot);
59 $this->chunk($this->fileEntryRepository->count(), self::CHUNK_SIZE, function(int $skip, int $count, int $totalCount) {
60 $this->queue->push((new DetermineLastModifiedDateJob($skip, $count, $totalCount))->toArray());
61 });
62 $this->queue->push((new PhaseCoordinatorJob())->toArray());
63 } elseif ($phase == 7) {
64 $this->log("New Phase: Determine WP Core Files");
65 $this->increasePhase($snapshot);
66 $this->chunk($this->fileEntryRepository->count(), self::CHUNK_SIZE, function(int $skip, int $count, int $totalCount) {
67 $this->queue->push((new DetermineWpCoreFileJob($skip, $count, $totalCount))->toArray());
68 });
69 $this->queue->push((new PhaseCoordinatorJob())->toArray());
70 } elseif ($phase == 8) {
71 $this->log("New Phase: Finish Collection...");
72 $this->increasePhase($snapshot);
73 $snapshot->collectPhaseFinished = 1;
74 $this->snapshotRepository->save($snapshot);
75 $this->log("DONE");
76 }
77 }
78 }
79
80 private function chunk(int $count, int $itemsPerChunk, Callable $callable) {
81 $pages = 1 + round($count / $itemsPerChunk);
82 for ($i = 0; $i < $pages; $i++) {
83 $callable($i * $itemsPerChunk, $itemsPerChunk, $count);
84 }
85 }
86
87 private function increasePhase(Snapshot $snapshot) {
88 $snapshot->phase++;
89 $this->snapshotRepository->save($snapshot);
90 }
91
92 public function toArray() {
93 return ['type' => self::class, 'args' => []];
94 }
95
96 public function toDescription(): string {
97 return 'Preparing next tasks...';
98 }
99
100 }
101