|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Oliverde8\PhpEtlBundle\Services; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Oliverde8\PhpEtlBundle\Entity\EtlExecution; |
|
8
|
|
|
use Symfony\Component\Filesystem\Exception\IOException; |
|
9
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
10
|
|
|
|
|
11
|
|
|
class ChainWorkDirManager |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var string */ |
|
14
|
|
|
protected $baseDir; |
|
15
|
|
|
|
|
16
|
|
|
/** @var Filesystem */ |
|
17
|
|
|
protected $fileSystem; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* ChainWorkDirManager constructor. |
|
21
|
|
|
* @param string $directory |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(string $baseDir, Filesystem $filesystem) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->baseDir = $baseDir; |
|
26
|
|
|
$this->fileSystem = $filesystem; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param EtlExecution $execution |
|
31
|
|
|
* @param bool $createIfMissing |
|
32
|
|
|
* @return string |
|
33
|
|
|
* |
|
34
|
|
|
* @throws IOException if directory can't be created. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getWorkDir(EtlExecution $execution, $createIfMissing = true): string |
|
37
|
|
|
{ |
|
38
|
|
|
$dir = $this->baseDir . "/" . $execution->getCreateTime()->format("y/m/d") . "/id-" . $execution->getId() . "/"; |
|
39
|
|
|
|
|
40
|
|
|
if ($createIfMissing) { |
|
41
|
|
|
$this->fileSystem->mkdir($dir); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $dir; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function listFiles(EtlExecution $execution): array |
|
48
|
|
|
{ |
|
49
|
|
|
$files = []; |
|
50
|
|
|
$dir = $this->getWorkDir($execution); |
|
51
|
|
|
|
|
52
|
|
|
$handle = opendir($dir); |
|
53
|
|
|
while (false !== ($entry = readdir($handle))) { |
|
54
|
|
|
if ($entry != "." && $entry != "..") { |
|
55
|
|
|
$files[] = $entry; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $files; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getFirstLogLines(EtlExecution $execution, $nbLines = 100): array |
|
63
|
|
|
{ |
|
64
|
|
|
$logFile = $this->getWorkDir($execution) . "/execution.log"; |
|
65
|
|
|
$logLines = []; |
|
66
|
|
|
|
|
67
|
|
|
if (!is_file($logFile)) { |
|
68
|
|
|
return $logLines; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$file = fopen($logFile, 'rb'); |
|
72
|
|
|
$nbLine = 0; |
|
73
|
|
|
|
|
74
|
|
|
while ($nbLine <= $nbLines && $line = fgets($file)) { |
|
75
|
|
|
$nbLine++; |
|
76
|
|
|
$logLines[] = $line; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $logLines; |
|
80
|
|
|
} |
|
81
|
|
|
} |