mirror of
https://github.com/code-rhapsodie/dataflow-bundle.git
synced 2026-03-24 14:52:21 +01:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9d76b45771 |
12
.github/workflows/ci.yml
vendored
12
.github/workflows/ci.yml
vendored
@@ -1,12 +0,0 @@
|
||||
name: CI
|
||||
|
||||
on: [push]
|
||||
|
||||
jobs:
|
||||
build-test:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: php-actions/composer@v6 # or alternative dependency management
|
||||
- uses: php-actions/phpunit@v4
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -3,6 +3,3 @@ composer.lock
|
||||
.phpunit.result.cache
|
||||
.php_cs.cache
|
||||
.php_cs
|
||||
.idea
|
||||
.phpunit.cache
|
||||
.php-version
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
# Version 4.1.0
|
||||
# Version 4.2.0
|
||||
* Added custom index for job status
|
||||
|
||||
# Version 4.1.0
|
||||
* Added custom index for exception log
|
||||
|
||||
# Version 4.0.0
|
||||
|
||||
111
README.md
111
README.md
@@ -1,23 +1,14 @@
|
||||
# Code Rhapsodie Dataflow Bundle
|
||||
|
||||
DataflowBundle is a bundle for Symfony 3.4+
|
||||
DataflowBundle is a bundle for Symfony 3.4+
|
||||
providing an easy way to create import / export dataflow.
|
||||
|
||||
| Dataflow | Symfony | Support |
|
||||
|----------|--------------------------|---------|
|
||||
| 5.x | 7.x | yes |
|
||||
| 4.x | 3.4 \| 4.x \| 5.x \| 6.x | yes |
|
||||
| 3.x | 3.4 \| 4.x \| 5.x | no |
|
||||
| 2.x | 3.4 \| 4.x | no |
|
||||
| 1.x | 3.4 \| 4.x | no |
|
||||
|
||||
Dataflow uses a linear generic workflow in three parts:
|
||||
* one reader
|
||||
* any number of steps that can be synchronous or asynchronous
|
||||
* one or more writers
|
||||
|
||||
* one reader
|
||||
* any number of steps that can be synchronous or asynchronous
|
||||
* one or more writers
|
||||
|
||||
The reader can read data from anywhere and return data row by row. Each step processes the current row data.
|
||||
The reader can read data from anywhere and return data row by row. Each step processes the current row data.
|
||||
The steps are executed in the order in which they are added.
|
||||
And, one or more writers save the row anywhere you want.
|
||||
|
||||
@@ -36,6 +27,7 @@ As the following schema shows, you can define more than one dataflow:
|
||||
* Display the result for the last Job for a Dataflow from the command line
|
||||
* Work with multiple Doctrine DBAL connections
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
Security notice: Symfony 4.x is not supported before 4.1.12, see https://github.com/advisories/GHSA-pgwj-prpq-jpc2
|
||||
@@ -52,8 +44,7 @@ $ composer require code-rhapsodie/dataflow-bundle
|
||||
|
||||
You can use the generic readers, writers and steps from [PortPHP](https://github.com/portphp/portphp).
|
||||
|
||||
For the writers, you must use the adapter `CodeRhapsodie\DataflowBundle\DataflowType\Writer\PortWriterAdapter` like
|
||||
this:
|
||||
For the writers, you must use the adapter `CodeRhapsodie\DataflowBundle\DataflowType\Writer\PortWriterAdapter` like this:
|
||||
|
||||
```php
|
||||
<?php
|
||||
@@ -66,7 +57,9 @@ $builder->addWriter(new \CodeRhapsodie\DataflowBundle\DataflowType\Writer\PortWr
|
||||
|
||||
### Register the bundle
|
||||
|
||||
Add `CodeRhapsodie\DataflowBundle\CodeRhapsodieDataflowBundle::class => ['all' => true],
|
||||
#### Symfony 4 (new tree)
|
||||
|
||||
For Symfony 4, add `CodeRhapsodie\DataflowBundle\CodeRhapsodieDataflowBundle::class => ['all' => true],
|
||||
` in the `config/bundles.php` file.
|
||||
|
||||
Like this:
|
||||
@@ -81,13 +74,32 @@ return [
|
||||
];
|
||||
```
|
||||
|
||||
#### Symfony 3.4 (old tree)
|
||||
|
||||
For Symfony 3.4, add a new line in the `app/AppKernel.php` file.
|
||||
|
||||
Like this:
|
||||
|
||||
```php
|
||||
<?php
|
||||
// app/AppKernel.php
|
||||
|
||||
public function registerBundles()
|
||||
{
|
||||
$bundles = [
|
||||
// ...
|
||||
new CodeRhapsodie\DataflowBundle\CodeRhapsodieDataflowBundle(),
|
||||
// ...
|
||||
];
|
||||
}
|
||||
```
|
||||
|
||||
### Update the database
|
||||
|
||||
This bundle uses Doctrine DBAL to store Dataflow schedule into the database table (`cr_dataflow_scheduled`)
|
||||
and jobs (`cr_dataflow_job`).
|
||||
|
||||
If you use [Doctrine Migration Bundle](https://symfony.com/doc/master/bundles/DoctrineMigrationsBundle/index.html)
|
||||
or [Phinx](https://phinx.org/)
|
||||
If you use [Doctrine Migration Bundle](https://symfony.com/doc/master/bundles/DoctrineMigrationsBundle/index.html) or [Phinx](https://phinx.org/)
|
||||
or [Kaliop Migration Bundle](https://github.com/kaliop-uk/ezmigrationbundle) or whatever,
|
||||
you can add a new migration with the generated SQL query from this command:
|
||||
|
||||
@@ -125,7 +137,6 @@ Dataflow can delegate the execution of its jobs to the Symfony messenger compone
|
||||
This allows jobs to be executed concurrently by workers instead of sequentially.
|
||||
|
||||
To enable messenger mode:
|
||||
|
||||
```yaml
|
||||
code_rhapsodie_dataflow:
|
||||
messenger_mode:
|
||||
@@ -134,7 +145,6 @@ code_rhapsodie_dataflow:
|
||||
```
|
||||
|
||||
You also need to route Dataflow messages to the proper transport:
|
||||
|
||||
```yaml
|
||||
# config/packages/messenger.yaml
|
||||
framework:
|
||||
@@ -148,11 +158,9 @@ framework:
|
||||
|
||||
## Define a dataflow type
|
||||
|
||||
This bundle uses a fixed and simple workflow structure in order to let you focus on the data processing logic part of
|
||||
your dataflow.
|
||||
This bundle uses a fixed and simple workflow structure in order to let you focus on the data processing logic part of your dataflow.
|
||||
|
||||
A dataflow type defines the different parts of your dataflow. A dataflow is made of:
|
||||
|
||||
- exactly one *Reader*
|
||||
- any number of *Steps*
|
||||
- one or more *Writers*
|
||||
@@ -161,10 +169,8 @@ Dataflow types can be configured with options.
|
||||
|
||||
A dataflow type must implement `CodeRhapsodie\DataflowBundle\DataflowType\DataflowTypeInterface`.
|
||||
|
||||
To help with creating your dataflow types, an abstract
|
||||
class `CodeRhapsodie\DataflowBundle\DataflowType\AbstractDataflowType`
|
||||
is provided, allowing you to define your dataflow through a handy
|
||||
builder `CodeRhapsodie\DataflowBundle\DataflowType\DataflowBuilder`.
|
||||
To help with creating your dataflow types, an abstract class `CodeRhapsodie\DataflowBundle\DataflowType\AbstractDataflowType`
|
||||
is provided, allowing you to define your dataflow through a handy builder `CodeRhapsodie\DataflowBundle\DataflowType\DataflowBuilder`.
|
||||
|
||||
This is an example to define one class DataflowType:
|
||||
|
||||
@@ -224,16 +230,15 @@ class MyFirstDataflowType extends AbstractDataflowType
|
||||
|
||||
Dataflow types must be tagged with `coderhapsodie.dataflow.type`.
|
||||
|
||||
If you're using Symfony auto-configuration for your services, this tag will be automatically added to all services
|
||||
implementing `DataflowTypeInterface`.
|
||||
If you're using Symfony auto-configuration for your services, this tag will be automatically added to all services implementing `DataflowTypeInterface`.
|
||||
|
||||
Otherwise, manually add the tag `coderhapsodie.dataflow.type` in your dataflow type service configuration:
|
||||
|
||||
```yaml
|
||||
```yaml
|
||||
CodeRhapsodie\DataflowExemple\DataflowType\MyFirstDataflowType:
|
||||
tags:
|
||||
- { name: coderhapsodie.dataflow.type }
|
||||
CodeRhapsodie\DataflowExemple\DataflowType\MyFirstDataflowType:
|
||||
tags:
|
||||
- { name: coderhapsodie.dataflow.type }
|
||||
```
|
||||
|
||||
### Use options for your dataflow type
|
||||
@@ -259,14 +264,11 @@ class MyFirstDataflowType extends AbstractDataflowType
|
||||
}
|
||||
```
|
||||
|
||||
With this configuration, the option `fileName` is required. For an advanced usage of the option resolver, read
|
||||
the [Symfony documentation](https://symfony.com/doc/current/components/options_resolver.html).
|
||||
With this configuration, the option `fileName` is required. For an advanced usage of the option resolver, read the [Symfony documentation](https://symfony.com/doc/current/components/options_resolver.html).
|
||||
|
||||
For asynchronous management, `AbstractDataflowType` come with two default options :
|
||||
|
||||
- loopInterval : default to 0. Update this interval if you wish customise the `tick` loop duration.
|
||||
- emitInterval : default to 0. Update this interval to have a control when reader must emit new data in the flow
|
||||
pipeline.
|
||||
- emitInterval : default to 0. Update this interval to have a control when reader must emit new data in the flow pipeline.
|
||||
|
||||
### Logging
|
||||
|
||||
@@ -290,15 +292,14 @@ class MyDataflowType extends AbstractDataflowType
|
||||
}
|
||||
```
|
||||
|
||||
When using the `code-rhapsodie:dataflow:run-pending` command, this logger will also be used to save the log in the
|
||||
corresponding job in the database.
|
||||
When using the `code-rhapsodie:dataflow:run-pending` command, this logger will also be used to save the log in the corresponding job in the database.
|
||||
|
||||
### Check if your DataflowType is ready
|
||||
|
||||
Execute this command to check if your DataflowType is correctly registered:
|
||||
|
||||
```shell script
|
||||
$ bin/console debug:container --tag coderhapsodie.dataflow.type
|
||||
$ bin/console debug:container --tag coderhapsodie.dataflow.type --show-private
|
||||
```
|
||||
|
||||
The result is like this:
|
||||
@@ -315,10 +316,10 @@ Symfony Container Public and Private Services Tagged with "coderhapsodie.dataflo
|
||||
|
||||
```
|
||||
|
||||
|
||||
### Readers
|
||||
|
||||
*Readers* provide the dataflow with elements to import / export. Usually, elements are read from an external resource (
|
||||
file, database, webservice, etc).
|
||||
*Readers* provide the dataflow with elements to import / export. Usually, elements are read from an external resource (file, database, webservice, etc).
|
||||
|
||||
A *Reader* can be any `iterable`.
|
||||
|
||||
@@ -356,16 +357,15 @@ You can set up this reader as follows:
|
||||
$builder->setReader(($this->myReader)())
|
||||
```
|
||||
|
||||
|
||||
### Steps
|
||||
|
||||
*Steps* are operations performed on the elements before they are handled by the *Writers*. Usually, steps are either:
|
||||
|
||||
- converters, that alter the element
|
||||
- filters, that conditionally prevent further operations on the element
|
||||
- generators, that can include asynchronous operations
|
||||
|
||||
A *Step* can be any callable, taking the element as its argument, and returning either:
|
||||
|
||||
- the element, possibly altered
|
||||
- `false`, if no further operations should be performed on this element
|
||||
|
||||
@@ -409,8 +409,7 @@ Note : you can ensure writing order for asynchronous operations if all steps are
|
||||
*Writers* perform the actual import / export operations.
|
||||
|
||||
A *Writer* must implement `CodeRhapsodie\DataflowBundle\DataflowType\Writer\WriterInterface`.
|
||||
As this interface is not compatible with `Port\Writer`, the
|
||||
adapter `CodeRhapsodie\DataflowBundle\DataflowType\Writer\PortWriterAdapter` is provided.
|
||||
As this interface is not compatible with `Port\Writer`, the adapter `CodeRhapsodie\DataflowBundle\DataflowType\Writer\PortWriterAdapter` is provided.
|
||||
|
||||
This example show how to use the predefined PhpPort Writer :
|
||||
|
||||
@@ -461,9 +460,7 @@ class FileWriter implements WriterInterface
|
||||
|
||||
#### CollectionWriter
|
||||
|
||||
If you want to write multiple items from a single item read, you can use the generic `CollectionWriter`. This writer
|
||||
will iterate over any `iterable` it receives, and pass each item from that collection to your own writer that handles
|
||||
single items.
|
||||
If you want to write multiple items from a single item read, you can use the generic `CollectionWriter`. This writer will iterate over any `iterable` it receives, and pass each item from that collection to your own writer that handles single items.
|
||||
|
||||
```php
|
||||
$builder->addWriter(new CollectionWriter($mySingleItemWriter));
|
||||
@@ -473,11 +470,9 @@ $builder->addWriter(new CollectionWriter($mySingleItemWriter));
|
||||
|
||||
If you want to call different writers depending on what item is read, you can use the generic `DelegatorWriter`.
|
||||
|
||||
As an example, let's suppose our items are arrays with the first entry being either `product` or `order`. We want to use
|
||||
a different writer based on that value.
|
||||
As an example, let's suppose our items are arrays with the first entry being either `product` or `order`. We want to use a different writer based on that value.
|
||||
|
||||
First, create your writers implementing `DelegateWriterInterface` (this interface extends `WriterInterface` so your
|
||||
writers can still be used without the `DelegatorWriter`).
|
||||
First, create your writers implementing `DelegateWriterInterface` (this interface extends `WriterInterface` so your writers can still be used without the `DelegatorWriter`).
|
||||
|
||||
```php
|
||||
<?php
|
||||
@@ -550,8 +545,7 @@ Then, configure your `DelegatorWriter` and add it to your dataflow type.
|
||||
}
|
||||
```
|
||||
|
||||
During execution, the `DelegatorWriter` will simply pass each item received to its first delegate (in the order those
|
||||
were added) that supports it. If no delegate supports an item, an exception will be thrown.
|
||||
During execution, the `DelegatorWriter` will simply pass each item received to its first delegate (in the order those were added) that supports it. If no delegate supports an item, an exception will be thrown.
|
||||
|
||||
## Queue
|
||||
|
||||
@@ -569,8 +563,7 @@ Several commands are provided to manage schedules and run jobs.
|
||||
|
||||
`code-rhapsodie:dataflow:run-pending` Executes job in the queue according to their schedule.
|
||||
|
||||
When messenger mode is enabled, jobs will still be created according to their schedule, but execution will be handled by
|
||||
the messenger component instead.
|
||||
When messenger mode is enabled, jobs will still be created according to their schedule, but execution will be handled by the messenger component instead.
|
||||
|
||||
`code-rhapsodie:dataflow:schedule:list` Display the list of dataflows scheduled.
|
||||
|
||||
@@ -609,7 +602,7 @@ $ bin/console code-rhapsodie:dataflow:run-pending --connection=default
|
||||
|
||||
Please report issues and request features at https://github.com/code-rhapsodie/dataflow-bundle/issues.
|
||||
|
||||
Please note that only the last release of the 4.x and the 5.x versions of this bundle are actively supported.
|
||||
Please note that only the last release of the 3.x and the 4.x versions of this bundle are actively supported.
|
||||
|
||||
# Contributing
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace CodeRhapsodie\DataflowBundle\Tests\DataflowType\Dataflow;
|
||||
|
||||
use Amp\Delayed;
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Dataflow\AMPAsyncDataflow;
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Dataflow\Dataflow;
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Writer\WriterInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
|
||||
@@ -40,13 +40,10 @@ class CollectionWriterTest extends TestCase
|
||||
->expects($this->once())
|
||||
->method('finish')
|
||||
;
|
||||
$matcher = $this->exactly(count($values));
|
||||
$embeddedWriter
|
||||
->expects($matcher)
|
||||
->expects($this->exactly(count($values)))
|
||||
->method('write')
|
||||
->with($this->callback(function ($arg) use ($matcher, $values) {
|
||||
return $arg === $values[$matcher->numberOfInvocations() - 1];
|
||||
}))
|
||||
->withConsecutive(...array_map(fn($item) => [$item], $values))
|
||||
;
|
||||
|
||||
$writer = new CollectionWriter($embeddedWriter);
|
||||
|
||||
@@ -10,10 +10,13 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DelegatorWriterTest extends TestCase
|
||||
{
|
||||
private DelegatorWriter $delegatorWriter;
|
||||
private DelegateWriterInterface|MockObject $delegateInt;
|
||||
private DelegateWriterInterface|MockObject $delegateString;
|
||||
private DelegateWriterInterface|MockObject $delegateArray;
|
||||
private \CodeRhapsodie\DataflowBundle\DataflowType\Writer\DelegatorWriter $delegatorWriter;
|
||||
|
||||
private \CodeRhapsodie\DataflowBundle\DataflowType\Writer\DelegateWriterInterface|\PHPUnit\Framework\MockObject\MockObject $delegateInt;
|
||||
|
||||
private \CodeRhapsodie\DataflowBundle\DataflowType\Writer\DelegateWriterInterface|\PHPUnit\Framework\MockObject\MockObject $delegateString;
|
||||
|
||||
private \CodeRhapsodie\DataflowBundle\DataflowType\Writer\DelegateWriterInterface|\PHPUnit\Framework\MockObject\MockObject $delegateArray;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
|
||||
@@ -12,7 +12,7 @@ class PortWriterAdapterTest extends TestCase
|
||||
$value = 'not an array';
|
||||
|
||||
$writer = $this->getMockBuilder('\Port\Writer')
|
||||
->onlyMethods(['prepare', 'finish', 'writeItem'])
|
||||
->setMethods(['prepare', 'finish', 'writeItem'])
|
||||
->getMock()
|
||||
;
|
||||
$writer
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Tests\Manager;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\DataflowTypeInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Entity\Job;
|
||||
use CodeRhapsodie\DataflowBundle\Entity\ScheduledDataflow;
|
||||
use CodeRhapsodie\DataflowBundle\Exceptions\UnknownDataflowTypeException;
|
||||
use CodeRhapsodie\DataflowBundle\Manager\ScheduledDataflowManager;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\ScheduledDataflowRepository;
|
||||
@@ -13,10 +15,13 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ScheduledDataflowManagerTest extends TestCase
|
||||
{
|
||||
private ScheduledDataflowManager $manager;
|
||||
private Connection|MockObject $connection;
|
||||
private ScheduledDataflowRepository|MockObject $scheduledDataflowRepository;
|
||||
private JobRepository|MockObject $jobRepository;
|
||||
private \CodeRhapsodie\DataflowBundle\Manager\ScheduledDataflowManager $manager;
|
||||
|
||||
private \Doctrine\DBAL\Connection|\PHPUnit\Framework\MockObject\MockObject $connection;
|
||||
|
||||
private \CodeRhapsodie\DataflowBundle\Repository\ScheduledDataflowRepository|\PHPUnit\Framework\MockObject\MockObject $scheduledDataflowRepository;
|
||||
|
||||
private \CodeRhapsodie\DataflowBundle\Repository\JobRepository|\PHPUnit\Framework\MockObject\MockObject $jobRepository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
@@ -45,20 +50,10 @@ class ScheduledDataflowManagerTest extends TestCase
|
||||
->willReturn([$scheduled1, $scheduled2])
|
||||
;
|
||||
|
||||
$matcher = $this->exactly(2);
|
||||
$this->jobRepository
|
||||
->expects($matcher)
|
||||
->expects($this->exactly(2))
|
||||
->method('findPendingForScheduledDataflow')
|
||||
->with($this->callback(function ($arg) use ($matcher, $scheduled1, $scheduled2) {
|
||||
switch ($matcher->numberOfInvocations()) {
|
||||
case 1:
|
||||
return $arg === $scheduled1;
|
||||
case 2:
|
||||
return $arg === $scheduled2;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}))
|
||||
->withConsecutive([$scheduled1], [$scheduled2])
|
||||
->willReturnOnConsecutiveCalls(new Job(), null)
|
||||
;
|
||||
|
||||
@@ -108,7 +103,7 @@ class ScheduledDataflowManagerTest extends TestCase
|
||||
$this->jobRepository
|
||||
->expects($this->exactly(1))
|
||||
->method('findPendingForScheduledDataflow')
|
||||
->with($scheduled1)
|
||||
->withConsecutive([$scheduled1])
|
||||
->willThrowException(new \Exception())
|
||||
;
|
||||
|
||||
|
||||
@@ -14,9 +14,11 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
class JobMessageHandlerTest extends TestCase
|
||||
{
|
||||
private JobRepository|MockObject $repository;
|
||||
private JobProcessorInterface|MockObject $processor;
|
||||
private JobMessageHandler $handler;
|
||||
private \CodeRhapsodie\DataflowBundle\Repository\JobRepository|\PHPUnit\Framework\MockObject\MockObject $repository;
|
||||
|
||||
private \CodeRhapsodie\DataflowBundle\Processor\JobProcessorInterface|\PHPUnit\Framework\MockObject\MockObject $processor;
|
||||
|
||||
private \CodeRhapsodie\DataflowBundle\MessengerMode\JobMessageHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
@@ -26,6 +28,11 @@ class JobMessageHandlerTest extends TestCase
|
||||
$this->handler = new JobMessageHandler($this->repository, $this->processor);
|
||||
}
|
||||
|
||||
public function testGetHandledMessages()
|
||||
{
|
||||
$this->assertSame([JobMessage::class], JobMessageHandler::getHandledMessages());
|
||||
}
|
||||
|
||||
public function testInvoke()
|
||||
{
|
||||
$message = new JobMessage($id = 32);
|
||||
|
||||
@@ -16,10 +16,13 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
class JobProcessorTest extends TestCase
|
||||
{
|
||||
private JobProcessor $processor;
|
||||
private JobRepository|MockObject $repository;
|
||||
private DataflowTypeRegistryInterface|MockObject $registry;
|
||||
private EventDispatcherInterface|MockObject $dispatcher;
|
||||
private \CodeRhapsodie\DataflowBundle\Processor\JobProcessor $processor;
|
||||
|
||||
private \CodeRhapsodie\DataflowBundle\Repository\JobRepository|\PHPUnit\Framework\MockObject\MockObject $repository;
|
||||
|
||||
private \CodeRhapsodie\DataflowBundle\Registry\DataflowTypeRegistryInterface|\PHPUnit\Framework\MockObject\MockObject $registry;
|
||||
|
||||
private \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit\Framework\MockObject\MockObject $dispatcher;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
@@ -39,25 +42,36 @@ class JobProcessorTest extends TestCase
|
||||
->setOptions($options = ['option1' => 'value1'])
|
||||
;
|
||||
|
||||
$matcher = $this->exactly(2);
|
||||
$this->dispatcher
|
||||
->expects($matcher)
|
||||
->method('dispatch')
|
||||
->with(
|
||||
$this->callback(function ($arg) use ($job) {
|
||||
return $arg instanceof ProcessingEvent && $arg->getJob() === $job;
|
||||
}),
|
||||
$this->callback(function ($arg) use ($matcher) {
|
||||
switch ($matcher->numberOfInvocations()) {
|
||||
case 1:
|
||||
return $arg === Events::BEFORE_PROCESSING;
|
||||
case 2:
|
||||
return $arg === Events::AFTER_PROCESSING;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
})
|
||||
);
|
||||
// Symfony 3.4 to 4.4 call
|
||||
if (!class_exists(\Symfony\Contracts\EventDispatcher\Event::class)) {
|
||||
$this->dispatcher
|
||||
->expects($this->exactly(2))
|
||||
->method('dispatch')
|
||||
->withConsecutive(
|
||||
[
|
||||
Events::BEFORE_PROCESSING,
|
||||
$this->callback(fn(ProcessingEvent $event) => $event->getJob() === $job)
|
||||
],
|
||||
[
|
||||
Events::AFTER_PROCESSING,
|
||||
$this->callback(fn(ProcessingEvent $event) => $event->getJob() === $job)
|
||||
],
|
||||
);
|
||||
} else { // Symfony 5.0+
|
||||
$this->dispatcher
|
||||
->expects($this->exactly(2))
|
||||
->method('dispatch')
|
||||
->withConsecutive(
|
||||
[
|
||||
$this->callback(fn(ProcessingEvent $event) => $event->getJob() === $job),
|
||||
Events::BEFORE_PROCESSING,
|
||||
],
|
||||
[
|
||||
$this->callback(fn(ProcessingEvent $event) => $event->getJob() === $job),
|
||||
Events::AFTER_PROCESSING,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
$dataflowType = $this->createMock(DataflowTypeInterface::class);
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DataflowTypeRegistryTest extends TestCase
|
||||
{
|
||||
private DataflowTypeRegistry $registry;
|
||||
private \CodeRhapsodie\DataflowBundle\Registry\DataflowTypeRegistry $registry;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
|
||||
@@ -13,9 +13,11 @@ use Symfony\Component\Messenger\MessageBusInterface;
|
||||
|
||||
class MessengerDataflowRunnerTest extends TestCase
|
||||
{
|
||||
private MessengerDataflowRunner $runner;
|
||||
private JobRepository|MockObject $repository;
|
||||
private MessageBusInterface|MockObject $bus;
|
||||
private \CodeRhapsodie\DataflowBundle\Runner\MessengerDataflowRunner $runner;
|
||||
|
||||
private \CodeRhapsodie\DataflowBundle\Repository\JobRepository|\PHPUnit\Framework\MockObject\MockObject $repository;
|
||||
|
||||
private \Symfony\Component\Messenger\MessageBusInterface|\PHPUnit\Framework\MockObject\MockObject $bus;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
@@ -35,36 +37,20 @@ class MessengerDataflowRunnerTest extends TestCase
|
||||
->method('findNextPendingDataflow')
|
||||
->willReturnOnConsecutiveCalls($job1, $job2, null)
|
||||
;
|
||||
$matcher = $this->exactly(2);
|
||||
$this->repository
|
||||
->expects($matcher)
|
||||
->expects($this->exactly(2))
|
||||
->method('save')
|
||||
->with($this->callback(function ($arg) use ($matcher, $job1, $job2) {
|
||||
switch ($matcher->numberOfInvocations()) {
|
||||
case 1:
|
||||
return $arg === $job1;
|
||||
case 2:
|
||||
return $arg === $job2;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}))
|
||||
->withConsecutive([$job1], [$job2])
|
||||
;
|
||||
|
||||
$matcher = $this->exactly(2);
|
||||
$this->bus
|
||||
->expects($matcher)
|
||||
->expects($this->exactly(2))
|
||||
->method('dispatch')
|
||||
->with($this->callback(function ($arg) use ($matcher, $id1, $id2) {
|
||||
switch ($matcher->numberOfInvocations()) {
|
||||
case 1:
|
||||
return $arg instanceof JobMessage && $arg->getJobId() === $id1;
|
||||
case 2:
|
||||
return $arg instanceof JobMessage && $arg->getJobId() === $id2;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}))
|
||||
->withConsecutive([
|
||||
$this->callback(fn($message) => $message instanceof JobMessage && $message->getJobId() === $id1)
|
||||
], [
|
||||
$this->callback(fn($message) => $message instanceof JobMessage && $message->getJobId() === $id2)
|
||||
])
|
||||
->willReturnOnConsecutiveCalls(
|
||||
new Envelope(new JobMessage($id1)),
|
||||
new Envelope(new JobMessage($id2))
|
||||
|
||||
@@ -11,9 +11,11 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PendingDataflowRunnerTest extends TestCase
|
||||
{
|
||||
private PendingDataflowRunner $runner;
|
||||
private JobRepository|MockObject $repository;
|
||||
private JobProcessorInterface|MockObject $processor;
|
||||
private \CodeRhapsodie\DataflowBundle\Runner\PendingDataflowRunner $runner;
|
||||
|
||||
private \CodeRhapsodie\DataflowBundle\Repository\JobRepository|\PHPUnit\Framework\MockObject\MockObject $repository;
|
||||
|
||||
private \CodeRhapsodie\DataflowBundle\Processor\JobProcessorInterface|\PHPUnit\Framework\MockObject\MockObject $processor;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
@@ -34,20 +36,10 @@ class PendingDataflowRunnerTest extends TestCase
|
||||
->willReturnOnConsecutiveCalls($job1, $job2, null)
|
||||
;
|
||||
|
||||
$matcher = $this->exactly(2);
|
||||
$this->processor
|
||||
->expects($matcher)
|
||||
->expects($this->exactly(2))
|
||||
->method('process')
|
||||
->with($this->callback(function ($arg) use ($matcher, $job1, $job2) {
|
||||
switch ($matcher->numberOfInvocations()) {
|
||||
case 1:
|
||||
return $arg === $job1;
|
||||
case 2:
|
||||
return $arg === $job2;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}))
|
||||
->withConsecutive([$job1], [$job2])
|
||||
;
|
||||
|
||||
$this->runner->runPendingDataflows();
|
||||
|
||||
@@ -4,18 +4,18 @@ namespace CodeRhapsodie\DataflowBundle\Tests\Validator\Constraints;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Validator\Constraints\Frequency;
|
||||
use CodeRhapsodie\DataflowBundle\Validator\Constraints\FrequencyValidator;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use Symfony\Component\Validator\ConstraintValidatorInterface;
|
||||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
||||
|
||||
class FrequencyValidatorTest extends ConstraintValidatorTestCase
|
||||
{
|
||||
protected function createValidator(): ConstraintValidatorInterface
|
||||
protected function createValidator()
|
||||
{
|
||||
return new FrequencyValidator();
|
||||
}
|
||||
|
||||
#[DataProvider('getValidValues')]
|
||||
/**
|
||||
* @dataProvider getValidValues
|
||||
*/
|
||||
public function testValidValues($value)
|
||||
{
|
||||
$this->validator->validate($value, new Frequency());
|
||||
@@ -23,7 +23,7 @@ class FrequencyValidatorTest extends ConstraintValidatorTestCase
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public static function getValidValues()
|
||||
public function getValidValues()
|
||||
{
|
||||
return [
|
||||
['3 days'],
|
||||
@@ -47,7 +47,9 @@ class FrequencyValidatorTest extends ConstraintValidatorTestCase
|
||||
;
|
||||
}
|
||||
|
||||
#[DataProvider('getNegativeValues')]
|
||||
/**
|
||||
* @dataProvider getNegativeValues
|
||||
*/
|
||||
public function testNegativeIntervals($value)
|
||||
{
|
||||
$constraint = new Frequency([
|
||||
@@ -62,7 +64,7 @@ class FrequencyValidatorTest extends ConstraintValidatorTestCase
|
||||
;
|
||||
}
|
||||
|
||||
public static function getNegativeValues()
|
||||
public function getNegativeValues()
|
||||
{
|
||||
return [
|
||||
['now'],
|
||||
|
||||
@@ -43,27 +43,26 @@
|
||||
"require": {
|
||||
"php": "^8.0",
|
||||
"ext-json": "*",
|
||||
"doctrine/dbal": "^3.0||^4.0",
|
||||
"doctrine/doctrine-bundle": "^2.0",
|
||||
"monolog/monolog": "^2.0||^3.0",
|
||||
"doctrine/dbal": "^2.12||^3.0",
|
||||
"doctrine/doctrine-bundle": "^1.0||^2.0",
|
||||
"monolog/monolog": "^1.0||^2.0",
|
||||
"psr/log": "^1.1||^2.0||^3.0",
|
||||
"symfony/config": "^7.0",
|
||||
"symfony/console": "^7.0",
|
||||
"symfony/dependency-injection": "^7.0",
|
||||
"symfony/event-dispatcher": "^7.0",
|
||||
"symfony/http-kernel": "^7.0",
|
||||
"symfony/lock": "^7.0",
|
||||
"symfony/monolog-bridge": "^7.0",
|
||||
"symfony/options-resolver": "^7.0",
|
||||
"symfony/validator": "^7.0",
|
||||
"symfony/yaml": "^7.0"
|
||||
"symfony/config": "^3.4||^4.0||^5.0||^6.0",
|
||||
"symfony/console": "^3.4||^4.0||^5.0||^6.0",
|
||||
"symfony/dependency-injection": "^3.4||>=4.1.12||^5.0||^6.0",
|
||||
"symfony/event-dispatcher": "^3.4||^4.0||^5.0||^6.0",
|
||||
"symfony/http-kernel": "^3.4||^4.0||^5.0||^6.0",
|
||||
"symfony/lock": "^3.4||^4.0||^5.0||^6.0",
|
||||
"symfony/monolog-bridge": "^3.4||^4.0||^5.0||^6.0",
|
||||
"symfony/options-resolver": "^3.4||^4.0||^5.0||^6.0",
|
||||
"symfony/validator": "^3.4||^4.0||^5.0||^6.0",
|
||||
"symfony/yaml": "^3.4||^4.0||^5.0||^6.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"amphp/amp": "^2.5",
|
||||
"phpunit/phpunit": "^11",
|
||||
"portphp/portphp": "^1.9",
|
||||
"rector/rector": "^1.0",
|
||||
"symfony/messenger": "^7.0"
|
||||
"phpunit/phpunit": "^7||^8||^9",
|
||||
"rector/rector": "^0.13.10",
|
||||
"symfony/messenger": "^4.4||^5.0||^6.0"
|
||||
},
|
||||
"suggest": {
|
||||
"amphp/amp": "Provide asynchronous steps for your dataflows",
|
||||
|
||||
20
phpunit.xml
20
phpunit.xml
@@ -1,6 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="Tests/bootstrap.php" colors="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.1/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="Tests/bootstrap.php" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" colors="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
|
||||
<coverage>
|
||||
<include>
|
||||
<directory>./src/</directory>
|
||||
</include>
|
||||
<exclude>
|
||||
<directory>Tests/</directory>
|
||||
<directory>vendor/</directory>
|
||||
</exclude>
|
||||
</coverage>
|
||||
<php>
|
||||
<ini name="error_reporting" value="-1"/>
|
||||
</php>
|
||||
@@ -9,13 +18,4 @@
|
||||
<directory suffix="Test.php">./Tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<source>
|
||||
<include>
|
||||
<directory>./src/</directory>
|
||||
</include>
|
||||
<exclude>
|
||||
<directory>Tests/</directory>
|
||||
<directory>vendor/</directory>
|
||||
</exclude>
|
||||
</source>
|
||||
</phpunit>
|
||||
|
||||
@@ -17,7 +17,7 @@ use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||
*/
|
||||
class CodeRhapsodieDataflowBundle extends Bundle
|
||||
{
|
||||
protected string $name = 'CodeRhapsodieDataflowBundle';
|
||||
protected $name = 'CodeRhapsodieDataflowBundle';
|
||||
|
||||
public function getContainerExtension(): ?ExtensionInterface
|
||||
{
|
||||
|
||||
@@ -8,7 +8,6 @@ use CodeRhapsodie\DataflowBundle\Entity\ScheduledDataflow;
|
||||
use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
|
||||
use CodeRhapsodie\DataflowBundle\Registry\DataflowTypeRegistryInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\ScheduledDataflowRepository;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
@@ -19,9 +18,10 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
#[AsCommand('code-rhapsodie:dataflow:schedule:add', 'Create a scheduled dataflow')]
|
||||
class AddScheduledDataflowCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'code-rhapsodie:dataflow:schedule:add';
|
||||
|
||||
public function __construct(private DataflowTypeRegistryInterface $registry, private ScheduledDataflowRepository $scheduledDataflowRepository, private ValidatorInterface $validator, private ConnectionFactory $connectionFactory)
|
||||
{
|
||||
parent::__construct();
|
||||
@@ -33,6 +33,7 @@ class AddScheduledDataflowCommand extends Command
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setDescription('Create a scheduled dataflow')
|
||||
->setHelp('The <info>%command.name%</info> allows you to create a new scheduled dataflow.')
|
||||
->addOption('label', null, InputOption::VALUE_REQUIRED, 'Label of the scheduled dataflow')
|
||||
->addOption('type', null, InputOption::VALUE_REQUIRED, 'Type of the scheduled dataflow (FQCN)')
|
||||
@@ -94,7 +95,7 @@ class AddScheduledDataflowCommand extends Command
|
||||
'dataflow_type' => $type,
|
||||
'options' => json_decode($options, true, 512, JSON_THROW_ON_ERROR),
|
||||
'frequency' => $frequency,
|
||||
'next' => new \DateTime($firstRun),
|
||||
'next' => new \DateTimeImmutable($firstRun),
|
||||
'enabled' => $enabled,
|
||||
]);
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ namespace CodeRhapsodie\DataflowBundle\Command;
|
||||
use CodeRhapsodie\DataflowBundle\Entity\ScheduledDataflow;
|
||||
use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\ScheduledDataflowRepository;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
@@ -18,9 +17,10 @@ use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
#[AsCommand('code-rhapsodie:dataflow:schedule:change-status', 'Change schedule status')]
|
||||
class ChangeScheduleStatusCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'code-rhapsodie:dataflow:schedule:change-status';
|
||||
|
||||
public function __construct(private ScheduledDataflowRepository $scheduledDataflowRepository, private ConnectionFactory $connectionFactory)
|
||||
{
|
||||
parent::__construct();
|
||||
@@ -32,6 +32,7 @@ class ChangeScheduleStatusCommand extends Command
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setDescription('Change schedule status')
|
||||
->setHelp('The <info>%command.name%</info> command able you to change schedule status.')
|
||||
->addArgument('schedule-id', InputArgument::REQUIRED, 'Id of the schedule')
|
||||
->addOption('enable', null, InputOption::VALUE_NONE, 'Enable the schedule')
|
||||
|
||||
@@ -8,7 +8,6 @@ use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
|
||||
use CodeRhapsodie\DataflowBundle\Registry\DataflowTypeRegistryInterface;
|
||||
use Psr\Log\LoggerAwareInterface;
|
||||
use Psr\Log\LoggerAwareTrait;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
@@ -21,11 +20,12 @@ use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
#[AsCommand('code-rhapsodie:dataflow:execute', 'Runs one dataflow type with provided options')]
|
||||
class ExecuteDataflowCommand extends Command implements LoggerAwareInterface
|
||||
{
|
||||
use LoggerAwareTrait;
|
||||
|
||||
protected static $defaultName = 'code-rhapsodie:dataflow:execute';
|
||||
|
||||
public function __construct(private DataflowTypeRegistryInterface $registry, private ConnectionFactory $connectionFactory)
|
||||
{
|
||||
parent::__construct();
|
||||
@@ -37,6 +37,7 @@ class ExecuteDataflowCommand extends Command implements LoggerAwareInterface
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setDescription('Runs one dataflow type with provided options')
|
||||
->setHelp(<<<'EOF'
|
||||
The <info>%command.name%</info> command runs one dataflow with the provided options.
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ namespace CodeRhapsodie\DataflowBundle\Command;
|
||||
use CodeRhapsodie\DataflowBundle\Entity\Job;
|
||||
use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
@@ -17,7 +16,6 @@ use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
#[AsCommand('code-rhapsodie:dataflow:job:show', 'Display job details for schedule or specific job')]
|
||||
class JobShowCommand extends Command
|
||||
{
|
||||
private const STATUS_MAPPING = [
|
||||
@@ -26,6 +24,8 @@ class JobShowCommand extends Command
|
||||
Job::STATUS_COMPLETED => 'Completed',
|
||||
];
|
||||
|
||||
protected static $defaultName = 'code-rhapsodie:dataflow:job:show';
|
||||
|
||||
public function __construct(private JobRepository $jobRepository, private ConnectionFactory $connectionFactory)
|
||||
{
|
||||
parent::__construct();
|
||||
@@ -37,6 +37,7 @@ class JobShowCommand extends Command
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setDescription('Display job details for schedule or specific job')
|
||||
->setHelp('The <info>%command.name%</info> display job details for schedule or specific job.')
|
||||
->addOption('job-id', null, InputOption::VALUE_REQUIRED, 'Id of the job to get details')
|
||||
->addOption('schedule-id', null, InputOption::VALUE_REQUIRED, 'Id of schedule for last execution details')
|
||||
|
||||
@@ -7,7 +7,6 @@ namespace CodeRhapsodie\DataflowBundle\Command;
|
||||
use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
|
||||
use CodeRhapsodie\DataflowBundle\Manager\ScheduledDataflowManagerInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Runner\PendingDataflowRunnerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Command\LockableTrait;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
@@ -19,11 +18,12 @@ use Symfony\Component\Console\Output\OutputInterface;
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
#[AsCommand('code-rhapsodie:dataflow:run-pending', 'Runs dataflows based on the scheduled defined in the UI.')]
|
||||
class RunPendingDataflowsCommand extends Command
|
||||
{
|
||||
use LockableTrait;
|
||||
|
||||
protected static $defaultName = 'code-rhapsodie:dataflow:run-pending';
|
||||
|
||||
public function __construct(private ScheduledDataflowManagerInterface $manager, private PendingDataflowRunnerInterface $runner, private ConnectionFactory $connectionFactory)
|
||||
{
|
||||
parent::__construct();
|
||||
@@ -35,6 +35,7 @@ class RunPendingDataflowsCommand extends Command
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setDescription('Runs dataflows based on the scheduled defined in the UI.')
|
||||
->setHelp(<<<'EOF'
|
||||
The <info>%command.name%</info> command runs dataflows according to the schedule defined in the UI by the user.
|
||||
EOF
|
||||
|
||||
@@ -6,7 +6,6 @@ namespace CodeRhapsodie\DataflowBundle\Command;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\ScheduledDataflowRepository;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
@@ -16,9 +15,10 @@ use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
#[AsCommand('code-rhapsodie:dataflow:schedule:list', 'List scheduled dataflows')]
|
||||
class ScheduleListCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'code-rhapsodie:dataflow:schedule:list';
|
||||
|
||||
public function __construct(private ScheduledDataflowRepository $scheduledDataflowRepository, private ConnectionFactory $connectionFactory)
|
||||
{
|
||||
parent::__construct();
|
||||
@@ -30,6 +30,7 @@ class ScheduleListCommand extends Command
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setDescription('List scheduled dataflows')
|
||||
->setHelp('The <info>%command.name%</info> lists all scheduled dataflows.')
|
||||
->addOption('connection', null, InputOption::VALUE_REQUIRED, 'Define the DBAL connection to use');
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ use CodeRhapsodie\DataflowBundle\Repository\ScheduledDataflowRepository;
|
||||
use CodeRhapsodie\DataflowBundle\SchemaProvider\DataflowSchemaProvider;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\DBAL\Schema\Table;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
@@ -20,9 +19,10 @@ use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
#[AsCommand('code-rhapsodie:dataflow:dump-schema', 'Generates schema create / update SQL queries')]
|
||||
class SchemaCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'code-rhapsodie:dataflow:dump-schema';
|
||||
|
||||
public function __construct(private ConnectionFactory $connectionFactory)
|
||||
{
|
||||
parent::__construct();
|
||||
@@ -34,6 +34,7 @@ class SchemaCommand extends Command
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setDescription('Generates schema create / update SQL queries')
|
||||
->setHelp('The <info>%command.name%</info> help you to generate SQL Query to create or update your database schema for this bundle')
|
||||
->addOption('update', null, InputOption::VALUE_NONE, 'Dump only the update SQL queries.')
|
||||
->addOption('connection', null, InputOption::VALUE_REQUIRED, 'Define the DBAL connection to use')
|
||||
|
||||
@@ -11,17 +11,19 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class AMPAsyncDataflowBuilder extends DataflowBuilder
|
||||
{
|
||||
public function __construct(protected ?int $loopInterval = 0, protected ?int $emitInterval = 0)
|
||||
{
|
||||
}
|
||||
|
||||
private ?string $name = null;
|
||||
|
||||
private ?iterable $reader = null;
|
||||
|
||||
private array $steps = [];
|
||||
|
||||
/** @var WriterInterface[] */
|
||||
private array $writers = [];
|
||||
|
||||
public function __construct(protected ?int $loopInterval = 0, protected ?int $emitInterval = 0)
|
||||
{
|
||||
}
|
||||
|
||||
public function setName(string $name): self
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
@@ -68,7 +68,7 @@ class AMPAsyncDataflow implements DataflowInterface, LoggerAwareInterface
|
||||
{
|
||||
$count = 0;
|
||||
$exceptions = [];
|
||||
$startTime = new \DateTime();
|
||||
$startTime = new \DateTimeImmutable();
|
||||
|
||||
try {
|
||||
foreach ($this->writers as $writer) {
|
||||
@@ -110,7 +110,7 @@ class AMPAsyncDataflow implements DataflowInterface, LoggerAwareInterface
|
||||
$this->logException($e);
|
||||
}
|
||||
|
||||
return new Result($this->name, $startTime, new \DateTime(), $count, $exceptions);
|
||||
return new Result($this->name, $startTime, new \DateTimeImmutable(), $count, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -62,7 +62,7 @@ class Dataflow implements DataflowInterface, LoggerAwareInterface
|
||||
{
|
||||
$count = 0;
|
||||
$exceptions = [];
|
||||
$startTime = new \DateTime();
|
||||
$startTime = new \DateTimeImmutable();
|
||||
|
||||
try {
|
||||
foreach ($this->writers as $writer) {
|
||||
@@ -97,7 +97,7 @@ class Dataflow implements DataflowInterface, LoggerAwareInterface
|
||||
$this->logException($e);
|
||||
}
|
||||
|
||||
return new Result($this->name, $startTime, new \DateTime(), $count, $exceptions);
|
||||
return new Result($this->name, $startTime, new \DateTimeImmutable(), $count, $exceptions);
|
||||
}
|
||||
|
||||
private function processItem(mixed $item): void
|
||||
|
||||
@@ -11,7 +11,9 @@ use CodeRhapsodie\DataflowBundle\DataflowType\Writer\WriterInterface;
|
||||
class DataflowBuilder
|
||||
{
|
||||
private ?string $name = null;
|
||||
|
||||
private ?iterable $reader = null;
|
||||
|
||||
private array $steps = [];
|
||||
|
||||
/** @var WriterInterface[] */
|
||||
|
||||
@@ -13,7 +13,12 @@ class Configuration implements ConfigurationInterface
|
||||
public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Builder\TreeBuilder
|
||||
{
|
||||
$treeBuilder = new TreeBuilder('code_rhapsodie_dataflow');
|
||||
$rootNode = $treeBuilder->getRootNode();
|
||||
if (method_exists($treeBuilder, 'getRootNode')) {
|
||||
$rootNode = $treeBuilder->getRootNode();
|
||||
} else {
|
||||
// BC for symfony/config < 4.2
|
||||
$rootNode = $treeBuilder->root('code_rhapsodie_dataflow');
|
||||
}
|
||||
|
||||
$rootNode
|
||||
->children()
|
||||
|
||||
@@ -72,6 +72,10 @@ class Job
|
||||
->setScheduledDataflowId($scheduled->getId());
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public static function createFromArray(array $datas)
|
||||
{
|
||||
$lost = array_diff(static::KEYS, array_keys($datas));
|
||||
|
||||
@@ -4,11 +4,17 @@ declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Event;
|
||||
|
||||
use Symfony\Contracts\EventDispatcher\Event;
|
||||
|
||||
/**
|
||||
/*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
abstract class CrEvent extends Event
|
||||
{
|
||||
if (class_exists(\Symfony\Contracts\EventDispatcher\Event::class)) {
|
||||
// For Symfony 5.0+
|
||||
abstract class CrEvent extends \Symfony\Contracts\EventDispatcher\Event
|
||||
{
|
||||
}
|
||||
} else {
|
||||
// For Symfony 3.4 to 4.4
|
||||
abstract class CrEvent extends \Symfony\Component\EventDispatcher\Event
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ use Monolog\Formatter\FormatterInterface;
|
||||
use Monolog\Formatter\LineFormatter;
|
||||
use Monolog\Handler\AbstractProcessingHandler;
|
||||
use Monolog\Logger;
|
||||
use Monolog\LogRecord;
|
||||
|
||||
class BufferHandler extends AbstractProcessingHandler
|
||||
{
|
||||
@@ -16,6 +15,11 @@ class BufferHandler extends AbstractProcessingHandler
|
||||
|
||||
private array $buffer = [];
|
||||
|
||||
public function __construct($level = Logger::DEBUG, bool $bubble = true)
|
||||
{
|
||||
parent::__construct($level, $bubble);
|
||||
}
|
||||
|
||||
public function clearBuffer(): array
|
||||
{
|
||||
$logs = $this->buffer;
|
||||
@@ -24,7 +28,7 @@ class BufferHandler extends AbstractProcessingHandler
|
||||
return $logs;
|
||||
}
|
||||
|
||||
protected function write(array|LogRecord $record): void
|
||||
protected function write(array $record): void
|
||||
{
|
||||
$this->buffer[] = $record['formatted'];
|
||||
}
|
||||
|
||||
@@ -6,10 +6,9 @@ namespace CodeRhapsodie\DataflowBundle\MessengerMode;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Processor\JobProcessorInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
|
||||
|
||||
#[AsMessageHandler]
|
||||
class JobMessageHandler
|
||||
class JobMessageHandler implements MessageSubscriberInterface
|
||||
{
|
||||
public function __construct(private JobRepository $repository, private JobProcessorInterface $processor)
|
||||
{
|
||||
@@ -19,4 +18,9 @@ class JobMessageHandler
|
||||
{
|
||||
$this->processor->process($this->repository->find($message->getJobId()));
|
||||
}
|
||||
|
||||
public static function getHandledMessages(): iterable
|
||||
{
|
||||
return [JobMessage::class];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,12 @@ class JobProcessor implements JobProcessorInterface, LoggerAwareInterface
|
||||
|
||||
private function beforeProcessing(Job $job): void
|
||||
{
|
||||
$this->dispatcher->dispatch(new ProcessingEvent($job), Events::BEFORE_PROCESSING);
|
||||
// Symfony 3.4 to 4.4 call
|
||||
if (!class_exists(\Symfony\Contracts\EventDispatcher\Event::class)) {
|
||||
$this->dispatcher->dispatch(Events::BEFORE_PROCESSING, new ProcessingEvent($job));
|
||||
} else { // Symfony 5.0+ call
|
||||
$this->dispatcher->dispatch(new ProcessingEvent($job), Events::BEFORE_PROCESSING);
|
||||
}
|
||||
|
||||
$job
|
||||
->setStatus(Job::STATUS_RUNNING)
|
||||
@@ -72,6 +77,11 @@ class JobProcessor implements JobProcessorInterface, LoggerAwareInterface
|
||||
;
|
||||
$this->repository->save($job);
|
||||
|
||||
$this->dispatcher->dispatch(new ProcessingEvent($job), Events::AFTER_PROCESSING);
|
||||
// Symfony 3.4 to 4.4 call
|
||||
if (!class_exists(\Symfony\Contracts\EventDispatcher\Event::class)) {
|
||||
$this->dispatcher->dispatch(Events::AFTER_PROCESSING, new ProcessingEvent($job));
|
||||
} else { // Symfony 5.0+ call
|
||||
$this->dispatcher->dispatch(new ProcessingEvent($job), Events::AFTER_PROCESSING);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,9 @@ namespace CodeRhapsodie\DataflowBundle\Repository;
|
||||
*/
|
||||
trait InitFromDbTrait
|
||||
{
|
||||
abstract private function getFields(): array;
|
||||
|
||||
private function initDateTime(array $datas): array
|
||||
{
|
||||
foreach ($this->getFields() as $key => $type) {
|
||||
foreach (static::FIELDS_TYPE as $key => $type) {
|
||||
if ('datetime' === $type && null !== $datas[$key]) {
|
||||
$datas[$key] = new \DateTime($datas[$key]);
|
||||
}
|
||||
|
||||
@@ -21,6 +21,20 @@ class JobRepository
|
||||
|
||||
public const TABLE_NAME = 'cr_dataflow_job';
|
||||
|
||||
private const FIELDS_TYPE = [
|
||||
'id' => ParameterType::INTEGER,
|
||||
'status' => ParameterType::INTEGER,
|
||||
'label' => ParameterType::STRING,
|
||||
'dataflow_type' => ParameterType::STRING,
|
||||
'options' => ParameterType::STRING,
|
||||
'requested_date' => 'datetime',
|
||||
'scheduled_dataflow_id' => ParameterType::INTEGER,
|
||||
'count' => ParameterType::INTEGER,
|
||||
'exceptions' => ParameterType::STRING,
|
||||
'start_time' => 'datetime',
|
||||
'end_time' => 'datetime',
|
||||
];
|
||||
|
||||
/**
|
||||
* JobRepository constructor.
|
||||
*/
|
||||
@@ -44,7 +58,7 @@ class JobRepository
|
||||
$qb
|
||||
->andWhere($qb->expr()->isNull('scheduled_dataflow_id'))
|
||||
->andWhere($qb->expr()->eq('status', $qb->createNamedParameter(Job::STATUS_PENDING, ParameterType::INTEGER)));
|
||||
$stmt = $qb->executeQuery();
|
||||
$stmt = $qb->execute();
|
||||
if (0 === $stmt->rowCount()) {
|
||||
return [];
|
||||
}
|
||||
@@ -92,7 +106,7 @@ class JobRepository
|
||||
$qb
|
||||
->orderBy('requested_date', 'DESC')
|
||||
->setMaxResults(20);
|
||||
$stmt = $qb->executeQuery();
|
||||
$stmt = $qb->execute();
|
||||
if (0 === $stmt->rowCount()) {
|
||||
return [];
|
||||
}
|
||||
@@ -107,7 +121,7 @@ class JobRepository
|
||||
$qb->andWhere($qb->expr()->eq('scheduled_dataflow_id', $qb->createNamedParameter($id, ParameterType::INTEGER)))
|
||||
->orderBy('requested_date', 'DESC')
|
||||
->setMaxResults(20);
|
||||
$stmt = $qb->executeQuery();
|
||||
$stmt = $qb->execute();
|
||||
if (0 === $stmt->rowCount()) {
|
||||
return [];
|
||||
}
|
||||
@@ -129,12 +143,12 @@ class JobRepository
|
||||
}
|
||||
|
||||
if (null === $job->getId()) {
|
||||
$this->connection->insert(static::TABLE_NAME, $datas, $this->getFields());
|
||||
$this->connection->insert(static::TABLE_NAME, $datas, static::FIELDS_TYPE);
|
||||
$job->setId((int) $this->connection->lastInsertId());
|
||||
|
||||
return;
|
||||
}
|
||||
$this->connection->update(static::TABLE_NAME, $datas, ['id' => $job->getId()], $this->getFields());
|
||||
$this->connection->update(static::TABLE_NAME, $datas, ['id' => $job->getId()], static::FIELDS_TYPE);
|
||||
}
|
||||
|
||||
public function createQueryBuilder($alias = null): QueryBuilder
|
||||
@@ -148,28 +162,11 @@ class JobRepository
|
||||
|
||||
private function returnFirstOrNull(QueryBuilder $qb): ?Job
|
||||
{
|
||||
$stmt = $qb->executeQuery();
|
||||
$stmt = $qb->execute();
|
||||
if (0 === $stmt->rowCount()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Job::createFromArray($this->initDateTime($this->initArray($stmt->fetchAssociative())));
|
||||
}
|
||||
|
||||
private function getFields(): array
|
||||
{
|
||||
return [
|
||||
'id' => ParameterType::INTEGER,
|
||||
'status' => ParameterType::INTEGER,
|
||||
'label' => ParameterType::STRING,
|
||||
'dataflow_type' => ParameterType::STRING,
|
||||
'options' => ParameterType::STRING,
|
||||
'requested_date' => 'datetime',
|
||||
'scheduled_dataflow_id' => ParameterType::INTEGER,
|
||||
'count' => ParameterType::INTEGER,
|
||||
'exceptions' => ParameterType::STRING,
|
||||
'start_time' => 'datetime',
|
||||
'end_time' => 'datetime',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,16 @@ class ScheduledDataflowRepository
|
||||
|
||||
public const TABLE_NAME = 'cr_dataflow_scheduled';
|
||||
|
||||
private const FIELDS_TYPE = [
|
||||
'id' => ParameterType::INTEGER,
|
||||
'label' => ParameterType::STRING,
|
||||
'dataflow_type' => ParameterType::STRING,
|
||||
'options' => ParameterType::STRING,
|
||||
'frequency' => ParameterType::STRING,
|
||||
'next' => 'datetime',
|
||||
'enabled' => ParameterType::BOOLEAN,
|
||||
];
|
||||
|
||||
/**
|
||||
* JobRepository constructor.
|
||||
*/
|
||||
@@ -36,11 +46,11 @@ class ScheduledDataflowRepository
|
||||
{
|
||||
$qb = $this->createQueryBuilder();
|
||||
$qb->andWhere($qb->expr()->lte('next', $qb->createNamedParameter(new \DateTime(), 'datetime')))
|
||||
->andWhere($qb->expr()->eq('enabled', $qb->createNamedParameter(1, ParameterType::INTEGER)))
|
||||
->andWhere($qb->expr()->eq('enabled', 1))
|
||||
->orderBy('next', 'ASC')
|
||||
;
|
||||
|
||||
$stmt = $qb->executeQuery();
|
||||
$stmt = $qb->execute();
|
||||
if (0 === $stmt->rowCount()) {
|
||||
return [];
|
||||
}
|
||||
@@ -64,7 +74,7 @@ class ScheduledDataflowRepository
|
||||
$qb = $this->createQueryBuilder();
|
||||
$qb->orderBy('label', 'ASC');
|
||||
|
||||
$stmt = $qb->executeQuery();
|
||||
$stmt = $qb->execute();
|
||||
if (0 === $stmt->rowCount()) {
|
||||
return [];
|
||||
}
|
||||
@@ -82,7 +92,7 @@ class ScheduledDataflowRepository
|
||||
->orderBy('w.label', 'ASC')
|
||||
->groupBy('w.id');
|
||||
|
||||
return $query->executeQuery()->fetchAllAssociative();
|
||||
return $query->execute()->fetchAllAssociative();
|
||||
}
|
||||
|
||||
public function save(ScheduledDataflow $scheduledDataflow)
|
||||
@@ -95,12 +105,12 @@ class ScheduledDataflowRepository
|
||||
}
|
||||
|
||||
if (null === $scheduledDataflow->getId()) {
|
||||
$this->connection->insert(static::TABLE_NAME, $datas, $this->getFields());
|
||||
$this->connection->insert(static::TABLE_NAME, $datas, static::FIELDS_TYPE);
|
||||
$scheduledDataflow->setId((int) $this->connection->lastInsertId());
|
||||
|
||||
return;
|
||||
}
|
||||
$this->connection->update(static::TABLE_NAME, $datas, ['id' => $scheduledDataflow->getId()], $this->getFields());
|
||||
$this->connection->update(static::TABLE_NAME, $datas, ['id' => $scheduledDataflow->getId()], static::FIELDS_TYPE);
|
||||
}
|
||||
|
||||
public function delete(int $id): void
|
||||
@@ -128,24 +138,11 @@ class ScheduledDataflowRepository
|
||||
|
||||
private function returnFirstOrNull(QueryBuilder $qb): ?ScheduledDataflow
|
||||
{
|
||||
$stmt = $qb->executeQuery();
|
||||
$stmt = $qb->execute();
|
||||
if (0 === $stmt->rowCount()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ScheduledDataflow::createFromArray($this->initDateTime($this->initArray($stmt->fetchAssociative())));
|
||||
}
|
||||
|
||||
private function getFields(): array
|
||||
{
|
||||
return [
|
||||
'id' => ParameterType::INTEGER,
|
||||
'label' => ParameterType::STRING,
|
||||
'dataflow_type' => ParameterType::STRING,
|
||||
'options' => ParameterType::STRING,
|
||||
'frequency' => ParameterType::STRING,
|
||||
'next' => 'datetime',
|
||||
'enabled' => ParameterType::BOOLEAN,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,8 @@ class DataflowSchemaProvider
|
||||
$tableSchedule->addColumn('next', 'datetime', ['notnull' => false]);
|
||||
$tableSchedule->addColumn('enabled', 'boolean', ['notnull' => true]);
|
||||
|
||||
$tableJob->addForeignKeyConstraint($tableSchedule->getName(), ['scheduled_dataflow_id'], ['id']);
|
||||
$tableJob->addForeignKeyConstraint($tableSchedule, ['scheduled_dataflow_id'], ['id']);
|
||||
$tableJob->addIndex(['status'], 'idx_status');
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ class FrequencyValidator extends ConstraintValidator
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate(mixed $value, Constraint $constraint)
|
||||
public function validate($value, Constraint $constraint)
|
||||
{
|
||||
if (!$constraint instanceof Frequency) {
|
||||
throw new UnexpectedTypeException($constraint, Frequency::class);
|
||||
@@ -23,12 +23,7 @@ class FrequencyValidator extends ConstraintValidator
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$interval = \DateInterval::createFromDateString($value);
|
||||
} catch (\Exception){
|
||||
$interval = false;
|
||||
}
|
||||
|
||||
$interval = @\DateInterval::createFromDateString($value);
|
||||
if (!$interval) {
|
||||
$this->context->buildViolation($constraint->message)
|
||||
->setParameter('{{ string }}', $value)
|
||||
@@ -47,6 +42,8 @@ class FrequencyValidator extends ConstraintValidator
|
||||
->setParameter('{{ string }}', $value)
|
||||
->addViolation()
|
||||
;
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user