Skip to content

Commit 457b4e2

Browse files
authored
Download logs (#11)
* Add possibility to download log file
1 parent 5921aa4 commit 457b4e2

4 files changed

Lines changed: 49 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Version 6.4.0
2+
* Add the possibility to download the log file
3+
14
# Version 6.3.0
25
* add stream exceptions management
36

src/Controller/JobController.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,23 @@ public function displayLog(int $id): Response
4545
$log = array_map(fn($line) => preg_replace('~#\d+~', "\n$0", (string) $line), $item->getExceptions() ?? []);
4646

4747
if (empty($log) && $item->getStreamExceptions()) {
48-
return new StreamedResponse(function () use ($item) {
48+
return new StreamedResponse(function () use ($item, $id) {
49+
$maxBytes = 1048576;
50+
$bytesRead = 0;
51+
4952
while (($line = fgets($item->getStreamExceptions())) !== false) {
50-
echo "<p>",preg_replace('~#\d+~', "<br>$0", (string) $line),"</p>";
53+
$bytesRead += strlen($line);
54+
55+
if ($bytesRead > $maxBytes) {
56+
echo sprintf('<p><strong>[%s]</strong></p><p><a href="%s">%s</a></p>',
57+
$this->translator->trans('coderhapsodie.ibexa_dataflow.logs.trucated'),
58+
$this->generateUrl('coderhapsodie.ibexa_dataflow.job.log.download', ['id' => $id]),
59+
$this->translator->trans('coderhapsodie.ibexa_dataflow.logs.download')
60+
);
61+
break;
62+
}
63+
64+
echo "<p>", preg_replace('~#\d+~', "<br>$0", (string)$line), "</p>";
5165
flush();
5266
}
5367
});
@@ -58,6 +72,32 @@ public function displayLog(int $id): Response
5872
]);
5973
}
6074

75+
#[Route(path: '/details/log/{id}/download', name: 'coderhapsodie.ibexa_dataflow.job.log.download', methods: 'GET')]
76+
public function downloadLog(int $id): Response
77+
{
78+
$this->denyAccessUnlessGranted(new Attribute('ibexa_dataflow', 'view'));
79+
$item = $this->jobGateway->find($id);
80+
$log = array_map(fn($line) => preg_replace('~#\d+~', "\n$0", (string) $line), $item->getExceptions() ?? []);
81+
82+
if (empty($log) && $item->getStreamExceptions()) {
83+
$headers = [
84+
'Content-Type' => 'plain/text; charset=utf-8',
85+
'Content-Disposition' => sprintf('attachment; filename="%s.log"', $item->getLabel().'-'.$item->getStartTime()->format('Y-m-d-H-i-s')),
86+
];
87+
88+
return new StreamedResponse(function () use ($item) {
89+
while (($line = fgets($item->getStreamExceptions())) !== false) {
90+
echo $line;
91+
flush();
92+
}
93+
}, headers: $headers);
94+
}
95+
96+
throw $this->createNotFoundException();
97+
}
98+
99+
100+
61101
#[Route(path: '/create', name: 'coderhapsodie.ibexa_dataflow.job.create', methods: ['POST'])]
62102
public function create(Request $request): Response
63103
{

src/Resources/translations/messages.en.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,5 @@ coderhapsodie.ibexa_dataflow.powered_by: 'Powered by'
9191
coderhapsodie.ibexa_dataflow.made_by: 'Made by'
9292
coderhapsodie.ibexa_dataflow.dashboard: Dashboard
9393
coderhapsodie.ibexa_dataflow.dashboard.title: Running or waiting tasks
94+
coderhapsodie.ibexa_dataflow.logs.trucated: 'Logs are truncated (over 1Mo).'
95+
coderhapsodie.ibexa_dataflow.logs.download: 'Download full logs'

src/Resources/translations/messages.fr.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,5 @@ coderhapsodie.ibexa_dataflow.powered_by: 'Propulsé par'
8989
coderhapsodie.ibexa_dataflow.made_by: 'Fabriqué par'
9090
coderhapsodie.ibexa_dataflow.dashboard: Dashboard
9191
coderhapsodie.ibexa_dataflow.dashboard.title: Tâches en cours ou en attente
92+
coderhapsodie.ibexa_dataflow.logs.trucated: 'Les logs sont tronqués (supérieur à 1Mo).'
93+
coderhapsodie.ibexa_dataflow.logs.download: 'Télécharger tous les logs'

0 commit comments

Comments
 (0)