Skip to content

Commit bcd77f9

Browse files
committed
* Add possibility to save exceptions in file
1 parent 1b08498 commit bcd77f9

File tree

10 files changed

+144
-16
lines changed

10 files changed

+144
-16
lines changed

src/Command/JobShowCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use CodeRhapsodie\DataflowBundle\Entity\Job;
88
use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
9+
use CodeRhapsodie\DataflowBundle\Gateway\JobGateway;
910
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
1011
use Symfony\Component\Console\Attribute\AsCommand;
1112
use Symfony\Component\Console\Command\Command;
@@ -26,7 +27,7 @@ class JobShowCommand extends Command
2627
Job::STATUS_COMPLETED => 'Completed',
2728
];
2829

29-
public function __construct(private JobRepository $jobRepository, private ConnectionFactory $connectionFactory)
30+
public function __construct(private JobGateway $jobGateway, private ConnectionFactory $connectionFactory)
3031
{
3132
parent::__construct();
3233
}
@@ -58,9 +59,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5859
}
5960

6061
if ($scheduleId) {
61-
$job = $this->jobRepository->findLastForDataflowId($scheduleId);
62+
$job = $this->jobGateway->findLastForDataflowId($scheduleId);
6263
} elseif ($jobId) {
63-
$job = $this->jobRepository->find($jobId);
64+
$job = $this->jobGateway->find($jobId);
6465
} else {
6566
$io->error('You must pass `job-id` or `schedule-id` option.');
6667

src/DependencyInjection/CodeRhapsodieDataflowExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function load(array $configs, ContainerBuilder $container): void
3333

3434
if ($config['exceptions_mode']['type'] === 'file') {
3535
$container->setParameter('coderhapsodie.dataflow.flysystem_service', $config['exceptions_mode']['flysystem_service']);
36+
$loader->load('exceptions_services.yaml');
3637
}
3738

3839
if ($config['messenger_mode']['enabled']) {

src/DependencyInjection/Compiler/ExceptionCompilerPass.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace CodeRhapsodie\DataflowBundle\DependencyInjection\Compiler;
66

7-
use CodeRhapsodie\DataflowBundle\Processor\JobProcessor;
7+
use CodeRhapsodie\DataflowBundle\ExceptionsHandler\FilesystemExceptionHandler;
88
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
99
use Symfony\Component\DependencyInjection\ContainerBuilder;
1010
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
@@ -23,7 +23,7 @@ public function process(ContainerBuilder $container): void
2323
throw new InvalidArgumentException(\sprintf('Service "%s" not found', $flysystem));
2424
}
2525

26-
$definition = $container->findDefinition(JobProcessor::class);
26+
$definition = $container->findDefinition(FilesystemExceptionHandler::class);
2727
$definition->setArgument('$filesystem', new Reference($flysystem));
2828
}
2929
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodeRhapsodie\DataflowBundle\ExceptionsHandler;
6+
7+
interface ExceptionHandlerInterface
8+
{
9+
public function save(?int $jobId, ?array $exceptions): void;
10+
11+
public function find(int $jobId): ?array;
12+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodeRhapsodie\DataflowBundle\ExceptionsHandler;
6+
7+
use League\Flysystem\Filesystem;
8+
use League\Flysystem\FilesystemException;
9+
10+
class FilesystemExceptionHandler implements ExceptionHandlerInterface
11+
{
12+
public function __construct(private Filesystem $filesystem)
13+
{
14+
}
15+
16+
public function save(?int $jobId, ?array $exceptions): void
17+
{
18+
if ($jobId === null || empty($exceptions)) {
19+
return;
20+
}
21+
22+
$this->filesystem->write(sprintf('dataflow-job-%s.log', $jobId), json_encode($exceptions));
23+
}
24+
25+
public function find(int $jobId): ?array
26+
{
27+
try {
28+
if (!$this->filesystem->has(sprintf('dataflow-job-%s.log', $jobId))) {
29+
return [];
30+
}
31+
32+
return json_decode($this->filesystem->read(sprintf('dataflow-job-%s.log', $jobId)), true);
33+
} catch (FilesystemException) {
34+
return [];
35+
}
36+
}
37+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodeRhapsodie\DataflowBundle\ExceptionsHandler;
6+
7+
class NullExceptionHandler implements ExceptionHandlerInterface
8+
{
9+
10+
public function save(?int $jobId, ?array $exceptions): void
11+
{
12+
}
13+
14+
public function find(int $jobId): ?array
15+
{
16+
return null;
17+
}
18+
}

src/Gateway/JobGateway.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodeRhapsodie\DataflowBundle\Gateway;
6+
7+
use CodeRhapsodie\DataflowBundle\Entity\Job;
8+
use CodeRhapsodie\DataflowBundle\ExceptionsHandler\ExceptionHandlerInterface;
9+
use CodeRhapsodie\DataflowBundle\ExceptionsHandler\NullExceptionHandler;
10+
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
11+
12+
class JobGateway
13+
{
14+
public function __construct(private JobRepository $repository, private ExceptionHandlerInterface $exceptionHandler)
15+
{
16+
}
17+
18+
public function find(int $jobId): ?Job
19+
{
20+
$job = $this->repository->find($jobId);
21+
22+
return $this->loadExceptions($job);
23+
}
24+
25+
public function save(Job $job): void
26+
{
27+
if (!$this->exceptionHandler instanceof NullExceptionHandler) {
28+
$this->exceptionHandler->save($job->getId(), $job->getExceptions());
29+
$job->setExceptions([]);
30+
}
31+
32+
$this->repository->save($job);
33+
}
34+
35+
public function findLastForDataflowId(int $scheduleId): ?Job
36+
{
37+
$job = $this->repository->findLastForDataflowId($scheduleId);
38+
39+
return $this->loadExceptions($job);
40+
}
41+
42+
private function loadExceptions(?Job $job): ?Job
43+
{
44+
if ($job === null || $this->exceptionHandler instanceof NullExceptionHandler) {
45+
return $job;
46+
}
47+
48+
$this->exceptionHandler->save($job->getId(), $job->getExceptions());
49+
50+
return $job->setExceptions($this->exceptionHandler->find($job->getId()));
51+
}
52+
}

src/Processor/JobProcessor.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use CodeRhapsodie\DataflowBundle\Entity\Job;
1010
use CodeRhapsodie\DataflowBundle\Event\Events;
1111
use CodeRhapsodie\DataflowBundle\Event\ProcessingEvent;
12+
use CodeRhapsodie\DataflowBundle\Gateway\JobGateway;
1213
use CodeRhapsodie\DataflowBundle\Logger\BufferHandler;
1314
use CodeRhapsodie\DataflowBundle\Logger\DelegatingLogger;
1415
use CodeRhapsodie\DataflowBundle\Registry\DataflowTypeRegistryInterface;
@@ -27,7 +28,7 @@ public function __construct(
2728
private JobRepository $repository,
2829
private DataflowTypeRegistryInterface $registry,
2930
private EventDispatcherInterface $dispatcher,
30-
private ?Filesystem $filesystem = null
31+
private JobGateway $jobGateway,
3132
)
3233
{
3334
}
@@ -70,25 +71,19 @@ private function beforeProcessing(Job $job): void
7071
->setStatus(Job::STATUS_RUNNING)
7172
->setStartTime(new \DateTime())
7273
;
73-
$this->repository->save($job);
74+
$this->jobGateway->save($job);
7475
}
7576

7677
private function afterProcessing(Job $job, Result $result, BufferHandler $bufferLogger): void
7778
{
78-
$exceptions = $bufferLogger->clearBuffer();
79-
if ($this->filesystem) {
80-
$this->filesystem->write(sprintf('dataflow-job-%s.log',$job->getId()), json_encode($exceptions));
81-
$exceptions = [];
82-
}
83-
8479
$job
8580
->setEndTime($result->getEndTime())
8681
->setStatus(Job::STATUS_COMPLETED)
8782
->setCount($result->getSuccessCount())
88-
->setExceptions($exceptions)
83+
->setExceptions($bufferLogger->clearBuffer())
8984
;
9085

91-
$this->repository->save($job);
86+
$this->jobGateway->save($job);
9287

9388
$this->dispatcher->dispatch(new ProcessingEvent($job), Events::AFTER_PROCESSING);
9489
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
CodeRhapsodie\DataflowBundle\ExceptionsHandler\ExceptionHandlerInterface: '@CodeRhapsodie\DataflowBundle\ExceptionsHandler\FilesystemExceptionHandler'
3+
CodeRhapsodie\DataflowBundle\ExceptionsHandler\FilesystemExceptionHandler:
4+
arguments:
5+
$filesystem: ~ # Filled in compiler pass

src/Resources/config/services.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ services:
2828

2929
CodeRhapsodie\DataflowBundle\Command\JobShowCommand:
3030
arguments:
31-
$jobRepository: '@CodeRhapsodie\DataflowBundle\Repository\JobRepository'
31+
$jobGateway: '@CodeRhapsodie\DataflowBundle\Gateway\JobGateway'
3232
$connectionFactory: '@CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory'
3333
tags: ['console.command']
3434

@@ -93,3 +93,10 @@ services:
9393
$repository: '@CodeRhapsodie\DataflowBundle\Repository\JobRepository'
9494
$registry: '@CodeRhapsodie\DataflowBundle\Registry\DataflowTypeRegistryInterface'
9595
$dispatcher: '@event_dispatcher'
96+
$jobGateway: '@CodeRhapsodie\DataflowBundle\Gateway\JobGateway'
97+
CodeRhapsodie\DataflowBundle\ExceptionsHandler\ExceptionHandlerInterface: '@CodeRhapsodie\DataflowBundle\ExceptionsHandler\NullExceptionHandler'
98+
CodeRhapsodie\DataflowBundle\ExceptionsHandler\NullExceptionHandler:
99+
CodeRhapsodie\DataflowBundle\Gateway\JobGateway:
100+
arguments:
101+
$repository: '@CodeRhapsodie\DataflowBundle\Repository\JobRepository'
102+
$exceptionHandler: '@CodeRhapsodie\DataflowBundle\ExceptionsHandler\ExceptionHandlerInterface'

0 commit comments

Comments
 (0)