mirror of
https://github.com/code-rhapsodie/dataflow-bundle.git
synced 2026-03-24 14:52:21 +01:00
Compare commits
39 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6ebbdbd63b | ||
|
|
405ce87923 | ||
|
|
0938f7fd1e | ||
|
|
4e82b114c4 | ||
|
|
47af0e226c | ||
|
|
6d86ba16a0 | ||
|
|
95124acc26 | ||
|
|
4011f39510 | ||
|
|
db37c4bdd1 | ||
|
|
f20cd96ec5 | ||
|
|
fd2c6aaab5 | ||
|
|
4efd310a6e | ||
|
|
cec42a3337 | ||
|
|
d440ad008b | ||
|
|
e8b362526a | ||
|
|
3c56a90a93 | ||
|
|
1b2b1be958 | ||
|
|
25b2e9ec0f | ||
|
|
d89d7b72b0 | ||
|
|
3cf05b555d | ||
|
|
9624f68675 | ||
|
|
b191e33c47 | ||
|
|
8bb55b1303 | ||
|
|
f0459462f7 | ||
|
|
5a76c11bc6 | ||
|
|
d7efd85c8e | ||
|
|
b0d17c31cc | ||
|
|
e72d0d5e8d | ||
|
|
a5518c80e2 | ||
|
|
bd9171ad53 | ||
|
|
5b4b3f1b6f | ||
|
|
d1330ae638 | ||
|
|
42b242ee6c | ||
|
|
4d98adfe0a | ||
|
|
c5fc6adf08 | ||
|
|
8efb4bd2d9 | ||
|
|
a9b19d933a | ||
|
|
ca946429b1 | ||
|
|
26ac98eb98 |
@@ -1,3 +0,0 @@
|
||||
service_name : travis-ci
|
||||
coverage_clover: var/build/clover.xml
|
||||
json_path : var/build/upload.json
|
||||
27
.github/workflows/build.yml
vendored
Normal file
27
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
name: Build
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: Build
|
||||
runs-on: ubuntu-latest
|
||||
permissions: read-all
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
|
||||
- uses: sonarsource/sonarqube-scan-action@master
|
||||
env:
|
||||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
||||
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
|
||||
# If you wish to fail your job when the Quality Gate is red, uncomment the
|
||||
# following lines. This would typically be used to fail a deployment.
|
||||
# - uses: sonarsource/sonarqube-quality-gate-action@master
|
||||
# timeout-minutes: 5
|
||||
# env:
|
||||
# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
||||
14
.github/workflows/ci.yml
vendored
Normal file
14
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
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
|
||||
- name: Run PHP CS Fixer
|
||||
run: php vendor/bin/php-cs-fixer fix --dry-run --diff
|
||||
26
.github/workflows/semicolons-kudos.yaml
vendored
Normal file
26
.github/workflows/semicolons-kudos.yaml
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
name: Kudos for Code
|
||||
on:
|
||||
push:
|
||||
branches: ["master"]
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
kudos:
|
||||
name: Semicolons Kudos
|
||||
permissions: write-all
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- uses: LoremLabs/kudos-for-code-action@latest
|
||||
with:
|
||||
search-dir: "."
|
||||
destination: "artifact"
|
||||
generate-nomerges: true
|
||||
generate-validemails: true
|
||||
generate-limitdepth: 0
|
||||
generate-fromrepo: true
|
||||
analyze-repo: false
|
||||
skip-ids: ""
|
||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -3,3 +3,7 @@ composer.lock
|
||||
.phpunit.result.cache
|
||||
.php_cs.cache
|
||||
.php_cs
|
||||
.idea
|
||||
.phpunit.cache
|
||||
.php-version
|
||||
.php-cs-fixer.cache
|
||||
49
.php-cs-fixer.dist.php
Normal file
49
.php-cs-fixer.dist.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
$finder = PhpCsFixer\Finder::create()
|
||||
->in('src')
|
||||
//->in('tests')
|
||||
->files()->name('*.php');
|
||||
|
||||
$config = new PhpCsFixer\Config();
|
||||
$config->setRules([
|
||||
'@Symfony' => true,
|
||||
'@Symfony:risky' => true,
|
||||
'@PSR12' => true,
|
||||
'array_syntax' => [
|
||||
'syntax' => 'short',
|
||||
],
|
||||
'combine_consecutive_unsets' => true,
|
||||
'native_function_invocation' => [
|
||||
'include' => [
|
||||
'@compiler_optimized',
|
||||
],
|
||||
],
|
||||
'no_extra_blank_lines' => [
|
||||
'tokens' => [
|
||||
'break',
|
||||
'continue',
|
||||
'extra',
|
||||
'return',
|
||||
'throw',
|
||||
'use',
|
||||
'parenthesis_brace_block',
|
||||
'square_brace_block',
|
||||
'curly_brace_block',
|
||||
],
|
||||
],
|
||||
'ordered_class_elements' => true,
|
||||
'ordered_imports' => true,
|
||||
'yoda_style' => [
|
||||
'equal' => false,
|
||||
'identical' => false,
|
||||
'less_and_greater' => false,
|
||||
'always_move_variable' => false,
|
||||
],
|
||||
])
|
||||
->setRiskyAllowed(true)
|
||||
->setFinder(
|
||||
$finder
|
||||
);
|
||||
|
||||
return $config;
|
||||
12
.php_cs.dist
12
.php_cs.dist
@@ -1,12 +0,0 @@
|
||||
<?php
|
||||
|
||||
$finder = PhpCsFixer\Finder::create()->in(__DIR__.'/src');
|
||||
|
||||
return PhpCsFixer\Config::create()
|
||||
->setRules([
|
||||
'@Symfony' => true,
|
||||
'declare_strict_types' => true,
|
||||
])
|
||||
->setFinder($finder)
|
||||
->setRiskyAllowed(true)
|
||||
;
|
||||
111
.travis.yml
111
.travis.yml
@@ -1,111 +0,0 @@
|
||||
language: php
|
||||
|
||||
sudo: false
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- $HOME/.composer/cache
|
||||
|
||||
branches:
|
||||
only:
|
||||
- master
|
||||
- /^\d+\.\d+$/
|
||||
- travis-setup
|
||||
|
||||
env:
|
||||
global:
|
||||
- SYMFONY_DEPRECATIONS_HELPER="max[self]=0"
|
||||
- PHPUNIT_FLAGS="-v"
|
||||
- PHPUNIT_ENABLED="true"
|
||||
- STABILITY=stable
|
||||
- COVERALLS_ENABLED="false"
|
||||
|
||||
matrix:
|
||||
fast_finish: true
|
||||
include:
|
||||
- php: '7.1'
|
||||
- php: '7.2'
|
||||
- php: '7.3'
|
||||
|
||||
# Enable code coverage with the latest supported PHP version
|
||||
- php: '7.3'
|
||||
env:
|
||||
- COVERALLS_ENABLED="true"
|
||||
- PHPUNIT_FLAGS="-v --coverage-text --coverage-clover var/build/clover.xml"
|
||||
|
||||
# Minimum supported dependencies with the latest and oldest supported PHP versions
|
||||
- php: '7.1'
|
||||
env:
|
||||
- COMPOSER_FLAGS="--prefer-lowest"
|
||||
- php: '7.3'
|
||||
env:
|
||||
- COMPOSER_FLAGS="--prefer-lowest"
|
||||
|
||||
# Test each supported Symfony version with lowest supported PHP version
|
||||
- php: '7.1'
|
||||
env:
|
||||
- SYMFONY_VERSION=3.4.*
|
||||
- php: '7.1'
|
||||
env:
|
||||
- SYMFONY_VERSION=4.2.*
|
||||
- php: '7.1'
|
||||
env:
|
||||
- SYMFONY_VERSION=4.3.*
|
||||
|
||||
# Test unsupported versions of Symfony
|
||||
- php: '7.1'
|
||||
env:
|
||||
- SYMFONY_VERSION=4.0.*
|
||||
- php: '7.1'
|
||||
env:
|
||||
- SYMFONY_VERSION=4.1.*
|
||||
|
||||
|
||||
# Test upcoming Symfony versions with lowest supported PHP version and dev dependencies
|
||||
- php: '7.1'
|
||||
env:
|
||||
- STABILITY=dev
|
||||
- SYMFONY_VERSION=4.4.*
|
||||
|
||||
# Test upcoming PHP versions with dev dependencies
|
||||
- php: '7.4snapshot'
|
||||
env:
|
||||
- STABILITY=dev
|
||||
- COMPOSER_FLAGS="--ignore-platform-reqs --prefer-stable"
|
||||
|
||||
allow_failures:
|
||||
- env:
|
||||
- SYMFONY_VERSION=4.0.*
|
||||
- env:
|
||||
- SYMFONY_VERSION=4.1.*
|
||||
- env:
|
||||
- STABILITY=dev
|
||||
- COMPOSER_FLAGS="--ignore-platform-reqs --prefer-stable"
|
||||
- env:
|
||||
- STABILITY=dev
|
||||
- SYMFONY_VERSION=4.4.*
|
||||
|
||||
before_install:
|
||||
- if [[ "$SYMFONY_VERSION" != "" ]]; then
|
||||
travis_retry composer global require "symfony/flex:^1.4";
|
||||
composer config extra.symfony.require $SYMFONY_VERSION;
|
||||
fi
|
||||
- if [[ "$STABILITY" != "stable" ]]; then
|
||||
travis_retry composer config minimum-stability $STABILITY;
|
||||
fi
|
||||
- if [[ "$COVERALLS_ENABLED" != "true" ]]; then
|
||||
phpenv config-rm xdebug.ini || true;
|
||||
fi
|
||||
- if [[ "$COVERALLS_ENABLED" == "true" ]]; then
|
||||
travis_retry composer require --dev satooshi/php-coveralls:^2.0 --no-update $COMPOSER_FLAGS;
|
||||
fi
|
||||
|
||||
install:
|
||||
- travis_retry composer update --prefer-dist --no-interaction --no-suggest --no-progress --ansi $COMPOSER_FLAGS
|
||||
|
||||
script: ./vendor/bin/phpunit $PHPUNIT_FLAGS
|
||||
|
||||
after_success:
|
||||
- if [[ "$PHPUNIT_ENABLED" == "true" && "$COVERALLS_ENABLED" == "true" ]]; then
|
||||
./vendor/bin/php-coveralls -vvv --config .coveralls.yml;
|
||||
fi;
|
||||
81
CHANGELOG.md
81
CHANGELOG.md
@@ -1,3 +1,84 @@
|
||||
# Version 5.4.1
|
||||
* Fix File Exceptions integration
|
||||
|
||||
# Version 5.4.0
|
||||
* Add possibility to save exceptions in file
|
||||
|
||||
# Version 5.3.1
|
||||
* Fix interface naming
|
||||
|
||||
# Version 5.3.0
|
||||
* Added auto update count processed item while running job
|
||||
|
||||
# Version 5.2.0
|
||||
* Added custom index for job status
|
||||
|
||||
# Version 5.1.0
|
||||
* Refactor SchemaDump command
|
||||
|
||||
# Version 5.0.1
|
||||
* Fix compatibility with doctrine 4
|
||||
|
||||
# Version 5.0.0
|
||||
* Initiate Kudos on dataflow-bundle
|
||||
* Added Symfony 7 support
|
||||
* Removed Symfony 6 compatibility
|
||||
* Removed Symfony 5 compatibility
|
||||
* Removed Symfony 4 compatibility
|
||||
* Removed Symfony 3 compatibility
|
||||
* Changed README.md
|
||||
* Added CI
|
||||
|
||||
# Version 4.1.3
|
||||
* Fix log exception argument typing
|
||||
|
||||
# Version 4.1.2
|
||||
* Fix DBAL 2.12 compatibility break
|
||||
|
||||
# Version 4.1.0
|
||||
|
||||
* Added custom index for exception log
|
||||
|
||||
# Version 4.0.0
|
||||
|
||||
* Added Symfony 6 support
|
||||
* PHP minimum requirements bumped to 8.0
|
||||
|
||||
# Version 3.1.0
|
||||
|
||||
* Added optional "messenger mode", to delegate jobs execution to workers from the Symfony messenger component
|
||||
* Added support for asynchronous steps execution, using the AMPHP library (contribution from [matyo91](https://github.com/matyo91))
|
||||
|
||||
# Version 3.0.0
|
||||
|
||||
* Added PHP 8 support
|
||||
* PHP minimum requirements bumped to 7.3
|
||||
* Added Doctrine DBAL 3 support
|
||||
* Doctrine DBAL minimum requirements bumped to 2.12
|
||||
|
||||
# Version 2.2.0
|
||||
|
||||
* Improve logging Dataflow job
|
||||
|
||||
# Version 2.1.1
|
||||
|
||||
* Fixed some Symfony 5 compatibility issues
|
||||
|
||||
# Version 2.1.0
|
||||
|
||||
* Added CollectionWriter and DelegatorWriter
|
||||
* Adding Symfony 5.0 compatibility
|
||||
* Save all exceptions caught in the log for `code-rhapsodie:dataflow:execute`
|
||||
* Added more output when errors occured during `code-rhapsodie:dataflow:execute`
|
||||
|
||||
# Version 2.0.2
|
||||
|
||||
* Fixed the connection proxy class created by the factory
|
||||
|
||||
# Version 2.0.1
|
||||
|
||||
* Fixed next execution time not increasing for scheduled dataflows
|
||||
|
||||
# Version 2.0.0
|
||||
|
||||
* Add Doctrine DBAL multi-connection support
|
||||
|
||||
282
README.md
282
README.md
@@ -1,18 +1,23 @@
|
||||
# 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.
|
||||
|
||||
[](https://travis-ci.org/code-rhapsodie/dataflow-bundle)
|
||||
|
||||
[](https://coveralls.io/github/code-rhapsodie/dataflow-bundle)
|
||||
| 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
|
||||
* one or more writers
|
||||
|
||||
The reader can read data from anywhere and return data row by row. Each step processes the current row data.
|
||||
* 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 steps are executed in the order in which they are added.
|
||||
And, one or more writers save the row anywhere you want.
|
||||
|
||||
@@ -20,7 +25,6 @@ As the following schema shows, you can define more than one dataflow:
|
||||
|
||||

|
||||
|
||||
|
||||
# Features
|
||||
|
||||
* Define and configure a Dataflow
|
||||
@@ -32,9 +36,10 @@ 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
|
||||
|
||||
### Add the dependency
|
||||
|
||||
To install this bundle, run this command :
|
||||
@@ -47,7 +52,8 @@ $ 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
|
||||
@@ -60,9 +66,7 @@ $builder->addWriter(new \CodeRhapsodie\DataflowBundle\DataflowType\Writer\PortWr
|
||||
|
||||
### Register the bundle
|
||||
|
||||
#### Symfony 4 (new tree)
|
||||
|
||||
For Symfony 4, add `CodeRhapsodie\DataflowBundle\CodeRhapsodieDataflowBundle::class => ['all' => true],
|
||||
Add `CodeRhapsodie\DataflowBundle\CodeRhapsodieDataflowBundle::class => ['all' => true],
|
||||
` in the `config/bundles.php` file.
|
||||
|
||||
Like this:
|
||||
@@ -77,32 +81,13 @@ 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:
|
||||
|
||||
@@ -126,11 +111,62 @@ code_rhapsodie_dataflow:
|
||||
dbal_default_connection: test #Name of the default connection used by Dataflow bundle
|
||||
```
|
||||
|
||||
By default, the `logger` service will be used to log all exceptions and custom messages.
|
||||
If you want to use another logger, like a specific Monolog handler, Add this configuration:
|
||||
|
||||
```yaml
|
||||
code_rhapsodie_dataflow:
|
||||
default_logger: monolog.logger.custom #Service ID of the logger you want Dataflow to use
|
||||
```
|
||||
|
||||
### Messenger mode
|
||||
|
||||
Dataflow can delegate the execution of its jobs to the Symfony messenger component, if available.
|
||||
This allows jobs to be executed concurrently by workers instead of sequentially.
|
||||
|
||||
To enable messenger mode:
|
||||
|
||||
```yaml
|
||||
code_rhapsodie_dataflow:
|
||||
messenger_mode:
|
||||
enabled: true
|
||||
# bus: 'messenger.default_bus' #Service ID of the bus you want Dataflow to use, if not the default one
|
||||
```
|
||||
|
||||
You also need to route Dataflow messages to the proper transport:
|
||||
|
||||
```yaml
|
||||
# config/packages/messenger.yaml
|
||||
framework:
|
||||
messenger:
|
||||
transports:
|
||||
async: '%env(MESSENGER_TRANSPORT_DSN)%'
|
||||
|
||||
routing:
|
||||
CodeRhapsodie\DataflowBundle\MessengerMode\JobMessage: async
|
||||
```
|
||||
|
||||
### Exceptions mode
|
||||
Dataflow can save exceptions in any filesystem you want.
|
||||
This allows dataflow to save exceptions in filesystem instead of the database
|
||||
You have to install `league/flysystem`.
|
||||
|
||||
To enable exceptions mode:
|
||||
|
||||
```yaml
|
||||
code_rhapsodie_dataflow:
|
||||
exceptions_mode:
|
||||
type: 'file'
|
||||
flysystem_service: 'app.filesystem' #The name of the \League\Flysystem\Filesystem service
|
||||
```
|
||||
|
||||
## 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*
|
||||
@@ -139,8 +175,10 @@ 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:
|
||||
|
||||
@@ -200,14 +238,16 @@ 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
|
||||
CodeRhapsodie\DataflowExemple\DataflowType\MyFirstDataflowType:
|
||||
tags:
|
||||
- { name: coderhapsodie.dataflow.type }
|
||||
```yaml
|
||||
CodeRhapsodie\DataflowExemple\DataflowType\MyFirstDataflowType:
|
||||
tags:
|
||||
- { name: coderhapsodie.dataflow.type }
|
||||
```
|
||||
|
||||
### Use options for your dataflow type
|
||||
@@ -233,14 +273,46 @@ 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.
|
||||
|
||||
### Logging
|
||||
|
||||
All exceptions will be caught and written in the logger.
|
||||
If you want to add custom messages in the log, you can inject the logger in your readers / steps / writers.
|
||||
If your DataflowType class extends `AbstractDataflowType`, the logger is accessible as `$this->logger`.
|
||||
|
||||
```php
|
||||
<?php
|
||||
// ...
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class MyDataflowType extends AbstractDataflowType
|
||||
{
|
||||
// ...
|
||||
protected function buildDataflow(DataflowBuilder $builder, array $options): void
|
||||
{
|
||||
$this->myWriter->setLogger($this->logger);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
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 --show-private
|
||||
$ bin/console debug:container --tag coderhapsodie.dataflow.type
|
||||
```
|
||||
|
||||
The result is like this:
|
||||
@@ -257,10 +329,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`.
|
||||
|
||||
@@ -298,14 +370,16 @@ 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
|
||||
|
||||
@@ -321,6 +395,16 @@ $builder->addStep(function ($item) {
|
||||
return $item;
|
||||
});
|
||||
|
||||
// asynchronous step with 2 scale factor
|
||||
$builder->addStep(function ($item): \Generator {
|
||||
yield new \Amp\Delayed(1000); // asynchronous processing for 1 second long
|
||||
|
||||
// Titles are changed to all caps before export
|
||||
$item['title'] = strtolower($item['title']);
|
||||
|
||||
return $item;
|
||||
}, 2);
|
||||
|
||||
$builder->addStep(function ($item) {
|
||||
// Private items are not exported
|
||||
if ($item['private']) {
|
||||
@@ -332,12 +416,15 @@ $builder->addStep(function ($item) {
|
||||
//[...]
|
||||
```
|
||||
|
||||
Note : you can ensure writing order for asynchronous operations if all steps are scaled at 1 factor.
|
||||
|
||||
### Writers
|
||||
|
||||
*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 :
|
||||
|
||||
@@ -386,6 +473,100 @@ 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.
|
||||
|
||||
```php
|
||||
$builder->addWriter(new CollectionWriter($mySingleItemWriter));
|
||||
```
|
||||
|
||||
#### DelegatorWriter
|
||||
|
||||
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.
|
||||
|
||||
First, create your writers implementing `DelegateWriterInterface` (this interface extends `WriterInterface` so your
|
||||
writers can still be used without the `DelegatorWriter`).
|
||||
|
||||
```php
|
||||
<?php
|
||||
namespace CodeRhapsodie\DataflowExemple\Writer;
|
||||
|
||||
use CodeRhapsodie\DataFlowBundle\DataflowType\Writer\WriterInterface;
|
||||
|
||||
class ProductWriter implements DelegateWriterInterface
|
||||
{
|
||||
public function supports($item): bool
|
||||
{
|
||||
return 'product' === reset($item);
|
||||
}
|
||||
|
||||
public function prepare()
|
||||
{
|
||||
}
|
||||
|
||||
public function write($item)
|
||||
{
|
||||
// Process your product
|
||||
}
|
||||
|
||||
public function finish()
|
||||
{
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```php
|
||||
<?php
|
||||
namespace CodeRhapsodie\DataflowExemple\Writer;
|
||||
|
||||
use CodeRhapsodie\DataFlowBundle\DataflowType\Writer\WriterInterface;
|
||||
|
||||
class OrderWriter implements DelegateWriterInterface
|
||||
{
|
||||
public function supports($item): bool
|
||||
{
|
||||
return 'order' === reset($item);
|
||||
}
|
||||
|
||||
public function prepare()
|
||||
{
|
||||
}
|
||||
|
||||
public function write($item)
|
||||
{
|
||||
// Process your order
|
||||
}
|
||||
|
||||
public function finish()
|
||||
{
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Then, configure your `DelegatorWriter` and add it to your dataflow type.
|
||||
|
||||
```php
|
||||
protected function buildDataflow(DataflowBuilder $builder, array $options): void
|
||||
{
|
||||
// Snip add reader and steps
|
||||
|
||||
$delegatorWriter = new DelegatorWriter();
|
||||
$delegatorWriter->addDelegate(new ProductWriter());
|
||||
$delegatorWriter->addDelegate(new OrderWriter());
|
||||
|
||||
$builder->addWriter($delegatorWriter);
|
||||
}
|
||||
```
|
||||
|
||||
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
|
||||
|
||||
All pending dataflow job processes are stored in a queue into the database.
|
||||
@@ -402,6 +583,9 @@ 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.
|
||||
|
||||
`code-rhapsodie:dataflow:schedule:list` Display the list of dataflows scheduled.
|
||||
|
||||
`code-rhapsodie:dataflow:schedule:change-status` Enable or disable a scheduled dataflow
|
||||
@@ -439,6 +623,8 @@ $ 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.
|
||||
|
||||
# Contributing
|
||||
|
||||
Contributions are very welcome. Please see [CONTRIBUTING.md](CONTRIBUTING.md) for
|
||||
|
||||
@@ -4,7 +4,6 @@ namespace CodeRhapsodie\DataflowBundle\Tests\DataflowType;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\AbstractDataflowType;
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\DataflowBuilder;
|
||||
use PHPUnit\Framework\Constraint\IsIdentical;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
@@ -15,18 +14,12 @@ class AbstractDataflowTypeTest extends TestCase
|
||||
$label = 'Test label';
|
||||
$options = ['testOption' => 'Test value'];
|
||||
$values = [1, 2, 3];
|
||||
$testCase = $this;
|
||||
|
||||
$dataflowType = new class($label, $options, $values) extends AbstractDataflowType
|
||||
$dataflowType = new class($label, $options, $values, $testCase) extends AbstractDataflowType
|
||||
{
|
||||
private $label;
|
||||
private $options;
|
||||
private $values;
|
||||
|
||||
public function __construct(string $label, array $options, array $values)
|
||||
public function __construct(private string $label, private array $options, private array $values, private TestCase $testCase)
|
||||
{
|
||||
$this->label = $label;
|
||||
$this->options = $options;
|
||||
$this->values = $values;
|
||||
}
|
||||
|
||||
public function getLabel(): string
|
||||
@@ -42,7 +35,7 @@ class AbstractDataflowTypeTest extends TestCase
|
||||
protected function buildDataflow(DataflowBuilder $builder, array $options): void
|
||||
{
|
||||
$builder->setReader($this->values);
|
||||
(new IsIdentical($this->options))->evaluate($options);
|
||||
$this->testCase->assertSame($this->options, $options);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
46
Tests/DataflowType/Dataflow/AMPAsyncDataflowTest.php
Normal file
46
Tests/DataflowType/Dataflow/AMPAsyncDataflowTest.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Tests\DataflowType\Dataflow;
|
||||
|
||||
use Amp\Delayed;
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Dataflow\AMPAsyncDataflow;
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Writer\WriterInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AMPAsyncDataflowTest extends TestCase
|
||||
{
|
||||
public function testProcess()
|
||||
{
|
||||
$reader = [1, 2, 3];
|
||||
$result = [];
|
||||
$dataflow = new AMPAsyncDataflow($reader, 'simple');
|
||||
$dataflow->addStep(static fn($item) => $item + 1);
|
||||
$dataflow->addStep(static function($item): \Generator {
|
||||
yield new Delayed(10); //delay 10 milliseconds
|
||||
return $item * 2;
|
||||
});
|
||||
$dataflow->addWriter(new class($result) implements WriterInterface {
|
||||
private $buffer;
|
||||
|
||||
public function __construct(&$buffer) {
|
||||
$this->buffer = &$buffer;
|
||||
}
|
||||
|
||||
public function prepare()
|
||||
{
|
||||
}
|
||||
|
||||
public function write($item)
|
||||
{
|
||||
$this->buffer[] = $item;
|
||||
}
|
||||
|
||||
public function finish()
|
||||
{
|
||||
}
|
||||
});
|
||||
$dataflow->process();
|
||||
|
||||
self::assertSame([4, 6, 8], $result);
|
||||
}
|
||||
}
|
||||
55
Tests/DataflowType/Writer/CollectionWriterTest.php
Normal file
55
Tests/DataflowType/Writer/CollectionWriterTest.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Tests\DataflowType\Writer;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Writer\CollectionWriter;
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Writer\WriterInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Exceptions\UnsupportedItemTypeException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class CollectionWriterTest extends TestCase
|
||||
{
|
||||
public function testNotACollection()
|
||||
{
|
||||
$this->expectException(UnsupportedItemTypeException::class);
|
||||
|
||||
$writer = new CollectionWriter($this->createMock(WriterInterface::class));
|
||||
$writer->write('Not an iterable');
|
||||
}
|
||||
|
||||
public function testSupports()
|
||||
{
|
||||
$writer = new CollectionWriter($this->createMock(WriterInterface::class));
|
||||
|
||||
$this->assertTrue($writer->supports([]));
|
||||
$this->assertTrue($writer->supports(new \ArrayIterator([])));
|
||||
$this->assertFalse($writer->supports(''));
|
||||
$this->assertFalse($writer->supports(0));
|
||||
}
|
||||
|
||||
public function testAll()
|
||||
{
|
||||
$values = ['a', 'b', 'c'];
|
||||
|
||||
$embeddedWriter = $this->createMock(WriterInterface::class);
|
||||
$embeddedWriter
|
||||
->expects($this->once())
|
||||
->method('prepare')
|
||||
;
|
||||
$embeddedWriter
|
||||
->expects($this->once())
|
||||
->method('finish')
|
||||
;
|
||||
$matcher = $this->exactly(count($values));
|
||||
$embeddedWriter
|
||||
->expects($matcher)
|
||||
->method('write')
|
||||
->with($this->callback(fn($arg) => $arg === $values[$matcher->numberOfInvocations() - 1]))
|
||||
;
|
||||
|
||||
$writer = new CollectionWriter($embeddedWriter);
|
||||
$writer->prepare();
|
||||
$writer->write($values);
|
||||
$writer->finish();
|
||||
}
|
||||
}
|
||||
147
Tests/DataflowType/Writer/DelegatorWriterTest.php
Normal file
147
Tests/DataflowType/Writer/DelegatorWriterTest.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Tests\DataflowType\Writer;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Writer\DelegateWriterInterface;
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Writer\DelegatorWriter;
|
||||
use CodeRhapsodie\DataflowBundle\Exceptions\UnsupportedItemTypeException;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DelegatorWriterTest extends TestCase
|
||||
{
|
||||
private DelegatorWriter $delegatorWriter;
|
||||
private DelegateWriterInterface|MockObject $delegateInt;
|
||||
private DelegateWriterInterface|MockObject $delegateString;
|
||||
private DelegateWriterInterface|MockObject $delegateArray;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->delegateInt = $this->createMock(DelegateWriterInterface::class);
|
||||
$this->delegateInt->method('supports')->willReturnCallback(fn($argument) => is_int($argument));
|
||||
|
||||
$this->delegateString = $this->createMock(DelegateWriterInterface::class);
|
||||
$this->delegateString->method('supports')->willReturnCallback(fn($argument) => is_string($argument));
|
||||
|
||||
$this->delegateArray = $this->createMock(DelegateWriterInterface::class);
|
||||
$this->delegateArray->method('supports')->willReturnCallback(fn($argument) => is_array($argument));
|
||||
|
||||
$this->delegatorWriter = new DelegatorWriter();
|
||||
$this->delegatorWriter->addDelegates([
|
||||
$this->delegateInt,
|
||||
$this->delegateString,
|
||||
$this->delegateArray,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testUnsupported()
|
||||
{
|
||||
$this->expectException(UnsupportedItemTypeException::class);
|
||||
|
||||
$this->delegatorWriter->write(new \stdClass());
|
||||
}
|
||||
|
||||
public function testStopAtFirstSupportingDelegate()
|
||||
{
|
||||
$value = 0;
|
||||
|
||||
$this->delegateInt->expects($this->once())->method('supports');
|
||||
$this->delegateInt
|
||||
->expects($this->once())
|
||||
->method('write')
|
||||
->with($value)
|
||||
;
|
||||
$this->delegateString->expects($this->never())->method('supports');
|
||||
$this->delegateArray->expects($this->never())->method('supports');
|
||||
$this->delegateString->expects($this->never())->method('write');
|
||||
$this->delegateArray->expects($this->never())->method('write');
|
||||
|
||||
$this->delegatorWriter->write($value);
|
||||
}
|
||||
|
||||
public function testNotSupported()
|
||||
{
|
||||
$value = new \stdClass();
|
||||
|
||||
$this->delegateInt
|
||||
->expects($this->once())
|
||||
->method('supports')
|
||||
->with($value)
|
||||
;
|
||||
$this->delegateString
|
||||
->expects($this->once())
|
||||
->method('supports')
|
||||
->with($value)
|
||||
;
|
||||
$this->delegateArray
|
||||
->expects($this->once())
|
||||
->method('supports')
|
||||
->with($value)
|
||||
;
|
||||
|
||||
$this->assertFalse($this->delegatorWriter->supports($value));
|
||||
}
|
||||
|
||||
public function testSupported()
|
||||
{
|
||||
$value = '';
|
||||
|
||||
$this->delegateInt
|
||||
->expects($this->once())
|
||||
->method('supports')
|
||||
->with($value)
|
||||
;
|
||||
$this->delegateString
|
||||
->expects($this->once())
|
||||
->method('supports')
|
||||
->with($value)
|
||||
;
|
||||
$this->delegateArray
|
||||
->expects($this->never())
|
||||
->method('supports')
|
||||
;
|
||||
|
||||
$this->assertTrue($this->delegatorWriter->supports($value));
|
||||
}
|
||||
|
||||
public function testAll()
|
||||
{
|
||||
$value = ['a'];
|
||||
|
||||
$this->delegateInt
|
||||
->expects($this->once())
|
||||
->method('supports')
|
||||
->with($value)
|
||||
;
|
||||
$this->delegateString
|
||||
->expects($this->once())
|
||||
->method('supports')
|
||||
->with($value)
|
||||
;
|
||||
$this->delegateArray
|
||||
->expects($this->once())
|
||||
->method('supports')
|
||||
->with($value)
|
||||
;
|
||||
|
||||
$this->delegateInt->expects($this->once())->method('prepare');
|
||||
$this->delegateString->expects($this->once())->method('prepare');
|
||||
$this->delegateArray->expects($this->once())->method('prepare');
|
||||
|
||||
$this->delegateInt->expects($this->once())->method('finish');
|
||||
$this->delegateString->expects($this->once())->method('finish');
|
||||
$this->delegateArray->expects($this->once())->method('finish');
|
||||
|
||||
$this->delegateInt->expects($this->never())->method('write');
|
||||
$this->delegateString->expects($this->never())->method('write');
|
||||
$this->delegateArray
|
||||
->expects($this->once())
|
||||
->method('write')
|
||||
->with($value)
|
||||
;
|
||||
|
||||
$this->delegatorWriter->prepare();
|
||||
$this->delegatorWriter->write($value);
|
||||
$this->delegatorWriter->finish();
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,8 @@ class PortWriterAdapterTest extends TestCase
|
||||
{
|
||||
$value = 'not an array';
|
||||
|
||||
$writer = $this->getMockBuilder('\Port\Writer')
|
||||
->setMethods(['prepare', 'finish', 'writeItem'])
|
||||
$writer = $this->getMockBuilder(\Port\Writer::class)
|
||||
->onlyMethods(['prepare', 'finish', 'writeItem'])
|
||||
->getMock()
|
||||
;
|
||||
$writer
|
||||
|
||||
@@ -2,31 +2,21 @@
|
||||
|
||||
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;
|
||||
use Doctrine\DBAL\Driver\Connection;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ScheduledDataflowManagerTest extends TestCase
|
||||
{
|
||||
/** @var ScheduledDataflowManager */
|
||||
private $manager;
|
||||
|
||||
/** @var Connection|MockObject */
|
||||
private $connection;
|
||||
|
||||
/** @var ScheduledDataflowRepository|MockObject */
|
||||
private $scheduledDataflowRepository;
|
||||
|
||||
/** @var JobRepository|MockObject */
|
||||
private $jobRepository;
|
||||
private ScheduledDataflowManager $manager;
|
||||
private Connection|MockObject $connection;
|
||||
private ScheduledDataflowRepository|MockObject $scheduledDataflowRepository;
|
||||
private JobRepository|MockObject $jobRepository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
@@ -55,10 +45,15 @@ class ScheduledDataflowManagerTest extends TestCase
|
||||
->willReturn([$scheduled1, $scheduled2])
|
||||
;
|
||||
|
||||
$matcher = $this->exactly(2);
|
||||
$this->jobRepository
|
||||
->expects($this->exactly(2))
|
||||
->expects($matcher)
|
||||
->method('findPendingForScheduledDataflow')
|
||||
->withConsecutive([$scheduled1], [$scheduled2])
|
||||
->with($this->callback(fn($arg) => match ($matcher->numberOfInvocations()) {
|
||||
1 => $arg === $scheduled1,
|
||||
2 => $arg === $scheduled2,
|
||||
default => false,
|
||||
}))
|
||||
->willReturnOnConsecutiveCalls(new Job(), null)
|
||||
;
|
||||
|
||||
@@ -70,16 +65,12 @@ class ScheduledDataflowManagerTest extends TestCase
|
||||
->expects($this->once())
|
||||
->method('save')
|
||||
->with(
|
||||
$this->callback(function (Job $job) use ($type, $options, $next, $label, $scheduled2) {
|
||||
return (
|
||||
$job->getStatus() === Job::STATUS_PENDING
|
||||
&& $job->getDataflowType() === $type
|
||||
&& $job->getOptions() === $options
|
||||
&& $job->getRequestedDate() == $next
|
||||
&& $job->getLabel() === $label
|
||||
&& $job->getScheduledDataflowId() === $scheduled2->getId()
|
||||
);
|
||||
})
|
||||
$this->callback(fn(Job $job) => $job->getStatus() === Job::STATUS_PENDING
|
||||
&& $job->getDataflowType() === $type
|
||||
&& $job->getOptions() === $options
|
||||
&& $job->getRequestedDate() == $next
|
||||
&& $job->getLabel() === $label
|
||||
&& $job->getScheduledDataflowId() === $scheduled2->getId())
|
||||
)
|
||||
;
|
||||
|
||||
@@ -98,4 +89,44 @@ class ScheduledDataflowManagerTest extends TestCase
|
||||
|
||||
$this->assertEquals($next->add(\DateInterval::createFromDateString($frequency)), $scheduled2->getNext());
|
||||
}
|
||||
|
||||
public function testCreateJobsFromScheduledDataflowsWithError()
|
||||
{
|
||||
$scheduled1 = new ScheduledDataflow();
|
||||
|
||||
$this->scheduledDataflowRepository
|
||||
->expects($this->once())
|
||||
->method('findReadyToRun')
|
||||
->willReturn([$scheduled1])
|
||||
;
|
||||
|
||||
$this->jobRepository
|
||||
->expects($this->exactly(1))
|
||||
->method('findPendingForScheduledDataflow')
|
||||
->with($scheduled1)
|
||||
->willThrowException(new \Exception())
|
||||
;
|
||||
|
||||
$this->connection
|
||||
->expects($this->once())
|
||||
->method('beginTransaction')
|
||||
;
|
||||
$this->jobRepository
|
||||
->expects($this->never())
|
||||
->method('save')
|
||||
;
|
||||
|
||||
$this->connection
|
||||
->expects($this->never())
|
||||
->method('commit')
|
||||
;
|
||||
$this->connection
|
||||
->expects($this->once())
|
||||
->method('rollBack')
|
||||
;
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
$this->manager->createJobsFromScheduledDataflows();
|
||||
}
|
||||
}
|
||||
|
||||
48
Tests/MessengerMode/JobMessageHandlerTest.php
Normal file
48
Tests/MessengerMode/JobMessageHandlerTest.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Tests\MessengerMode;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Entity\Job;
|
||||
use CodeRhapsodie\DataflowBundle\MessengerMode\JobMessage;
|
||||
use CodeRhapsodie\DataflowBundle\MessengerMode\JobMessageHandler;
|
||||
use CodeRhapsodie\DataflowBundle\Processor\JobProcessorInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class JobMessageHandlerTest extends TestCase
|
||||
{
|
||||
private JobRepository|MockObject $repository;
|
||||
private JobProcessorInterface|MockObject $processor;
|
||||
private JobMessageHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = $this->createMock(JobRepository::class);
|
||||
$this->processor = $this->createMock(JobProcessorInterface::class);
|
||||
|
||||
$this->handler = new JobMessageHandler($this->repository, $this->processor);
|
||||
}
|
||||
|
||||
public function testInvoke()
|
||||
{
|
||||
$message = new JobMessage($id = 32);
|
||||
|
||||
$this->repository
|
||||
->expects($this->once())
|
||||
->method('find')
|
||||
->with($id)
|
||||
->willReturn($job = new Job())
|
||||
;
|
||||
|
||||
$this->processor
|
||||
->expects($this->once())
|
||||
->method('process')
|
||||
->with($job)
|
||||
;
|
||||
|
||||
($this->handler)($message);
|
||||
}
|
||||
}
|
||||
90
Tests/Processor/JobProcessorTest.php
Normal file
90
Tests/Processor/JobProcessorTest.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Tests\Processor;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\DataflowTypeInterface;
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Result;
|
||||
use CodeRhapsodie\DataflowBundle\Entity\Job;
|
||||
use CodeRhapsodie\DataflowBundle\Event\Events;
|
||||
use CodeRhapsodie\DataflowBundle\Event\ProcessingEvent;
|
||||
use CodeRhapsodie\DataflowBundle\Gateway\JobGateway;
|
||||
use CodeRhapsodie\DataflowBundle\Processor\JobProcessor;
|
||||
use CodeRhapsodie\DataflowBundle\Registry\DataflowTypeRegistryInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
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 JobGateway|MockObject $jobGateway;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = $this->createMock(JobRepository::class);
|
||||
$this->registry = $this->createMock(DataflowTypeRegistryInterface::class);
|
||||
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
|
||||
$this->jobGateway = $this->createMock(JobGateway::class);
|
||||
|
||||
$this->processor = new JobProcessor($this->repository, $this->registry, $this->dispatcher, $this->jobGateway);
|
||||
}
|
||||
|
||||
public function testProcess()
|
||||
{
|
||||
$now = new \DateTimeImmutable();
|
||||
$job = (new Job())
|
||||
->setStatus(Job::STATUS_PENDING)
|
||||
->setDataflowType($type = 'type')
|
||||
->setOptions($options = ['option1' => 'value1'])
|
||||
;
|
||||
|
||||
$matcher = $this->exactly(2);
|
||||
$this->dispatcher
|
||||
->expects($matcher)
|
||||
->method('dispatch')
|
||||
->with(
|
||||
$this->callback(fn($arg) => $arg instanceof ProcessingEvent && $arg->getJob() === $job),
|
||||
$this->callback(fn($arg) => match ($matcher->numberOfInvocations()) {
|
||||
1 => $arg === Events::BEFORE_PROCESSING,
|
||||
2 => $arg === Events::AFTER_PROCESSING,
|
||||
default => false,
|
||||
})
|
||||
);
|
||||
|
||||
$dataflowType = $this->createMock(DataflowTypeInterface::class);
|
||||
|
||||
$this->registry
|
||||
->expects($this->once())
|
||||
->method('getDataflowType')
|
||||
->with($type)
|
||||
->willReturn($dataflowType)
|
||||
;
|
||||
|
||||
$bag = [new \Exception('message1')];
|
||||
|
||||
$result = new Result('name', new \DateTimeImmutable(), $end = new \DateTimeImmutable(), $count = 10, $bag);
|
||||
|
||||
$dataflowType
|
||||
->expects($this->once())
|
||||
->method('process')
|
||||
->with($options)
|
||||
->willReturn($result)
|
||||
;
|
||||
|
||||
$this->jobGateway
|
||||
->expects($this->exactly(2))
|
||||
->method('save')
|
||||
;
|
||||
|
||||
$this->processor->process($job);
|
||||
|
||||
$this->assertGreaterThanOrEqual($now, $job->getStartTime());
|
||||
$this->assertSame(Job::STATUS_COMPLETED, $job->getStatus());
|
||||
$this->assertSame($end, $job->getEndTime());
|
||||
$this->assertSame($count - count($bag), $job->getCount());
|
||||
}
|
||||
}
|
||||
@@ -10,8 +10,7 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DataflowTypeRegistryTest extends TestCase
|
||||
{
|
||||
/** @var DataflowTypeRegistry */
|
||||
private $registry;
|
||||
private DataflowTypeRegistry $registry;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
@@ -33,7 +32,7 @@ class DataflowTypeRegistryTest extends TestCase
|
||||
|
||||
$this->registry->registerDataflowType($type);
|
||||
|
||||
$this->assertSame($type, $this->registry->getDataflowType(get_class($type)));
|
||||
$this->assertSame($type, $this->registry->getDataflowType($type::class));
|
||||
$this->assertSame($type, $this->registry->getDataflowType($alias1));
|
||||
$this->assertSame($type, $this->registry->getDataflowType($alias2));
|
||||
$this->assertContains($type, $this->registry->listDataflowTypes());
|
||||
|
||||
69
Tests/Runner/MessengerDataflowRunnerTest.php
Normal file
69
Tests/Runner/MessengerDataflowRunnerTest.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Tests\Runner;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Entity\Job;
|
||||
use CodeRhapsodie\DataflowBundle\MessengerMode\JobMessage;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
use CodeRhapsodie\DataflowBundle\Runner\MessengerDataflowRunner;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
|
||||
class MessengerDataflowRunnerTest extends TestCase
|
||||
{
|
||||
private MessengerDataflowRunner $runner;
|
||||
private JobRepository|MockObject $repository;
|
||||
private MessageBusInterface|MockObject $bus;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = $this->createMock(JobRepository::class);
|
||||
$this->bus = $this->createMock(MessageBusInterface::class);
|
||||
|
||||
$this->runner = new MessengerDataflowRunner($this->repository, $this->bus);
|
||||
}
|
||||
|
||||
public function testRunPendingDataflows()
|
||||
{
|
||||
$job1 = (new Job())->setId($id1 = 10);
|
||||
$job2 = (new Job())->setId($id2 = 20);
|
||||
|
||||
$this->repository
|
||||
->expects($this->exactly(3))
|
||||
->method('findNextPendingDataflow')
|
||||
->willReturnOnConsecutiveCalls($job1, $job2, null)
|
||||
;
|
||||
$matcher = $this->exactly(2);
|
||||
$this->repository
|
||||
->expects($matcher)
|
||||
->method('save')
|
||||
->with($this->callback(fn($arg) => match ($matcher->numberOfInvocations()) {
|
||||
1 => $arg === $job1,
|
||||
2 => $arg === $job2,
|
||||
default => false,
|
||||
}))
|
||||
;
|
||||
|
||||
$matcher = $this->exactly(2);
|
||||
$this->bus
|
||||
->expects($matcher)
|
||||
->method('dispatch')
|
||||
->with($this->callback(fn($arg) => match ($matcher->numberOfInvocations()) {
|
||||
1 => $arg instanceof JobMessage && $arg->getJobId() === $id1,
|
||||
2 => $arg instanceof JobMessage && $arg->getJobId() === $id2,
|
||||
default => false,
|
||||
}))
|
||||
->willReturnOnConsecutiveCalls(
|
||||
new Envelope(new JobMessage($id1)),
|
||||
new Envelope(new JobMessage($id2))
|
||||
)
|
||||
;
|
||||
|
||||
$this->runner->runPendingDataflows();
|
||||
|
||||
$this->assertSame(Job::STATUS_QUEUED, $job1->getStatus());
|
||||
$this->assertSame(Job::STATUS_QUEUED, $job2->getStatus());
|
||||
}
|
||||
}
|
||||
@@ -2,55 +2,31 @@
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Tests\Runner;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\DataflowTypeInterface;
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Result;
|
||||
use CodeRhapsodie\DataflowBundle\Entity\Job;
|
||||
use CodeRhapsodie\DataflowBundle\Event\Events;
|
||||
use CodeRhapsodie\DataflowBundle\Event\ProcessingEvent;
|
||||
use CodeRhapsodie\DataflowBundle\Registry\DataflowTypeRegistryInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Processor\JobProcessorInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
use CodeRhapsodie\DataflowBundle\Runner\PendingDataflowRunner;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
class PendingDataflowRunnerTest extends TestCase
|
||||
{
|
||||
/** @var PendingDataflowRunner */
|
||||
private $runner;
|
||||
|
||||
/** @var JobRepository|MockObject */
|
||||
private $repository;
|
||||
|
||||
/** @var DataflowTypeRegistryInterface|MockObject */
|
||||
private $registry;
|
||||
|
||||
/** @var EventDispatcherInterface|MockObject */
|
||||
private $dispatcher;
|
||||
private PendingDataflowRunner $runner;
|
||||
private JobRepository|MockObject $repository;
|
||||
private JobProcessorInterface|MockObject $processor;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = $this->createMock(JobRepository::class);
|
||||
$this->registry = $this->createMock(DataflowTypeRegistryInterface::class);
|
||||
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
|
||||
$this->processor = $this->createMock(JobProcessorInterface::class);
|
||||
|
||||
$this->runner = new PendingDataflowRunner($this->repository, $this->registry, $this->dispatcher);
|
||||
$this->runner = new PendingDataflowRunner($this->repository, $this->processor);
|
||||
}
|
||||
|
||||
public function testRunPendingDataflows()
|
||||
{
|
||||
$now = new \DateTime();
|
||||
$job1 = (new Job())
|
||||
->setStatus(Job::STATUS_PENDING)
|
||||
->setDataflowType($type1 = 'type1')
|
||||
->setOptions($options1 = ['option1' => 'value1'])
|
||||
;
|
||||
$job2 = (new Job())
|
||||
->setStatus(Job::STATUS_PENDING)
|
||||
->setDataflowType($type2 = 'type2')
|
||||
->setOptions($options2 = ['option2' => 'value2'])
|
||||
;
|
||||
$job1 = new Job();
|
||||
$job2 = new Job();
|
||||
|
||||
$this->repository
|
||||
->expects($this->exactly(3))
|
||||
@@ -58,81 +34,17 @@ class PendingDataflowRunnerTest extends TestCase
|
||||
->willReturnOnConsecutiveCalls($job1, $job2, null)
|
||||
;
|
||||
|
||||
$this->dispatcher
|
||||
->expects($this->exactly(4))
|
||||
->method('dispatch')
|
||||
->withConsecutive(
|
||||
[
|
||||
Events::BEFORE_PROCESSING,
|
||||
$this->callback(function (ProcessingEvent $event) use ($job1) {
|
||||
return $event->getJob() === $job1;
|
||||
})
|
||||
],
|
||||
[
|
||||
Events::AFTER_PROCESSING,
|
||||
$this->callback(function (ProcessingEvent $event) use ($job1) {
|
||||
return $event->getJob() === $job1;
|
||||
})
|
||||
],
|
||||
[
|
||||
Events::BEFORE_PROCESSING,
|
||||
$this->callback(function (ProcessingEvent $event) use ($job2) {
|
||||
return $event->getJob() === $job2;
|
||||
})
|
||||
],
|
||||
[
|
||||
Events::AFTER_PROCESSING,
|
||||
$this->callback(function (ProcessingEvent $event) use ($job2) {
|
||||
return $event->getJob() === $job2;
|
||||
})
|
||||
]
|
||||
)
|
||||
;
|
||||
|
||||
$dataflowType1 = $this->createMock(DataflowTypeInterface::class);
|
||||
$dataflowType2 = $this->createMock(DataflowTypeInterface::class);
|
||||
|
||||
$this->registry
|
||||
->expects($this->exactly(2))
|
||||
->method('getDataflowType')
|
||||
->withConsecutive([$type1], [$type2])
|
||||
->willReturnOnConsecutiveCalls($dataflowType1, $dataflowType2)
|
||||
;
|
||||
|
||||
$bag1 = [new \Exception('message1')];
|
||||
$bag2 = [new \Exception('message2')];
|
||||
|
||||
$result1 = new Result('name', new \DateTime(), $end1 = new \DateTime(), $count1 = 10, $bag1);
|
||||
$result2 = new Result('name', new \DateTime(), $end2 = new \DateTime(), $count2 = 20, $bag2);
|
||||
|
||||
$dataflowType1
|
||||
->expects($this->once())
|
||||
$matcher = $this->exactly(2);
|
||||
$this->processor
|
||||
->expects($matcher)
|
||||
->method('process')
|
||||
->with($options1)
|
||||
->willReturn($result1)
|
||||
;
|
||||
$dataflowType2
|
||||
->expects($this->once())
|
||||
->method('process')
|
||||
->with($options2)
|
||||
->willReturn($result2)
|
||||
;
|
||||
|
||||
$this->repository
|
||||
->expects($this->exactly(4))
|
||||
->method('save')
|
||||
->with($this->callback(fn($arg) => match ($matcher->numberOfInvocations()) {
|
||||
1 => $arg === $job1,
|
||||
2 => $arg === $job2,
|
||||
default => false,
|
||||
}))
|
||||
;
|
||||
|
||||
$this->runner->runPendingDataflows();
|
||||
|
||||
$this->assertGreaterThanOrEqual($now, $job1->getStartTime());
|
||||
$this->assertSame(Job::STATUS_COMPLETED, $job1->getStatus());
|
||||
$this->assertSame($end1, $job1->getEndTime());
|
||||
$this->assertSame($count1 - count($bag1), $job1->getCount());
|
||||
|
||||
$this->assertGreaterThanOrEqual($now, $job2->getStartTime());
|
||||
$this->assertSame(Job::STATUS_COMPLETED, $job2->getStatus());
|
||||
$this->assertSame($end2, $job2->getEndTime());
|
||||
$this->assertSame($count2 - count($bag2), $job2->getCount());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
protected function createValidator(): ConstraintValidatorInterface
|
||||
{
|
||||
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 function getValidValues()
|
||||
public static function getValidValues()
|
||||
{
|
||||
return [
|
||||
['3 days'],
|
||||
@@ -47,9 +47,7 @@ class FrequencyValidatorTest extends ConstraintValidatorTestCase
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getNegativeValues
|
||||
*/
|
||||
#[DataProvider('getNegativeValues')]
|
||||
public function testNegativeIntervals($value)
|
||||
{
|
||||
$constraint = new Frequency([
|
||||
@@ -64,7 +62,7 @@ class FrequencyValidatorTest extends ConstraintValidatorTestCase
|
||||
;
|
||||
}
|
||||
|
||||
public function getNegativeValues()
|
||||
public static function getNegativeValues()
|
||||
{
|
||||
return [
|
||||
['now'],
|
||||
|
||||
@@ -2,7 +2,12 @@
|
||||
"name": "code-rhapsodie/dataflow-bundle",
|
||||
"description": "Data processing framework inspired by PortPHP",
|
||||
"type": "symfony-bundle",
|
||||
"keywords": ["dataflow", "import", "export", "data processing"],
|
||||
"keywords": [
|
||||
"dataflow",
|
||||
"import",
|
||||
"export",
|
||||
"data processing"
|
||||
],
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
@@ -36,33 +41,43 @@
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"php": "^7.1",
|
||||
"doctrine/dbal": "^2.0",
|
||||
"seld/signal-handler": "^1.0",
|
||||
"symfony/config": "^3.4||^4.0",
|
||||
"symfony/console": "^3.4||^4.0",
|
||||
"symfony/dependency-injection": "^3.4||^4.0",
|
||||
"symfony/event-dispatcher": "^3.4||^4.0",
|
||||
"symfony/http-kernel": "^3.4||^4.0",
|
||||
"symfony/lock": "^3.4||^4.0",
|
||||
"symfony/options-resolver": "^3.4||^4.0",
|
||||
"symfony/validator": "^3.4||^4.0",
|
||||
"symfony/yaml": "^3.4||^4.0",
|
||||
"doctrine/doctrine-bundle": "^1.0"
|
||||
"php": "^8.0",
|
||||
"ext-json": "*",
|
||||
"doctrine/dbal": "^3.0||^4.0",
|
||||
"doctrine/doctrine-bundle": "^2.0",
|
||||
"monolog/monolog": "^2.0||^3.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"
|
||||
},
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "^2.15",
|
||||
"phpunit/phpunit": "^7||^8"
|
||||
"amphp/amp": "^2.5",
|
||||
"friendsofphp/php-cs-fixer": "^3.75",
|
||||
"phpunit/phpunit": "^11",
|
||||
"portphp/portphp": "^1.9",
|
||||
"rector/rector": "^2.0",
|
||||
"symfony/messenger": "^7.0"
|
||||
},
|
||||
"suggest": {
|
||||
"portphp/portphp": "Provides generic readers, steps and writers for your dataflows."
|
||||
"amphp/amp": "Provide asynchronous steps for your dataflows",
|
||||
"portphp/portphp": "Provides generic readers, steps and writers for your dataflows.",
|
||||
"symfony/messenger": "Allows messenger mode, i.e. letting workers run jobs"
|
||||
},
|
||||
"config": {
|
||||
"sort-packages": true
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "2.x-dev",
|
||||
"dev-master": "2.1.x-dev",
|
||||
"dev-v2.0.x": "2.0.x-dev",
|
||||
"dev-v1.x": "1.x-dev"
|
||||
}
|
||||
}
|
||||
|
||||
49
phpunit.xml
49
phpunit.xml
@@ -1,32 +1,21 @@
|
||||
<?xml version = '1.0' encoding = 'UTF-8'?>
|
||||
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
|
||||
<phpunit
|
||||
backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
bootstrap="Tests/bootstrap.php"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
colors="false"
|
||||
>
|
||||
<php>
|
||||
<ini name="error_reporting" value="-1" />
|
||||
</php>
|
||||
<testsuites>
|
||||
<testsuite name="Dataflow tests suite">
|
||||
<directory suffix="Test.php">./Tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory>./src/</directory>
|
||||
<exclude>
|
||||
<directory>Tests/</directory>
|
||||
<directory>vendor/</directory>
|
||||
</exclude>
|
||||
</whitelist>
|
||||
</filter>
|
||||
|
||||
<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">
|
||||
<php>
|
||||
<ini name="error_reporting" value="-1"/>
|
||||
</php>
|
||||
<testsuites>
|
||||
<testsuite name="Dataflow tests suite">
|
||||
<directory suffix="Test.php">./Tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<source>
|
||||
<include>
|
||||
<directory>./src/</directory>
|
||||
</include>
|
||||
<exclude>
|
||||
<directory>Tests/</directory>
|
||||
<directory>vendor/</directory>
|
||||
</exclude>
|
||||
</source>
|
||||
</phpunit>
|
||||
|
||||
25
rector.php
Normal file
25
rector.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Set\ValueObject\LevelSetList;
|
||||
use Rector\Symfony\Set\SymfonySetList;
|
||||
|
||||
return static function (RectorConfig $rectorConfig): void {
|
||||
$rectorConfig->paths([
|
||||
__DIR__ . '/src',
|
||||
__DIR__ . '/Tests',
|
||||
]);
|
||||
|
||||
// register a single rule
|
||||
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
|
||||
|
||||
$rectorConfig->sets([
|
||||
SymfonySetList::SYMFONY_70,
|
||||
SymfonySetList::SYMFONY_CODE_QUALITY,
|
||||
SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION,
|
||||
LevelSetList::UP_TO_PHP_80,
|
||||
]);
|
||||
};
|
||||
4
sonar-project.properties
Normal file
4
sonar-project.properties
Normal file
@@ -0,0 +1,4 @@
|
||||
sonar.projectKey=code-rhapsodie_dataflow-bundle_AYvYuaAwWE9sbcQmD1vw
|
||||
|
||||
sonar.sources=src
|
||||
sonar.tests=Tests
|
||||
@@ -5,8 +5,12 @@ declare(strict_types=1);
|
||||
namespace CodeRhapsodie\DataflowBundle;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\DependencyInjection\CodeRhapsodieDataflowExtension;
|
||||
use CodeRhapsodie\DataflowBundle\DependencyInjection\Compiler\BusCompilerPass;
|
||||
use CodeRhapsodie\DataflowBundle\DependencyInjection\Compiler\DataflowTypeCompilerPass;
|
||||
use CodeRhapsodie\DataflowBundle\DependencyInjection\Compiler\DefaultLoggerCompilerPass;
|
||||
use CodeRhapsodie\DataflowBundle\DependencyInjection\Compiler\ExceptionCompilerPass;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
|
||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||
|
||||
/**
|
||||
@@ -14,15 +18,20 @@ use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||
*/
|
||||
class CodeRhapsodieDataflowBundle extends Bundle
|
||||
{
|
||||
protected $name = 'CodeRhapsodieDataflowBundle';
|
||||
protected string $name = 'CodeRhapsodieDataflowBundle';
|
||||
|
||||
public function getContainerExtension()
|
||||
public function getContainerExtension(): ?ExtensionInterface
|
||||
{
|
||||
return new CodeRhapsodieDataflowExtension();
|
||||
}
|
||||
|
||||
public function build(ContainerBuilder $container)
|
||||
public function build(ContainerBuilder $container): void
|
||||
{
|
||||
$container->addCompilerPass(new DataflowTypeCompilerPass());
|
||||
$container
|
||||
->addCompilerPass(new DataflowTypeCompilerPass())
|
||||
->addCompilerPass(new DefaultLoggerCompilerPass())
|
||||
->addCompilerPass(new BusCompilerPass())
|
||||
->addCompilerPass(new ExceptionCompilerPass())
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,67 +5,49 @@ declare(strict_types=1);
|
||||
namespace CodeRhapsodie\DataflowBundle\Command;
|
||||
|
||||
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;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
#[AsCommand('code-rhapsodie:dataflow:schedule:add', 'Create a scheduled dataflow')]
|
||||
class AddScheduledDataflowCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'code-rhapsodie:dataflow:schedule:add';
|
||||
|
||||
/** @var DataflowTypeRegistryInterface */
|
||||
private $registry;
|
||||
/** @var ScheduledDataflowRepository */
|
||||
private $scheduledDataflowRepository;
|
||||
/** @var ValidatorInterface */
|
||||
private $validator;
|
||||
|
||||
/** @var ConnectionFactory */
|
||||
private $connectionFactory;
|
||||
|
||||
public function __construct(DataflowTypeRegistryInterface $registry, ScheduledDataflowRepository $scheduledDataflowRepository, ValidatorInterface $validator, ConnectionFactory $connectionFactory)
|
||||
public function __construct(private DataflowTypeRegistryInterface $registry, private ScheduledDataflowRepository $scheduledDataflowRepository, private ValidatorInterface $validator, private ConnectionFactory $connectionFactory)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->registry = $registry;
|
||||
$this->scheduledDataflowRepository = $scheduledDataflowRepository;
|
||||
$this->validator = $validator;
|
||||
$this->connectionFactory = $connectionFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
protected function configure(): void
|
||||
{
|
||||
$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)')
|
||||
->addOption('options', null, InputOption::VALUE_OPTIONAL,
|
||||
'Options of the scheduled dataflow (ex: {"option1": "value1", "option2": "value2"})')
|
||||
->addOption(
|
||||
'options',
|
||||
null,
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'Options of the scheduled dataflow (ex: {"option1": "value1", "option2": "value2"})'
|
||||
)
|
||||
->addOption('frequency', null, InputOption::VALUE_REQUIRED, 'Frequency of the scheduled dataflow')
|
||||
->addOption('first_run', null, InputOption::VALUE_REQUIRED, 'Date for the first run of the scheduled dataflow (Y-m-d H:i:s)')
|
||||
->addOption('enabled', null, InputOption::VALUE_REQUIRED, 'State of the scheduled dataflow')
|
||||
->addOption('connection', null, InputOption::VALUE_REQUIRED, 'Define the DBAL connection to use');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
if (null !== $input->getOption('connection')) {
|
||||
if ($input->getOption('connection') !== null) {
|
||||
$this->connectionFactory->setConnectionName($input->getOption('connection'));
|
||||
}
|
||||
$choices = [];
|
||||
@@ -87,13 +69,17 @@ class AddScheduledDataflowCommand extends Command
|
||||
}
|
||||
$options = $input->getOption('options');
|
||||
if (!$options) {
|
||||
$options = $io->ask('What are the launch options for the scheduled dataflow? (ex: {"option1": "value1", "option2": "value2"})',
|
||||
json_encode([]));
|
||||
$options = $io->ask(
|
||||
'What are the launch options for the scheduled dataflow? (ex: {"option1": "value1", "option2": "value2"})',
|
||||
json_encode([])
|
||||
);
|
||||
}
|
||||
$frequency = $input->getOption('frequency');
|
||||
if (!$frequency) {
|
||||
$frequency = $io->choice('What is the frequency for the scheduled dataflow?',
|
||||
ScheduledDataflow::AVAILABLE_FREQUENCIES);
|
||||
$frequency = $io->choice(
|
||||
'What is the frequency for the scheduled dataflow?',
|
||||
ScheduledDataflow::AVAILABLE_FREQUENCIES
|
||||
);
|
||||
}
|
||||
$firstRun = $input->getOption('first_run');
|
||||
if (!$firstRun) {
|
||||
@@ -108,22 +94,25 @@ class AddScheduledDataflowCommand extends Command
|
||||
'id' => null,
|
||||
'label' => $label,
|
||||
'dataflow_type' => $type,
|
||||
'options' => json_decode($options, true),
|
||||
'options' => json_decode($options, true, 512, \JSON_THROW_ON_ERROR),
|
||||
'frequency' => $frequency,
|
||||
'next' => new \DateTimeImmutable($firstRun),
|
||||
'next' => new \DateTime($firstRun),
|
||||
'enabled' => $enabled,
|
||||
]);
|
||||
|
||||
$errors = $this->validator->validate($newScheduledDataflow);
|
||||
if (count($errors) > 0) {
|
||||
if (\count($errors) > 0) {
|
||||
$io->error((string) $errors);
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
$this->scheduledDataflowRepository->save($newScheduledDataflow);
|
||||
$io->success(sprintf('New scheduled dataflow "%s" (id:%d) was created successfully.',
|
||||
$newScheduledDataflow->getLabel(), $newScheduledDataflow->getId()));
|
||||
$io->success(\sprintf(
|
||||
'New scheduled dataflow "%s" (id:%d) was created successfully.',
|
||||
$newScheduledDataflow->getLabel(),
|
||||
$newScheduledDataflow->getId()
|
||||
));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -5,43 +5,30 @@ declare(strict_types=1);
|
||||
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;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
#[AsCommand('code-rhapsodie:dataflow:schedule:change-status', 'Change schedule status')]
|
||||
class ChangeScheduleStatusCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'code-rhapsodie:dataflow:schedule:change-status';
|
||||
|
||||
/** @var ScheduledDataflowRepository */
|
||||
private $scheduledDataflowRepository;
|
||||
|
||||
/** @var ConnectionFactory */
|
||||
private $connectionFactory;
|
||||
|
||||
public function __construct(ScheduledDataflowRepository $scheduledDataflowRepository, ConnectionFactory $connectionFactory)
|
||||
public function __construct(private ScheduledDataflowRepository $scheduledDataflowRepository, private ConnectionFactory $connectionFactory)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->scheduledDataflowRepository = $scheduledDataflowRepository;
|
||||
$this->connectionFactory = $connectionFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
protected function configure(): void
|
||||
{
|
||||
$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')
|
||||
@@ -49,12 +36,9 @@ class ChangeScheduleStatusCommand extends Command
|
||||
->addOption('connection', null, InputOption::VALUE_REQUIRED, 'Define the DBAL connection to use');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
if (null !== $input->getOption('connection')) {
|
||||
if ($input->getOption('connection') !== null) {
|
||||
$this->connectionFactory->setConnectionName($input->getOption('connection'));
|
||||
}
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
@@ -62,7 +46,7 @@ class ChangeScheduleStatusCommand extends Command
|
||||
$schedule = $this->scheduledDataflowRepository->find((int) $input->getArgument('schedule-id'));
|
||||
|
||||
if (!$schedule) {
|
||||
$io->error(sprintf('Cannot find scheduled dataflow with id "%d".', $input->getArgument('schedule-id')));
|
||||
$io->error(\sprintf('Cannot find scheduled dataflow with id "%d".', $input->getArgument('schedule-id')));
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -81,9 +65,9 @@ class ChangeScheduleStatusCommand extends Command
|
||||
try {
|
||||
$schedule->setEnabled($input->getOption('enable'));
|
||||
$this->scheduledDataflowRepository->save($schedule);
|
||||
$io->success(sprintf('Schedule with id "%s" has been successfully updated.', $schedule->getId()));
|
||||
$io->success(\sprintf('Schedule with id "%s" has been successfully updated.', $schedule->getId()));
|
||||
} catch (\Exception $e) {
|
||||
$io->error(sprintf('An error occured when changing schedule status : "%s".', $e->getMessage()));
|
||||
$io->error(\sprintf('An error occured when changing schedule status : "%s".', $e->getMessage()));
|
||||
|
||||
return 4;
|
||||
}
|
||||
|
||||
110
src/Command/DatabaseSchemaCommand.php
Normal file
110
src/Command/DatabaseSchemaCommand.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Command;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\ScheduledDataflowRepository;
|
||||
use CodeRhapsodie\DataflowBundle\SchemaProvider\DataflowSchemaProvider;
|
||||
use Doctrine\DBAL\Schema\Comparator;
|
||||
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;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\ConfirmationQuestion;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
#[AsCommand(name: 'code-rhapsodie:dataflow:database-schema', description: 'Generates schema create / update SQL queries')]
|
||||
class DatabaseSchemaCommand extends Command
|
||||
{
|
||||
public function __construct(private ConnectionFactory $connectionFactory)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->setHelp('The <info>%command.name%</info> help you to generate SQL Query to create or update your database schema for this bundle')
|
||||
->addOption('dump-sql', null, InputOption::VALUE_NONE, 'Dump only the update SQL queries.')
|
||||
->addOption('update', null, InputOption::VALUE_NONE, 'Dump/execute only the update SQL queries.')
|
||||
->addOption('connection', null, InputOption::VALUE_REQUIRED, 'Define the DBAL connection to use');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
if ($input->getOption('connection') !== null) {
|
||||
$this->connectionFactory->setConnectionName($input->getOption('connection'));
|
||||
}
|
||||
|
||||
$connection = $this->connectionFactory->getConnection();
|
||||
|
||||
$schemaProvider = new DataflowSchemaProvider();
|
||||
$schema = $schemaProvider->createSchema();
|
||||
|
||||
$sqls = $schema->toSql($connection->getDatabasePlatform());
|
||||
|
||||
if ($input->getOption('update')) {
|
||||
$sm = $connection->createSchemaManager();
|
||||
|
||||
$tableArray = [JobRepository::TABLE_NAME, ScheduledDataflowRepository::TABLE_NAME];
|
||||
$tables = [];
|
||||
foreach ($sm->listTables() as $table) {
|
||||
/** @var Table $table */
|
||||
if (\in_array($table->getName(), $tableArray)) {
|
||||
$tables[] = $table;
|
||||
}
|
||||
}
|
||||
|
||||
$namespaces = [];
|
||||
|
||||
if ($connection->getDatabasePlatform()->supportsSchemas()) {
|
||||
$namespaces = $sm->listSchemaNames();
|
||||
}
|
||||
|
||||
$sequences = [];
|
||||
|
||||
if ($connection->getDatabasePlatform()->supportsSequences()) {
|
||||
$sequences = $sm->listSequences();
|
||||
}
|
||||
|
||||
$oldSchema = new Schema($tables, $sequences, $sm->createSchemaConfig(), $namespaces);
|
||||
|
||||
$sqls = $connection->getDatabasePlatform()->getAlterSchemaSQL((new Comparator($connection->getDatabasePlatform()))->compareSchemas($oldSchema, $schema));
|
||||
|
||||
if (empty($sqls)) {
|
||||
$io->info('There is no update SQL queries.');
|
||||
}
|
||||
}
|
||||
|
||||
if ($input->getOption('dump-sql')) {
|
||||
$io->text('Execute these SQL Queries on your database:');
|
||||
foreach ($sqls as $sql) {
|
||||
$io->text($sql.';');
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
if (!$io->askQuestion(new ConfirmationQuestion('Are you sure to update database ?', true))) {
|
||||
$io->text('Execution canceled.');
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
foreach ($sqls as $sql) {
|
||||
$connection->executeQuery($sql);
|
||||
}
|
||||
|
||||
$io->success(\sprintf('%d queries executed.', \count($sqls)));
|
||||
|
||||
return parent::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -4,45 +4,40 @@ declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Command;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\AutoUpdateCountInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
|
||||
use CodeRhapsodie\DataflowBundle\Registry\DataflowTypeRegistryInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
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;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
/**
|
||||
* Runs one dataflow.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class ExecuteDataflowCommand extends Command
|
||||
#[AsCommand('code-rhapsodie:dataflow:execute', 'Runs one dataflow type with provided options')]
|
||||
class ExecuteDataflowCommand extends Command implements LoggerAwareInterface
|
||||
{
|
||||
protected static $defaultName = 'code-rhapsodie:dataflow:execute';
|
||||
use LoggerAwareTrait;
|
||||
|
||||
/** @var DataflowTypeRegistryInterface */
|
||||
private $registry;
|
||||
|
||||
/** @var ConnectionFactory */
|
||||
private $connectionFactory;
|
||||
|
||||
public function __construct(DataflowTypeRegistryInterface $registry, ConnectionFactory $connectionFactory)
|
||||
public function __construct(private DataflowTypeRegistryInterface $registry, private ConnectionFactory $connectionFactory, private JobRepository $jobRepository)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->registry = $registry;
|
||||
$this->connectionFactory = $connectionFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->setDescription('Runs one dataflow type with provided options')
|
||||
->setHelp(<<<'EOF'
|
||||
->setHelp(
|
||||
<<<'EOF'
|
||||
The <info>%command.name%</info> command runs one dataflow with the provided options.
|
||||
|
||||
<info>php %command.full_name% App\Dataflow\MyDataflow '{"option1": "value1", "option2": "value2"}'</info>
|
||||
@@ -53,24 +48,36 @@ EOF
|
||||
->addOption('connection', null, InputOption::VALUE_REQUIRED, 'Define the DBAL connection to use');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
if (null !== $input->getOption('connection')) {
|
||||
if ($input->getOption('connection') !== null) {
|
||||
$this->connectionFactory->setConnectionName($input->getOption('connection'));
|
||||
}
|
||||
$fqcnOrAlias = $input->getArgument('fqcn');
|
||||
$options = json_decode($input->getArgument('options'), true);
|
||||
$options = json_decode($input->getArgument('options'), true, 512, \JSON_THROW_ON_ERROR);
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
$dataflowType = $this->registry->getDataflowType($fqcnOrAlias);
|
||||
if ($dataflowType instanceof AutoUpdateCountInterface) {
|
||||
$dataflowType->setRepository($this->jobRepository);
|
||||
}
|
||||
|
||||
if ($dataflowType instanceof LoggerAwareInterface && isset($this->logger)) {
|
||||
$dataflowType->setLogger($this->logger);
|
||||
}
|
||||
|
||||
$result = $dataflowType->process($options);
|
||||
|
||||
$output->writeln('Executed: '.$result->getName());
|
||||
$output->writeln('Start time: '.$result->getStartTime()->format('Y/m/d H:i:s'));
|
||||
$output->writeln('End time: '.$result->getEndTime()->format('Y/m/d H:i:s'));
|
||||
$output->writeln('Success: '.$result->getSuccessCount());
|
||||
$io->writeln('Executed: '.$result->getName());
|
||||
$io->writeln('Start time: '.$result->getStartTime()->format('Y/m/d H:i:s'));
|
||||
$io->writeln('End time: '.$result->getEndTime()->format('Y/m/d H:i:s'));
|
||||
$io->writeln('Success: '.$result->getSuccessCount());
|
||||
|
||||
if ($result->hasErrors()) {
|
||||
$io->error("Errors: {$result->getErrorCount()}\nExceptions traces are available in the logs.");
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -5,17 +5,19 @@ declare(strict_types=1);
|
||||
namespace CodeRhapsodie\DataflowBundle\Command;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Entity\Job;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
|
||||
use CodeRhapsodie\DataflowBundle\Gateway\JobGateway;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
#[AsCommand('code-rhapsodie:dataflow:job:show', 'Display job details for schedule or specific job')]
|
||||
class JobShowCommand extends Command
|
||||
{
|
||||
private const STATUS_MAPPING = [
|
||||
@@ -24,29 +26,14 @@ class JobShowCommand extends Command
|
||||
Job::STATUS_COMPLETED => 'Completed',
|
||||
];
|
||||
|
||||
protected static $defaultName = 'code-rhapsodie:dataflow:job:show';
|
||||
|
||||
/** @var JobRepository */
|
||||
private $jobRepository;
|
||||
|
||||
/** @var ConnectionFactory */
|
||||
private $connectionFactory;
|
||||
|
||||
public function __construct(JobRepository $jobRepository, ConnectionFactory $connectionFactory)
|
||||
public function __construct(private JobGateway $jobGateway, private ConnectionFactory $connectionFactory)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->jobRepository = $jobRepository;
|
||||
$this->connectionFactory = $connectionFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
protected function configure(): void
|
||||
{
|
||||
$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')
|
||||
@@ -54,12 +41,9 @@ class JobShowCommand extends Command
|
||||
->addOption('connection', null, InputOption::VALUE_REQUIRED, 'Define the DBAL connection to use');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
if (null !== $input->getOption('connection')) {
|
||||
if ($input->getOption('connection') !== null) {
|
||||
$this->connectionFactory->setConnectionName($input->getOption('connection'));
|
||||
}
|
||||
|
||||
@@ -74,16 +58,16 @@ class JobShowCommand extends Command
|
||||
}
|
||||
|
||||
if ($scheduleId) {
|
||||
$job = $this->jobRepository->findLastForDataflowId($scheduleId);
|
||||
$job = $this->jobGateway->findLastForDataflowId($scheduleId);
|
||||
} elseif ($jobId) {
|
||||
$job = $this->jobRepository->find($jobId);
|
||||
$job = $this->jobGateway->find($jobId);
|
||||
} else {
|
||||
$io->error('You must pass `job-id` or `schedule-id` option.');
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (null === $job) {
|
||||
if ($job === null) {
|
||||
$io->error('Cannot find job :/');
|
||||
|
||||
return 3;
|
||||
@@ -97,21 +81,19 @@ class JobShowCommand extends Command
|
||||
['Started at', $job->getStartTime() ? $job->getStartTime()->format('Y-m-d H:i:s') : '-'],
|
||||
['Ended at', $job->getEndTime() ? $job->getEndTime()->format('Y-m-d H:i:s') : '-'],
|
||||
['Object number', $job->getCount()],
|
||||
['Errors', count($job->getExceptions())],
|
||||
['Errors', \count((array) $job->getExceptions())],
|
||||
['Status', $this->translateStatus($job->getStatus())],
|
||||
];
|
||||
if ($input->getOption('details')) {
|
||||
$display[] = ['Type', $job->getDataflowType()];
|
||||
$display[] = ['Options', json_encode($job->getOptions())];
|
||||
$display[] = ['Options', json_encode($job->getOptions(), \JSON_THROW_ON_ERROR)];
|
||||
$io->section('Summary');
|
||||
}
|
||||
|
||||
$io->table(['Field', 'Value'], $display);
|
||||
if ($input->getOption('details')) {
|
||||
$io->section('Exceptions');
|
||||
$exceptions = array_map(function (string $exception) {
|
||||
return substr($exception, 0, 900).'…';
|
||||
}, $job->getExceptions());
|
||||
$exceptions = array_map(fn (string $exception) => substr($exception, 0, 900).'…', $job->getExceptions());
|
||||
|
||||
$io->write($exceptions);
|
||||
}
|
||||
|
||||
@@ -4,62 +4,43 @@ declare(strict_types=1);
|
||||
|
||||
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;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
|
||||
|
||||
/**
|
||||
* Runs dataflows according to user-defined schedule.
|
||||
*
|
||||
* @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';
|
||||
|
||||
/** @var ScheduledDataflowManagerInterface */
|
||||
private $manager;
|
||||
|
||||
/** @var PendingDataflowRunnerInterface */
|
||||
private $runner;
|
||||
|
||||
/** @var ConnectionFactory */
|
||||
private $connectionFactory;
|
||||
|
||||
public function __construct(ScheduledDataflowManagerInterface $manager, PendingDataflowRunnerInterface $runner, ConnectionFactory $connectionFactory)
|
||||
public function __construct(private ScheduledDataflowManagerInterface $manager, private PendingDataflowRunnerInterface $runner, private ConnectionFactory $connectionFactory)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->manager = $manager;
|
||||
$this->runner = $runner;
|
||||
$this->connectionFactory = $connectionFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->setDescription('Runs dataflows based on the scheduled defined in the UI.')
|
||||
->setHelp(<<<'EOF'
|
||||
->setHelp(
|
||||
<<<'EOF'
|
||||
The <info>%command.name%</info> command runs dataflows according to the schedule defined in the UI by the user.
|
||||
EOF
|
||||
)
|
||||
->addOption('connection', null, InputOption::VALUE_REQUIRED, 'Define the DBAL connection to use');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
if (!$this->lock()) {
|
||||
$output->writeln('The command is already running in another process.');
|
||||
@@ -67,7 +48,7 @@ EOF
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (null !== $input->getOption('connection')) {
|
||||
if ($input->getOption('connection') !== null) {
|
||||
$this->connectionFactory->setConnectionName($input->getOption('connection'));
|
||||
}
|
||||
|
||||
|
||||
@@ -4,52 +4,36 @@ declare(strict_types=1);
|
||||
|
||||
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;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
#[AsCommand('code-rhapsodie:dataflow:schedule:list', 'List scheduled dataflows')]
|
||||
class ScheduleListCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'code-rhapsodie:dataflow:schedule:list';
|
||||
|
||||
/** @var ScheduledDataflowRepository */
|
||||
private $scheduledDataflowRepository;
|
||||
|
||||
/** @var ConnectionFactory */
|
||||
private $connectionFactory;
|
||||
|
||||
public function __construct(ScheduledDataflowRepository $scheduledDataflowRepository, ConnectionFactory $connectionFactory)
|
||||
public function __construct(private ScheduledDataflowRepository $scheduledDataflowRepository, private ConnectionFactory $connectionFactory)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->scheduledDataflowRepository = $scheduledDataflowRepository;
|
||||
$this->connectionFactory = $connectionFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
protected function configure(): void
|
||||
{
|
||||
$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');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
if (null !== $input->getOption('connection')) {
|
||||
if ($input->getOption('connection') !== null) {
|
||||
$this->connectionFactory->setConnectionName($input->getOption('connection'));
|
||||
}
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
@@ -4,96 +4,51 @@ declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Command;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
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\ArrayInput;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @deprecated This command is deprecated and will be removed in 6.0, use this command "code-rhapsodie:dataflow:database-schema" instead.
|
||||
*/
|
||||
#[AsCommand('code-rhapsodie:dataflow:dump-schema', 'Generates schema create / update SQL queries')]
|
||||
class SchemaCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'code-rhapsodie:dataflow:dump-schema';
|
||||
|
||||
/** @var ConnectionFactory */
|
||||
private $connectionFactory;
|
||||
|
||||
public function __construct(ConnectionFactory $connectionFactory)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->connectionFactory = $connectionFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
protected function configure(): void
|
||||
{
|
||||
$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')
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
if (null !== $input->getOption('connection')) {
|
||||
$this->connectionFactory->setConnectionName($input->getOption('connection'));
|
||||
}
|
||||
|
||||
$connection = $this->connectionFactory->getConnection();
|
||||
|
||||
$schemaProvider = new DataflowSchemaProvider();
|
||||
$schema = $schemaProvider->createSchema();
|
||||
|
||||
$sqls = $schema->toSql($connection->getDatabasePlatform());
|
||||
|
||||
if ($input->getOption('update')) {
|
||||
$sm = $connection->getSchemaManager();
|
||||
|
||||
$tableArray = [JobRepository::TABLE_NAME, ScheduledDataflowRepository::TABLE_NAME];
|
||||
$tables = [];
|
||||
foreach ($sm->listTables() as $table) {
|
||||
/** @var Table $table */
|
||||
if (in_array($table->getName(), $tableArray)) {
|
||||
$tables[] = $table;
|
||||
}
|
||||
}
|
||||
|
||||
$namespaces = [];
|
||||
|
||||
if ($connection->getDatabasePlatform()->supportsSchemas()) {
|
||||
$namespaces = $sm->listNamespaceNames();
|
||||
}
|
||||
|
||||
$sequences = [];
|
||||
|
||||
if ($connection->getDatabasePlatform()->supportsSequences()) {
|
||||
$sequences = $sm->listSequences();
|
||||
}
|
||||
|
||||
$oldSchema = new Schema($tables, $sequences, $sm->createSchemaConfig(), $namespaces);
|
||||
|
||||
$sqls = $schema->getMigrateFromSql($oldSchema, $connection->getDatabasePlatform());
|
||||
}
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$io->text('Execute these SQL Queries on your database:');
|
||||
foreach ($sqls as $sql) {
|
||||
$io->text($sql);
|
||||
}
|
||||
$io->warning('This command is deprecated and will be removed in 6.0, use this command "code-rhapsodie:dataflow:database-schema" instead.');
|
||||
|
||||
$options = array_filter($input->getOptions());
|
||||
|
||||
// add -- before each keys
|
||||
$options = array_combine(
|
||||
array_map(fn ($key) => '--'.$key, array_keys($options)),
|
||||
array_values($options)
|
||||
);
|
||||
|
||||
$options['--dump-sql'] = true;
|
||||
|
||||
$inputArray = new ArrayInput([
|
||||
'command' => 'code-rhapsodie:dataflow:database-schema',
|
||||
...$options,
|
||||
]);
|
||||
|
||||
return $this->getApplication()->doRun($inputArray, $output);
|
||||
}
|
||||
}
|
||||
|
||||
69
src/DataflowType/AMPAsyncDataflowBuilder.php
Normal file
69
src/DataflowType/AMPAsyncDataflowBuilder.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\DataflowType;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Dataflow\AMPAsyncDataflow;
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Dataflow\DataflowInterface;
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Writer\WriterInterface;
|
||||
|
||||
class AMPAsyncDataflowBuilder extends DataflowBuilder
|
||||
{
|
||||
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;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setReader(iterable $reader): self
|
||||
{
|
||||
$this->reader = $reader;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addStep(callable $step, int $priority = 0, int $scale = 1): self
|
||||
{
|
||||
$this->steps[$priority][] = ['step' => $step, 'scale' => $scale];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addWriter(WriterInterface $writer): self
|
||||
{
|
||||
$this->writers[] = $writer;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDataflow(): DataflowInterface
|
||||
{
|
||||
$dataflow = new AMPAsyncDataflow($this->reader, $this->name, $this->loopInterval, $this->emitInterval);
|
||||
|
||||
krsort($this->steps);
|
||||
foreach ($this->steps as $stepArray) {
|
||||
foreach ($stepArray as $step) {
|
||||
$dataflow->addStep($step['step'], $step['scale']);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->writers as $writer) {
|
||||
$dataflow->addWriter($writer);
|
||||
}
|
||||
|
||||
return $dataflow;
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,20 @@ declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\DataflowType;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
use Psr\Log\LoggerAwareInterface;
|
||||
use Psr\Log\LoggerAwareTrait;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
abstract class AbstractDataflowType implements DataflowTypeInterface
|
||||
abstract class AbstractDataflowType implements DataflowTypeInterface, LoggerAwareInterface, AutoUpdateCountInterface
|
||||
{
|
||||
use LoggerAwareTrait;
|
||||
|
||||
private JobRepository $repository;
|
||||
|
||||
private ?\DateTime $saveDate = null;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
@@ -16,21 +26,43 @@ abstract class AbstractDataflowType implements DataflowTypeInterface
|
||||
return [];
|
||||
}
|
||||
|
||||
public function process(array $options): Result
|
||||
public function process(array $options, ?int $jobId = null): Result
|
||||
{
|
||||
$this->saveDate = new \DateTime('+1 minute');
|
||||
|
||||
$optionsResolver = new OptionsResolver();
|
||||
$this->configureOptions($optionsResolver);
|
||||
$options = $optionsResolver->resolve($options);
|
||||
|
||||
$builder = (new DataflowBuilder())
|
||||
->setName($this->getLabel())
|
||||
;
|
||||
$builder = $this->createDataflowBuilder();
|
||||
$builder->setName($this->getLabel());
|
||||
$builder->addAfterItemProcessor(function (int|string $index, mixed $item, int $count) use ($jobId) {
|
||||
if ($jobId === null || $this->saveDate > new \DateTime()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->repository->updateCount($jobId, $count);
|
||||
$this->saveDate = new \DateTime('+1 minute');
|
||||
});
|
||||
$this->buildDataflow($builder, $options);
|
||||
$dataflow = $builder->getDataflow();
|
||||
if ($dataflow instanceof LoggerAwareInterface && $this->logger instanceof LoggerInterface) {
|
||||
$dataflow->setLogger($this->logger);
|
||||
}
|
||||
|
||||
return $dataflow->process();
|
||||
}
|
||||
|
||||
public function setRepository(JobRepository $repository): void
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
protected function createDataflowBuilder(): DataflowBuilder
|
||||
{
|
||||
return new DataflowBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
|
||||
12
src/DataflowType/AutoUpdateCountInterface.php
Normal file
12
src/DataflowType/AutoUpdateCountInterface.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\DataflowType;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
|
||||
interface AutoUpdateCountInterface
|
||||
{
|
||||
public function setRepository(JobRepository $repository): void;
|
||||
}
|
||||
162
src/DataflowType/Dataflow/AMPAsyncDataflow.php
Normal file
162
src/DataflowType/Dataflow/AMPAsyncDataflow.php
Normal file
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\DataflowType\Dataflow;
|
||||
|
||||
use function Amp\coroutine;
|
||||
|
||||
use Amp\Deferred;
|
||||
use Amp\Delayed;
|
||||
use Amp\Loop;
|
||||
use Amp\Producer;
|
||||
use Amp\Promise;
|
||||
|
||||
use function Amp\Promise\wait;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Result;
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Writer\WriterInterface;
|
||||
use Psr\Log\LoggerAwareInterface;
|
||||
use Psr\Log\LoggerAwareTrait;
|
||||
|
||||
class AMPAsyncDataflow implements DataflowInterface, LoggerAwareInterface
|
||||
{
|
||||
use LoggerAwareTrait;
|
||||
|
||||
/** @var callable[] */
|
||||
private array $steps = [];
|
||||
|
||||
/** @var WriterInterface[] */
|
||||
private array $writers = [];
|
||||
|
||||
private array $states = [];
|
||||
|
||||
private array $stepsJobs = [];
|
||||
|
||||
public function __construct(private iterable $reader, private ?string $name, private ?int $loopInterval = 0, private ?int $emitInterval = 0)
|
||||
{
|
||||
if (!\function_exists('Amp\\Promise\\wait')) {
|
||||
throw new \RuntimeException('Amp is not loaded. Suggest install it with composer require amphp/amp');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $scale
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addStep(callable $step, $scale = 1): self
|
||||
{
|
||||
$this->steps[] = [$step, $scale];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function addWriter(WriterInterface $writer): self
|
||||
{
|
||||
$this->writers[] = $writer;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function process(): Result
|
||||
{
|
||||
$count = 0;
|
||||
$exceptions = [];
|
||||
$startTime = new \DateTime();
|
||||
|
||||
try {
|
||||
foreach ($this->writers as $writer) {
|
||||
$writer->prepare();
|
||||
}
|
||||
|
||||
$deferred = new Deferred();
|
||||
$resolved = false; // missing $deferred->isResolved() in version 2.5
|
||||
$producer = new Producer(function (callable $emit) {
|
||||
foreach ($this->reader as $index => $item) {
|
||||
yield new Delayed($this->emitInterval);
|
||||
yield $emit([$index, $item]);
|
||||
}
|
||||
});
|
||||
|
||||
$watcherId = Loop::repeat($this->loopInterval, function () use ($deferred, &$resolved, $producer, &$count, &$exceptions) {
|
||||
if (yield $producer->advance()) {
|
||||
$it = $producer->getCurrent();
|
||||
[$index, $item] = $it;
|
||||
$this->states[$index] = [$index, 0, $item];
|
||||
} elseif (!$resolved && \count($this->states) === 0) {
|
||||
$resolved = true;
|
||||
$deferred->resolve();
|
||||
}
|
||||
|
||||
foreach ($this->states as $state) {
|
||||
$this->processState($state, $count, $exceptions);
|
||||
}
|
||||
});
|
||||
|
||||
wait($deferred->promise());
|
||||
Loop::cancel($watcherId);
|
||||
|
||||
foreach ($this->writers as $writer) {
|
||||
$writer->finish();
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$exceptions[] = $e;
|
||||
$this->logException($e);
|
||||
}
|
||||
|
||||
return new Result($this->name, $startTime, new \DateTime(), $count, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $count internal count reference
|
||||
* @param array $exceptions internal exceptions
|
||||
*/
|
||||
private function processState(mixed $state, int &$count, array &$exceptions): void
|
||||
{
|
||||
[$readIndex, $stepIndex, $item] = $state;
|
||||
if ($stepIndex < \count($this->steps)) {
|
||||
if (!isset($this->stepsJobs[$stepIndex])) {
|
||||
$this->stepsJobs[$stepIndex] = [];
|
||||
}
|
||||
[$step, $scale] = $this->steps[$stepIndex];
|
||||
if ((is_countable($this->stepsJobs[$stepIndex]) ? \count($this->stepsJobs[$stepIndex]) : 0) < $scale && !isset($this->stepsJobs[$stepIndex][$readIndex])) {
|
||||
$this->stepsJobs[$stepIndex][$readIndex] = true;
|
||||
/** @var Promise<void> $promise */
|
||||
$promise = coroutine($step)($item);
|
||||
$promise->onResolve(function (?\Throwable $exception = null, $newItem = null) use ($stepIndex, $readIndex, &$exceptions) {
|
||||
if ($exception) {
|
||||
$exceptions[$stepIndex] = $exception;
|
||||
$this->logException($exception, (string) $stepIndex);
|
||||
} elseif ($newItem === false) {
|
||||
unset($this->states[$readIndex]);
|
||||
} else {
|
||||
$this->states[$readIndex] = [$readIndex, $stepIndex + 1, $newItem];
|
||||
}
|
||||
|
||||
unset($this->stepsJobs[$stepIndex][$readIndex]);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
unset($this->states[$readIndex]);
|
||||
|
||||
foreach ($this->writers as $writer) {
|
||||
$writer->write($item);
|
||||
}
|
||||
|
||||
++$count;
|
||||
}
|
||||
}
|
||||
|
||||
private function logException(\Throwable $e, ?string $index = null): void
|
||||
{
|
||||
if (!isset($this->logger)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->logger->error($e, ['exception' => $e, 'index' => $index]);
|
||||
}
|
||||
}
|
||||
@@ -6,34 +6,33 @@ namespace CodeRhapsodie\DataflowBundle\DataflowType\Dataflow;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Result;
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Writer\WriterInterface;
|
||||
use Psr\Log\LoggerAwareInterface;
|
||||
use Psr\Log\LoggerAwareTrait;
|
||||
|
||||
class Dataflow implements DataflowInterface
|
||||
class Dataflow implements DataflowInterface, LoggerAwareInterface
|
||||
{
|
||||
/** @var string */
|
||||
private $name;
|
||||
|
||||
/** @var iterable */
|
||||
private $reader;
|
||||
use LoggerAwareTrait;
|
||||
|
||||
/** @var callable[] */
|
||||
private $steps = [];
|
||||
private array $steps = [];
|
||||
|
||||
/** @var WriterInterface[] */
|
||||
private $writers = [];
|
||||
private array $writers = [];
|
||||
|
||||
private ?\Closure $customExceptionIndex = null;
|
||||
|
||||
private ?\DateTimeInterface $dateTime = null;
|
||||
|
||||
/**
|
||||
* @param iterable $reader
|
||||
* @param string|null $name
|
||||
* @var \Closure[]
|
||||
*/
|
||||
public function __construct(iterable $reader, ?string $name)
|
||||
private array $afterItemProcessors = [];
|
||||
|
||||
public function __construct(private iterable $reader, private ?string $name)
|
||||
{
|
||||
$this->reader = $reader;
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable $step
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addStep(callable $step): self
|
||||
@@ -44,8 +43,6 @@ class Dataflow implements DataflowInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WriterInterface $writer
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addWriter(WriterInterface $writer): self
|
||||
@@ -56,44 +53,77 @@ class Dataflow implements DataflowInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @return $this
|
||||
*/
|
||||
public function setCustomExceptionIndex(callable $callable): self
|
||||
{
|
||||
$this->customExceptionIndex = \Closure::fromCallable($callable);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<callable> $processors
|
||||
*/
|
||||
public function setAfterItemProcessors(array $processors): self
|
||||
{
|
||||
$this->afterItemProcessors = array_map(fn (callable $callable) => \Closure::fromCallable($callable), $processors);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function process(): Result
|
||||
{
|
||||
$count = 0;
|
||||
$exceptions = [];
|
||||
$startTime = new \DateTime();
|
||||
|
||||
foreach ($this->writers as $writer) {
|
||||
$writer->prepare();
|
||||
}
|
||||
|
||||
foreach ($this->reader as $index => $item) {
|
||||
try {
|
||||
$this->processItem($item);
|
||||
} catch (\Exception $e) {
|
||||
$exceptions[$index] = $e;
|
||||
try {
|
||||
foreach ($this->writers as $writer) {
|
||||
$writer->prepare();
|
||||
}
|
||||
|
||||
++$count;
|
||||
}
|
||||
foreach ($this->reader as $index => $item) {
|
||||
try {
|
||||
$this->processItem($item);
|
||||
} catch (\Throwable $e) {
|
||||
$exceptionIndex = $index;
|
||||
try {
|
||||
if (\is_callable($this->customExceptionIndex)) {
|
||||
$exceptionIndex = (string) ($this->customExceptionIndex)($item, $index);
|
||||
}
|
||||
} catch (\Throwable $e2) {
|
||||
$exceptions[$index] = $e2;
|
||||
$this->logException($e2, $index);
|
||||
}
|
||||
$exceptions[$exceptionIndex] = $e;
|
||||
$this->logException($e, $exceptionIndex);
|
||||
}
|
||||
|
||||
foreach ($this->writers as $writer) {
|
||||
$writer->finish();
|
||||
++$count;
|
||||
|
||||
foreach ($this->afterItemProcessors as $afterItemProcessor) {
|
||||
$afterItemProcessor($index, $item, $count);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->writers as $writer) {
|
||||
$writer->finish();
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$exceptions[] = $e;
|
||||
$this->logException($e);
|
||||
}
|
||||
|
||||
return new Result($this->name, $startTime, new \DateTime(), $count, $exceptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $item
|
||||
*/
|
||||
private function processItem($item): void
|
||||
private function processItem(mixed $item): void
|
||||
{
|
||||
foreach ($this->steps as $step) {
|
||||
$item = call_user_func($step, $item);
|
||||
$item = \call_user_func($step, $item);
|
||||
|
||||
if (false === $item) {
|
||||
if ($item === false) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -102,4 +132,13 @@ class Dataflow implements DataflowInterface
|
||||
$writer->write($item);
|
||||
}
|
||||
}
|
||||
|
||||
private function logException(\Throwable $e, string|int|null $index = null): void
|
||||
{
|
||||
if (!isset($this->logger)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->logger->error($e, ['exception' => $e, 'index' => $index]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,8 +13,6 @@ interface DataflowInterface
|
||||
{
|
||||
/**
|
||||
* Processes the data.
|
||||
*
|
||||
* @return Result
|
||||
*/
|
||||
public function process(): Result;
|
||||
}
|
||||
|
||||
@@ -10,17 +10,18 @@ use CodeRhapsodie\DataflowBundle\DataflowType\Writer\WriterInterface;
|
||||
|
||||
class DataflowBuilder
|
||||
{
|
||||
/** @var string */
|
||||
private $name;
|
||||
|
||||
/** @var iterable */
|
||||
private $reader;
|
||||
|
||||
/** @var array */
|
||||
private $steps = [];
|
||||
private ?string $name = null;
|
||||
private ?iterable $reader = null;
|
||||
private array $steps = [];
|
||||
|
||||
/** @var WriterInterface[] */
|
||||
private $writers = [];
|
||||
private array $writers = [];
|
||||
|
||||
private ?\Closure $customExceptionIndex = null;
|
||||
/**
|
||||
* @var \Closure[]
|
||||
*/
|
||||
private array $afterItemProcessors = [];
|
||||
|
||||
public function setName(string $name): self
|
||||
{
|
||||
@@ -50,6 +51,20 @@ class DataflowBuilder
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setCustomExceptionIndex(callable $callable): self
|
||||
{
|
||||
$this->customExceptionIndex = \Closure::fromCallable($callable);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addAfterItemProcessor(callable $callable): self
|
||||
{
|
||||
$this->afterItemProcessors[] = \Closure::fromCallable($callable);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDataflow(): DataflowInterface
|
||||
{
|
||||
$dataflow = new Dataflow($this->reader, $this->name);
|
||||
@@ -65,6 +80,12 @@ class DataflowBuilder
|
||||
$dataflow->addWriter($writer);
|
||||
}
|
||||
|
||||
if (\is_callable($this->customExceptionIndex)) {
|
||||
$dataflow->setCustomExceptionIndex($this->customExceptionIndex);
|
||||
}
|
||||
|
||||
$dataflow->setAfterItemProcessors($this->afterItemProcessors);
|
||||
|
||||
return $dataflow;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ interface DataflowTypeInterface
|
||||
|
||||
public function getAliases(): iterable;
|
||||
|
||||
public function process(array $options): Result;
|
||||
public function process(array $options, ?int $jobId = null): Result;
|
||||
}
|
||||
|
||||
@@ -9,116 +9,62 @@ namespace CodeRhapsodie\DataflowBundle\DataflowType;
|
||||
*/
|
||||
class Result
|
||||
{
|
||||
/** @var string */
|
||||
private $name;
|
||||
private \DateInterval $elapsed;
|
||||
|
||||
/** @var \DateTimeInterface */
|
||||
private $startTime;
|
||||
private int $errorCount = 0;
|
||||
|
||||
/** @var \DateTimeInterface */
|
||||
private $endTime;
|
||||
private int $successCount = 0;
|
||||
|
||||
/** @var \DateInterval */
|
||||
private $elapsed;
|
||||
private array $exceptions;
|
||||
|
||||
/** @var int */
|
||||
private $errorCount = 0;
|
||||
|
||||
/** @var int */
|
||||
private $successCount = 0;
|
||||
|
||||
/** @var int */
|
||||
private $totalProcessedCount = 0;
|
||||
|
||||
/** @var array */
|
||||
private $exceptions;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param \DateTimeInterface $startTime
|
||||
* @param \DateTimeInterface $endTime
|
||||
* @param int $totalCount
|
||||
* @param \SplObjectStorage $exceptions
|
||||
*/
|
||||
public function __construct(string $name, \DateTimeInterface $startTime, \DateTimeInterface $endTime, int $totalCount, array $exceptions)
|
||||
public function __construct(private string $name, private \DateTimeInterface $startTime, private \DateTimeInterface $endTime, private int $totalProcessedCount, array $exceptions)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->startTime = $startTime;
|
||||
$this->endTime = $endTime;
|
||||
$this->elapsed = $startTime->diff($endTime);
|
||||
$this->totalProcessedCount = $totalCount;
|
||||
$this->errorCount = count($exceptions);
|
||||
$this->successCount = $totalCount - $this->errorCount;
|
||||
$this->errorCount = \count($exceptions);
|
||||
$this->successCount = $totalProcessedCount - $this->errorCount;
|
||||
$this->exceptions = $exceptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \DateTimeInterface
|
||||
*/
|
||||
public function getStartTime(): \DateTimeInterface
|
||||
{
|
||||
return $this->startTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \DateTimeInterface
|
||||
*/
|
||||
public function getEndTime(): \DateTimeInterface
|
||||
{
|
||||
return $this->endTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \DateInterval
|
||||
*/
|
||||
public function getElapsed(): \DateInterval
|
||||
{
|
||||
return $this->elapsed;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getErrorCount(): int
|
||||
{
|
||||
return $this->errorCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getSuccessCount(): int
|
||||
{
|
||||
return $this->successCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getTotalProcessedCount(): int
|
||||
{
|
||||
return $this->totalProcessedCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasErrors(): bool
|
||||
{
|
||||
return $this->errorCount > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getExceptions(): array
|
||||
{
|
||||
return $this->exceptions;
|
||||
|
||||
46
src/DataflowType/Writer/CollectionWriter.php
Normal file
46
src/DataflowType/Writer/CollectionWriter.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\DataflowType\Writer;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Exceptions\UnsupportedItemTypeException;
|
||||
|
||||
/**
|
||||
* Delegates the writing of each item in a collection to an embedded writer.
|
||||
*/
|
||||
class CollectionWriter implements DelegateWriterInterface
|
||||
{
|
||||
/**
|
||||
* CollectionWriter constructor.
|
||||
*/
|
||||
public function __construct(private WriterInterface $writer)
|
||||
{
|
||||
}
|
||||
|
||||
public function prepare()
|
||||
{
|
||||
$this->writer->prepare();
|
||||
}
|
||||
|
||||
public function write($collection)
|
||||
{
|
||||
if (!is_iterable($collection)) {
|
||||
throw new UnsupportedItemTypeException(\sprintf('Item to write was expected to be an iterable, received %s.', get_debug_type($collection)));
|
||||
}
|
||||
|
||||
foreach ($collection as $item) {
|
||||
$this->writer->write($item);
|
||||
}
|
||||
}
|
||||
|
||||
public function finish()
|
||||
{
|
||||
$this->writer->finish();
|
||||
}
|
||||
|
||||
public function supports($item): bool
|
||||
{
|
||||
return is_iterable($item);
|
||||
}
|
||||
}
|
||||
16
src/DataflowType/Writer/DelegateWriterInterface.php
Normal file
16
src/DataflowType/Writer/DelegateWriterInterface.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\DataflowType\Writer;
|
||||
|
||||
/**
|
||||
* A writer that can be used as a delegate of DelegatorWriter.
|
||||
*/
|
||||
interface DelegateWriterInterface extends WriterInterface
|
||||
{
|
||||
/**
|
||||
* Returns true if the argument is of a supported type.
|
||||
*/
|
||||
public function supports($item): bool;
|
||||
}
|
||||
83
src/DataflowType/Writer/DelegatorWriter.php
Normal file
83
src/DataflowType/Writer/DelegatorWriter.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\DataflowType\Writer;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Exceptions\UnsupportedItemTypeException;
|
||||
|
||||
/**
|
||||
* Writer that delegated the actual writing to other writers.
|
||||
*/
|
||||
class DelegatorWriter implements DelegateWriterInterface
|
||||
{
|
||||
/** @var DelegateWriterInterface[] */
|
||||
private array $delegates = [];
|
||||
|
||||
/**
|
||||
* DelegatorWriter constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function prepare()
|
||||
{
|
||||
foreach ($this->delegates as $delegate) {
|
||||
$delegate->prepare();
|
||||
}
|
||||
}
|
||||
|
||||
public function write($item)
|
||||
{
|
||||
foreach ($this->delegates as $delegate) {
|
||||
if (!$delegate->supports($item)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$delegate->write($item);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
throw new UnsupportedItemTypeException(\sprintf('None of the registered delegate writers support the received item of type %s', get_debug_type($item)));
|
||||
}
|
||||
|
||||
public function finish()
|
||||
{
|
||||
foreach ($this->delegates as $delegate) {
|
||||
$delegate->finish();
|
||||
}
|
||||
}
|
||||
|
||||
public function supports($item): bool
|
||||
{
|
||||
foreach ($this->delegates as $delegate) {
|
||||
if ($delegate->supports($item)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a collection of delegates.
|
||||
*
|
||||
* @param iterable|DelegateWriterInterface[] $delegates
|
||||
*/
|
||||
public function addDelegates(iterable $delegates): void
|
||||
{
|
||||
foreach ($delegates as $delegate) {
|
||||
$this->addDelegate($delegate);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers one delegate.
|
||||
*/
|
||||
public function addDelegate(DelegateWriterInterface $delegate): void
|
||||
{
|
||||
$this->delegates[] = $delegate;
|
||||
}
|
||||
}
|
||||
@@ -6,12 +6,8 @@ namespace CodeRhapsodie\DataflowBundle\DataflowType\Writer;
|
||||
|
||||
class PortWriterAdapter implements WriterInterface
|
||||
{
|
||||
/** @var \Port\Writer */
|
||||
private $writer;
|
||||
|
||||
public function __construct(\Port\Writer $writer)
|
||||
public function __construct(private \Port\Writer $writer)
|
||||
{
|
||||
$this->writer = $writer;
|
||||
}
|
||||
|
||||
public function prepare()
|
||||
|
||||
@@ -4,11 +4,23 @@ declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\DataflowType\Writer;
|
||||
|
||||
/**
|
||||
* Represents a writer for dataflows.
|
||||
*/
|
||||
interface WriterInterface
|
||||
{
|
||||
/**
|
||||
* Called before the dataflow is processed.
|
||||
*/
|
||||
public function prepare();
|
||||
|
||||
public function write($item);
|
||||
/**
|
||||
* Write an item.
|
||||
*/
|
||||
public function write(mixed $item);
|
||||
|
||||
/**
|
||||
* Called after the dataflow is processed.
|
||||
*/
|
||||
public function finish();
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
||||
*/
|
||||
class CodeRhapsodieDataflowExtension extends Extension
|
||||
{
|
||||
public function load(array $configs, ContainerBuilder $container)
|
||||
public function load(array $configs, ContainerBuilder $container): void
|
||||
{
|
||||
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
||||
$loader->load('services.yaml');
|
||||
@@ -28,5 +28,17 @@ class CodeRhapsodieDataflowExtension extends Extension
|
||||
$config = $this->processConfiguration($configuration, $configs);
|
||||
|
||||
$container->setParameter('coderhapsodie.dataflow.dbal_default_connection', $config['dbal_default_connection']);
|
||||
$container->setParameter('coderhapsodie.dataflow.default_logger', $config['default_logger']);
|
||||
$container->setParameter('coderhapsodie.dataflow.exceptions_mode.type', $config['exceptions_mode']['type']);
|
||||
|
||||
if ($config['exceptions_mode']['type'] === 'file') {
|
||||
$container->setParameter('coderhapsodie.dataflow.flysystem_service', $config['exceptions_mode']['flysystem_service']);
|
||||
$loader->load('exceptions_services.yaml');
|
||||
}
|
||||
|
||||
if ($config['messenger_mode']['enabled']) {
|
||||
$container->setParameter('coderhapsodie.dataflow.bus', $config['messenger_mode']['bus']);
|
||||
$loader->load('messenger_services.yaml');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
33
src/DependencyInjection/Compiler/BusCompilerPass.php
Normal file
33
src/DependencyInjection/Compiler/BusCompilerPass.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\DependencyInjection\Compiler;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Runner\MessengerDataflowRunner;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
class BusCompilerPass implements CompilerPassInterface
|
||||
{
|
||||
public function process(ContainerBuilder $container): void
|
||||
{
|
||||
if (!$container->hasParameter('coderhapsodie.dataflow.bus')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$bus = $container->getParameter('coderhapsodie.dataflow.bus');
|
||||
if (!$container->has($bus)) {
|
||||
throw new InvalidArgumentException(\sprintf('Service "%s" not found', $bus));
|
||||
}
|
||||
|
||||
if (!$container->has(MessengerDataflowRunner::class)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$definition = $container->findDefinition(MessengerDataflowRunner::class);
|
||||
$definition->setArgument('$bus', new Reference($bus));
|
||||
}
|
||||
}
|
||||
@@ -16,10 +16,7 @@ use Symfony\Component\DependencyInjection\Reference;
|
||||
*/
|
||||
class DataflowTypeCompilerPass implements CompilerPassInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function process(ContainerBuilder $container)
|
||||
public function process(ContainerBuilder $container): void
|
||||
{
|
||||
if (!$container->has(DataflowTypeRegistry::class)) {
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\DependencyInjection\Compiler;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Command\ExecuteDataflowCommand;
|
||||
use CodeRhapsodie\DataflowBundle\Processor\JobProcessor;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
class DefaultLoggerCompilerPass implements CompilerPassInterface
|
||||
{
|
||||
public function process(ContainerBuilder $container): void
|
||||
{
|
||||
$defaultLogger = $container->getParameter('coderhapsodie.dataflow.default_logger');
|
||||
if (!$container->has($defaultLogger)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ([ExecuteDataflowCommand::class, JobProcessor::class] as $serviceId) {
|
||||
if (!$container->has($serviceId)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$definition = $container->findDefinition($serviceId);
|
||||
$definition->addMethodCall('setLogger', [new Reference($defaultLogger)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
29
src/DependencyInjection/Compiler/ExceptionCompilerPass.php
Normal file
29
src/DependencyInjection/Compiler/ExceptionCompilerPass.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\DependencyInjection\Compiler;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\ExceptionsHandler\FilesystemExceptionHandler;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
class ExceptionCompilerPass implements CompilerPassInterface
|
||||
{
|
||||
public function process(ContainerBuilder $container): void
|
||||
{
|
||||
if (!$container->hasParameter('coderhapsodie.dataflow.flysystem_service')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$flysystem = $container->getParameter('coderhapsodie.dataflow.flysystem_service');
|
||||
if (!$container->has($flysystem)) {
|
||||
throw new InvalidArgumentException(\sprintf('Service "%s" not found', $flysystem));
|
||||
}
|
||||
|
||||
$definition = $container->findDefinition(FilesystemExceptionHandler::class);
|
||||
$definition->setArgument('$filesystem', new Reference($flysystem));
|
||||
}
|
||||
}
|
||||
@@ -6,19 +6,52 @@ namespace CodeRhapsodie\DataflowBundle\DependencyInjection;
|
||||
|
||||
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||
use Symfony\Component\Config\Definition\ConfigurationInterface;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
|
||||
class Configuration implements ConfigurationInterface
|
||||
{
|
||||
public function getConfigTreeBuilder()
|
||||
public function getConfigTreeBuilder(): TreeBuilder
|
||||
{
|
||||
$treeBuilder = new TreeBuilder();
|
||||
$rootNode = $treeBuilder->root('code_rhapsodie_dataflow');
|
||||
$treeBuilder = new TreeBuilder('code_rhapsodie_dataflow');
|
||||
$rootNode = $treeBuilder->getRootNode();
|
||||
|
||||
$rootNode
|
||||
->children()
|
||||
->scalarNode('dbal_default_connection')
|
||||
->defaultValue('default')
|
||||
->end()
|
||||
->scalarNode('default_logger')
|
||||
->defaultValue('logger')
|
||||
->end()
|
||||
->arrayNode('messenger_mode')
|
||||
->addDefaultsIfNotSet()
|
||||
->children()
|
||||
->booleanNode('enabled')
|
||||
->defaultFalse()
|
||||
->end()
|
||||
->scalarNode('bus')
|
||||
->defaultValue('messenger.default_bus')
|
||||
->end()
|
||||
->end()
|
||||
->validate()
|
||||
->ifTrue(static fn ($v): bool => $v['enabled'] && !interface_exists(MessageBusInterface::class))
|
||||
->thenInvalid('You need "symfony/messenger" in order to use Dataflow messenger mode.')
|
||||
->end()
|
||||
->end()
|
||||
->arrayNode('exceptions_mode')
|
||||
->addDefaultsIfNotSet()
|
||||
->children()
|
||||
->scalarNode('type')
|
||||
->defaultValue('database')
|
||||
->end()
|
||||
->scalarNode('flysystem_service')
|
||||
->end()
|
||||
->end()
|
||||
->validate()
|
||||
->ifTrue(static fn ($v): bool => $v['type'] === 'file' && !class_exists('\League\Flysystem\Filesystem'))
|
||||
->thenInvalid('You need "league/flysystem" to use Dataflow file exception mode.')
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
;
|
||||
|
||||
|
||||
@@ -13,9 +13,10 @@ use Symfony\Component\Validator\Constraints as Asserts;
|
||||
*/
|
||||
class Job
|
||||
{
|
||||
const STATUS_PENDING = 0;
|
||||
const STATUS_RUNNING = 1;
|
||||
const STATUS_COMPLETED = 2;
|
||||
public const STATUS_PENDING = 0;
|
||||
public const STATUS_RUNNING = 1;
|
||||
public const STATUS_COMPLETED = 2;
|
||||
public const STATUS_QUEUED = 3;
|
||||
|
||||
private const KEYS = [
|
||||
'id',
|
||||
@@ -31,78 +32,35 @@ class Job
|
||||
'end_time',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
private $id;
|
||||
private ?int $id = null;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*
|
||||
* @Asserts\Range(min=0, max=2)
|
||||
*/
|
||||
private $status;
|
||||
#[Asserts\Range(min: 0, max: 2)]
|
||||
private int $status = self::STATUS_PENDING;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*
|
||||
* @Asserts\NotBlank()
|
||||
* @Asserts\Length(min=1, max=255)
|
||||
* @Asserts\Regex("#^[[:alnum:] ]+\z#u")
|
||||
*/
|
||||
private $label;
|
||||
#[Asserts\NotBlank]
|
||||
#[Asserts\Length(min: 1, max: 255)]
|
||||
#[Asserts\Regex('#^[[:alnum:] ]+\z#u')]
|
||||
private ?string $label = null;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*
|
||||
* @Asserts\NotBlank()
|
||||
* @Asserts\Length(min=1, max=255)
|
||||
* @Asserts\Regex("#^[[:alnum:]\\]+\z#u")
|
||||
*/
|
||||
private $dataflowType;
|
||||
#[Asserts\NotBlank]
|
||||
#[Asserts\Length(min: 1, max: 255)]
|
||||
#[Asserts\Regex('#^[[:alnum:]\\\]+\z#u')]
|
||||
private ?string $dataflowType = null;
|
||||
|
||||
/**
|
||||
* @var array|null
|
||||
*/
|
||||
private $options;
|
||||
private ?array $options = null;
|
||||
|
||||
/**
|
||||
* @var \DateTimeInterface|null
|
||||
*
|
||||
* @Asserts\DateTime()
|
||||
*/
|
||||
private $requestedDate;
|
||||
private ?\DateTimeInterface $requestedDate = null;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
private $scheduledDataflowId;
|
||||
private ?int $scheduledDataflowId = null;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
private $count;
|
||||
private ?int $count = 0;
|
||||
|
||||
/**
|
||||
* @var array|null
|
||||
*/
|
||||
private $exceptions;
|
||||
private ?array $exceptions = null;
|
||||
|
||||
/**
|
||||
* @var \DateTimeInterface|null
|
||||
*/
|
||||
private $startTime;
|
||||
private ?\DateTimeInterface $startTime = null;
|
||||
|
||||
/**
|
||||
* @var \DateTimeInterface|null
|
||||
*/
|
||||
private $endTime;
|
||||
private ?\DateTimeInterface $endTime = null;
|
||||
|
||||
/**
|
||||
* @param ScheduledDataflow $scheduled
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public static function createFromScheduledDataflow(ScheduledDataflow $scheduled): self
|
||||
{
|
||||
return (new static())
|
||||
@@ -114,29 +72,22 @@ class Job
|
||||
->setScheduledDataflowId($scheduled->getId());
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->count = 0;
|
||||
$this->status = static::STATUS_PENDING;
|
||||
}
|
||||
|
||||
public static function createFromArray(array $datas)
|
||||
{
|
||||
$lost = array_diff(static::KEYS, array_keys($datas));
|
||||
if (count($lost) > 0) {
|
||||
throw new \LogicException('The first argument of '.__METHOD__.' must be contains: "'.implode(', ',
|
||||
$lost).'"');
|
||||
if (\count($lost) > 0) {
|
||||
throw new \LogicException('The first argument of '.__METHOD__.' must be contains: "'.implode(', ', $lost).'"');
|
||||
}
|
||||
|
||||
$job = new self();
|
||||
$job->id = null === $datas['id'] ? null : (int) $datas['id'];
|
||||
$job->setStatus(null === $datas['status'] ? null : (int) $datas['status']);
|
||||
$job->id = $datas['id'] === null ? null : (int) $datas['id'];
|
||||
$job->setStatus($datas['status'] === null ? null : (int) $datas['status']);
|
||||
$job->setLabel($datas['label']);
|
||||
$job->setDataflowType($datas['dataflow_type']);
|
||||
$job->setOptions($datas['options']);
|
||||
$job->setRequestedDate($datas['requested_date']);
|
||||
$job->setScheduledDataflowId(null === $datas['scheduled_dataflow_id'] ? null : (int) $datas['scheduled_dataflow_id']);
|
||||
$job->setCount(null === $datas['count'] ? null : (int) $datas['count']);
|
||||
$job->setScheduledDataflowId($datas['scheduled_dataflow_id'] === null ? null : (int) $datas['scheduled_dataflow_id']);
|
||||
$job->setCount($datas['count'] === null ? null : (int) $datas['count']);
|
||||
$job->setExceptions($datas['exceptions']);
|
||||
$job->setStartTime($datas['start_time']);
|
||||
$job->setEndTime($datas['end_time']);
|
||||
@@ -161,220 +112,132 @@ class Job
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function setId(int $id): Job
|
||||
public function setId(int $id): self
|
||||
{
|
||||
$this->id = $id;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getStatus(): int
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $status
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function setStatus(int $status): Job
|
||||
public function setStatus(int $status): self
|
||||
{
|
||||
$this->status = $status;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getLabel(): ?string
|
||||
{
|
||||
return $this->label;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $label
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function setLabel(?string $label): Job
|
||||
public function setLabel(?string $label): self
|
||||
{
|
||||
$this->label = $label;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getDataflowType(): ?string
|
||||
{
|
||||
return $this->dataflowType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $dataflowType
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function setDataflowType(?string $dataflowType): Job
|
||||
public function setDataflowType(?string $dataflowType): self
|
||||
{
|
||||
$this->dataflowType = $dataflowType;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*/
|
||||
public function getOptions(): ?array
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|null $options
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function setOptions(?array $options): Job
|
||||
public function setOptions(?array $options): self
|
||||
{
|
||||
$this->options = $options;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \DateTimeInterface|null
|
||||
*/
|
||||
public function getRequestedDate(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->requestedDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTimeInterface|null $requestedDate
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function setRequestedDate(?\DateTimeInterface $requestedDate): Job
|
||||
public function setRequestedDate(?\DateTimeInterface $requestedDate): self
|
||||
{
|
||||
$this->requestedDate = $requestedDate;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getScheduledDataflowId(): ?int
|
||||
{
|
||||
return $this->scheduledDataflowId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $scheduledDataflowId
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function setScheduledDataflowId(?int $scheduledDataflowId): Job
|
||||
public function setScheduledDataflowId(?int $scheduledDataflowId): self
|
||||
{
|
||||
$this->scheduledDataflowId = $scheduledDataflowId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getCount(): ?int
|
||||
{
|
||||
return $this->count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $count
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function setCount(?int $count): Job
|
||||
public function setCount(?int $count): self
|
||||
{
|
||||
$this->count = $count;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*/
|
||||
public function getExceptions(): ?array
|
||||
{
|
||||
return $this->exceptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|null $exceptions
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function setExceptions(?array $exceptions): Job
|
||||
public function setExceptions(?array $exceptions): self
|
||||
{
|
||||
$this->exceptions = $exceptions;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \DateTimeInterface|null
|
||||
*/
|
||||
public function getStartTime(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->startTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTimeInterface|null $startTime
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function setStartTime(?\DateTimeInterface $startTime): Job
|
||||
public function setStartTime(?\DateTimeInterface $startTime): self
|
||||
{
|
||||
$this->startTime = $startTime;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \DateTimeInterface|null
|
||||
*/
|
||||
public function getEndTime(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->endTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTimeInterface|null $endTime
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function setEndTime(?\DateTimeInterface $endTime): Job
|
||||
public function setEndTime(?\DateTimeInterface $endTime): self
|
||||
{
|
||||
$this->endTime = $endTime;
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ use Symfony\Component\Validator\Constraints as Asserts;
|
||||
*/
|
||||
class ScheduledDataflow
|
||||
{
|
||||
const AVAILABLE_FREQUENCIES = [
|
||||
public const AVAILABLE_FREQUENCIES = [
|
||||
'1 hour',
|
||||
'1 day',
|
||||
'1 week',
|
||||
@@ -23,69 +23,46 @@ class ScheduledDataflow
|
||||
|
||||
private const KEYS = ['id', 'label', 'dataflow_type', 'options', 'frequency', 'next', 'enabled'];
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
private $id;
|
||||
private ?int $id = null;
|
||||
|
||||
#[Asserts\NotBlank]
|
||||
#[Asserts\Length(min: 1, max: 255)]
|
||||
#[Asserts\Regex('#^[[:alnum:] ]+\z#u')]
|
||||
private ?string $label = null;
|
||||
|
||||
#[Asserts\NotBlank]
|
||||
#[Asserts\Length(min: 1, max: 255)]
|
||||
#[Asserts\Regex('#^[[:alnum:]\\\]+\z#u')]
|
||||
private ?string $dataflowType = null;
|
||||
|
||||
private ?array $options = null;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*
|
||||
* @Asserts\NotBlank()
|
||||
* @Asserts\Length(min=1, max=255)
|
||||
* @Asserts\Regex("#^[[:alnum:] ]+\z#u")
|
||||
*/
|
||||
private $label;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*
|
||||
* @Asserts\NotBlank()
|
||||
* @Asserts\Length(min=1, max=255)
|
||||
* @Asserts\Regex("#^[[:alnum:]\\]+\z#u")
|
||||
*/
|
||||
private $dataflowType;
|
||||
|
||||
/**
|
||||
* @var array|null
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*
|
||||
* @Asserts\NotBlank()
|
||||
* @Frequency()
|
||||
*/
|
||||
private $frequency;
|
||||
#[Asserts\NotBlank]
|
||||
private ?string $frequency = null;
|
||||
|
||||
/**
|
||||
* @var \DateTimeInterface|null
|
||||
*/
|
||||
private $next;
|
||||
private ?\DateTimeInterface $next = null;
|
||||
|
||||
/**
|
||||
* @var bool|null
|
||||
*/
|
||||
private $enabled;
|
||||
private ?bool $enabled = null;
|
||||
|
||||
public static function createFromArray(array $datas)
|
||||
{
|
||||
$lost = array_diff(static::KEYS, array_keys($datas));
|
||||
if (count($lost) > 0) {
|
||||
throw new \LogicException('The first argument of '.__METHOD__.' must be contains: "'.implode(', ',
|
||||
$lost).'"');
|
||||
if (\count($lost) > 0) {
|
||||
throw new \LogicException('The first argument of '.__METHOD__.' must be contains: "'.implode(', ', $lost).'"');
|
||||
}
|
||||
|
||||
$scheduledDataflow = new self();
|
||||
$scheduledDataflow->id = null === $datas['id'] ? null : (int) $datas['id'];
|
||||
$scheduledDataflow->id = $datas['id'] === null ? null : (int) $datas['id'];
|
||||
|
||||
$scheduledDataflow->setLabel($datas['label']);
|
||||
$scheduledDataflow->setDataflowType($datas['dataflow_type']);
|
||||
$scheduledDataflow->setOptions($datas['options']);
|
||||
$scheduledDataflow->setFrequency($datas['frequency']);
|
||||
$scheduledDataflow->setNext($datas['next']);
|
||||
$scheduledDataflow->setEnabled(null === $datas['enabled'] ? null : (bool) $datas['enabled']);
|
||||
$scheduledDataflow->setEnabled($datas['enabled'] === null ? null : (bool) $datas['enabled']);
|
||||
|
||||
return $scheduledDataflow;
|
||||
}
|
||||
@@ -103,140 +80,84 @@ class ScheduledDataflow
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*
|
||||
* @return ScheduledDataflow
|
||||
*/
|
||||
public function setId(int $id): ScheduledDataflow
|
||||
public function setId(int $id): self
|
||||
{
|
||||
$this->id = $id;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getLabel(): ?string
|
||||
{
|
||||
return $this->label;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $label
|
||||
*
|
||||
* @return ScheduledDataflow
|
||||
*/
|
||||
public function setLabel(?string $label): ScheduledDataflow
|
||||
public function setLabel(?string $label): self
|
||||
{
|
||||
$this->label = $label;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getDataflowType(): ?string
|
||||
{
|
||||
return $this->dataflowType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $dataflowType
|
||||
*
|
||||
* @return ScheduledDataflow
|
||||
*/
|
||||
public function setDataflowType(?string $dataflowType): ScheduledDataflow
|
||||
public function setDataflowType(?string $dataflowType): self
|
||||
{
|
||||
$this->dataflowType = $dataflowType;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*/
|
||||
public function getOptions(): ?array
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|null $options
|
||||
*
|
||||
* @return ScheduledDataflow
|
||||
*/
|
||||
public function setOptions(?array $options): ScheduledDataflow
|
||||
public function setOptions(?array $options): self
|
||||
{
|
||||
$this->options = $options;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getFrequency(): ?string
|
||||
{
|
||||
return $this->frequency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $frequency
|
||||
*
|
||||
* @return ScheduledDataflow
|
||||
*/
|
||||
public function setFrequency(?string $frequency): ScheduledDataflow
|
||||
public function setFrequency(?string $frequency): self
|
||||
{
|
||||
$this->frequency = $frequency;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \DateTimeInterface|null
|
||||
*/
|
||||
public function getNext(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->next;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTimeInterface|null $next
|
||||
*
|
||||
* @return ScheduledDataflow
|
||||
*/
|
||||
public function setNext(?\DateTimeInterface $next): ScheduledDataflow
|
||||
public function setNext(?\DateTimeInterface $next): self
|
||||
{
|
||||
$this->next = $next;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool|null
|
||||
*/
|
||||
public function getEnabled(): ?bool
|
||||
{
|
||||
return $this->enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool|null $enabled
|
||||
*
|
||||
* @return ScheduledDataflow
|
||||
*/
|
||||
public function setEnabled(?bool $enabled): ScheduledDataflow
|
||||
public function setEnabled(?bool $enabled): self
|
||||
{
|
||||
$this->enabled = $enabled;
|
||||
|
||||
|
||||
14
src/Event/CrEvent.php
Normal file
14
src/Event/CrEvent.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Event;
|
||||
|
||||
use Symfony\Contracts\EventDispatcher\Event;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
abstract class CrEvent extends Event
|
||||
{
|
||||
}
|
||||
@@ -6,6 +6,6 @@ namespace CodeRhapsodie\DataflowBundle\Event;
|
||||
|
||||
final class Events
|
||||
{
|
||||
const BEFORE_PROCESSING = 'coderhapsodie.dataflow.before_processing';
|
||||
const AFTER_PROCESSING = 'coderhapsodie.dataflow.after_processing';
|
||||
public const BEFORE_PROCESSING = 'coderhapsodie.dataflow.before_processing';
|
||||
public const AFTER_PROCESSING = 'coderhapsodie.dataflow.after_processing';
|
||||
}
|
||||
|
||||
@@ -5,31 +5,21 @@ declare(strict_types=1);
|
||||
namespace CodeRhapsodie\DataflowBundle\Event;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Entity\Job;
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
|
||||
/**
|
||||
* Event used during the dataflow lifecycle.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class ProcessingEvent extends Event
|
||||
class ProcessingEvent extends CrEvent
|
||||
{
|
||||
/** @var Job */
|
||||
private $job;
|
||||
|
||||
/**
|
||||
* ProcessingEvent constructor.
|
||||
*
|
||||
* @param Job $job
|
||||
*/
|
||||
public function __construct(Job $job)
|
||||
public function __construct(private Job $job)
|
||||
{
|
||||
$this->job = $job;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Job
|
||||
*/
|
||||
public function getJob(): Job
|
||||
{
|
||||
return $this->job;
|
||||
|
||||
@@ -9,4 +9,12 @@ namespace CodeRhapsodie\DataflowBundle\Exceptions;
|
||||
*/
|
||||
class UnknownDataflowTypeException extends \Exception
|
||||
{
|
||||
public static function create(string $aliasOrFqcn, array $knownDataflowTypes): self
|
||||
{
|
||||
return new self(\sprintf(
|
||||
'Unknown dataflow type FQCN or alias "%s". Registered dataflow types FQCN and aliases are %s.',
|
||||
$aliasOrFqcn,
|
||||
implode(', ', $knownDataflowTypes)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
12
src/Exceptions/UnsupportedItemTypeException.php
Normal file
12
src/Exceptions/UnsupportedItemTypeException.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Exceptions;
|
||||
|
||||
/**
|
||||
* Exception thrown when a writer receives an item of an unsupported type.
|
||||
*/
|
||||
class UnsupportedItemTypeException extends \Exception
|
||||
{
|
||||
}
|
||||
12
src/ExceptionsHandler/ExceptionHandlerInterface.php
Normal file
12
src/ExceptionsHandler/ExceptionHandlerInterface.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\ExceptionsHandler;
|
||||
|
||||
interface ExceptionHandlerInterface
|
||||
{
|
||||
public function save(?int $jobId, ?array $exceptions): void;
|
||||
|
||||
public function find(int $jobId): ?array;
|
||||
}
|
||||
37
src/ExceptionsHandler/FilesystemExceptionHandler.php
Normal file
37
src/ExceptionsHandler/FilesystemExceptionHandler.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\ExceptionsHandler;
|
||||
|
||||
use League\Flysystem\Filesystem;
|
||||
use League\Flysystem\FilesystemException;
|
||||
|
||||
class FilesystemExceptionHandler implements ExceptionHandlerInterface
|
||||
{
|
||||
public function __construct(private Filesystem $filesystem)
|
||||
{
|
||||
}
|
||||
|
||||
public function save(?int $jobId, ?array $exceptions): void
|
||||
{
|
||||
if ($jobId === null || empty($exceptions)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->filesystem->write(\sprintf('dataflow-job-%s.log', $jobId), json_encode($exceptions));
|
||||
}
|
||||
|
||||
public function find(int $jobId): ?array
|
||||
{
|
||||
try {
|
||||
if (!$this->filesystem->fileExists(\sprintf('dataflow-job-%s.log', $jobId))) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return json_decode($this->filesystem->read(\sprintf('dataflow-job-%s.log', $jobId)), true);
|
||||
} catch (FilesystemException) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
17
src/ExceptionsHandler/NullExceptionHandler.php
Normal file
17
src/ExceptionsHandler/NullExceptionHandler.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\ExceptionsHandler;
|
||||
|
||||
class NullExceptionHandler implements ExceptionHandlerInterface
|
||||
{
|
||||
public function save(?int $jobId, ?array $exceptions): void
|
||||
{
|
||||
}
|
||||
|
||||
public function find(int $jobId): ?array
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -13,14 +13,8 @@ use Symfony\Component\DependencyInjection\Container;
|
||||
*/
|
||||
class ConnectionFactory
|
||||
{
|
||||
private $connectionName;
|
||||
|
||||
private $container;
|
||||
|
||||
public function __construct(Container $container, string $connectionName)
|
||||
public function __construct(private Container $container, private string $connectionName)
|
||||
{
|
||||
$this->connectionName = $connectionName;
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
public function setConnectionName(string $connectionName)
|
||||
@@ -28,8 +22,8 @@ class ConnectionFactory
|
||||
$this->connectionName = $connectionName;
|
||||
}
|
||||
|
||||
public function getConnection(): \Doctrine\DBAL\Driver\Connection
|
||||
public function getConnection(): \Doctrine\DBAL\Connection
|
||||
{
|
||||
return $this->container->get(sprintf('doctrine.dbal.%s_connection', $this->connectionName));
|
||||
return $this->container->get(\sprintf('doctrine.dbal.%s_connection', $this->connectionName));
|
||||
}
|
||||
}
|
||||
|
||||
52
src/Gateway/JobGateway.php
Normal file
52
src/Gateway/JobGateway.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Gateway;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Entity\Job;
|
||||
use CodeRhapsodie\DataflowBundle\ExceptionsHandler\ExceptionHandlerInterface;
|
||||
use CodeRhapsodie\DataflowBundle\ExceptionsHandler\NullExceptionHandler;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
|
||||
class JobGateway
|
||||
{
|
||||
public function __construct(private JobRepository $repository, private ExceptionHandlerInterface $exceptionHandler)
|
||||
{
|
||||
}
|
||||
|
||||
public function find(int $jobId): ?Job
|
||||
{
|
||||
$job = $this->repository->find($jobId);
|
||||
|
||||
return $this->loadExceptions($job);
|
||||
}
|
||||
|
||||
public function save(Job $job): void
|
||||
{
|
||||
if (!$this->exceptionHandler instanceof NullExceptionHandler) {
|
||||
$this->exceptionHandler->save($job->getId(), $job->getExceptions());
|
||||
$job->setExceptions([]);
|
||||
}
|
||||
|
||||
$this->repository->save($job);
|
||||
}
|
||||
|
||||
public function findLastForDataflowId(int $scheduleId): ?Job
|
||||
{
|
||||
$job = $this->repository->findLastForDataflowId($scheduleId);
|
||||
|
||||
return $this->loadExceptions($job);
|
||||
}
|
||||
|
||||
private function loadExceptions(?Job $job): ?Job
|
||||
{
|
||||
if ($job === null || $this->exceptionHandler instanceof NullExceptionHandler) {
|
||||
return $job;
|
||||
}
|
||||
|
||||
$this->exceptionHandler->save($job->getId(), $job->getExceptions());
|
||||
|
||||
return $job->setExceptions($this->exceptionHandler->find($job->getId()));
|
||||
}
|
||||
}
|
||||
35
src/Logger/BufferHandler.php
Normal file
35
src/Logger/BufferHandler.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Logger;
|
||||
|
||||
use Monolog\Formatter\FormatterInterface;
|
||||
use Monolog\Formatter\LineFormatter;
|
||||
use Monolog\Handler\AbstractProcessingHandler;
|
||||
use Monolog\LogRecord;
|
||||
|
||||
class BufferHandler extends AbstractProcessingHandler
|
||||
{
|
||||
private const FORMAT = "[%datetime%] %level_name% when processing item %context.index%: %message% %context% %extra%\n";
|
||||
|
||||
private array $buffer = [];
|
||||
|
||||
public function clearBuffer(): array
|
||||
{
|
||||
$logs = $this->buffer;
|
||||
$this->buffer = [];
|
||||
|
||||
return $logs;
|
||||
}
|
||||
|
||||
protected function write(array|LogRecord $record): void
|
||||
{
|
||||
$this->buffer[] = $record['formatted'];
|
||||
}
|
||||
|
||||
protected function getDefaultFormatter(): FormatterInterface
|
||||
{
|
||||
return new LineFormatter(self::FORMAT);
|
||||
}
|
||||
}
|
||||
32
src/Logger/DelegatingLogger.php
Normal file
32
src/Logger/DelegatingLogger.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Logger;
|
||||
|
||||
use Psr\Log\AbstractLogger;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
final class DelegatingLogger extends AbstractLogger
|
||||
{
|
||||
/** @var LoggerInterface[] */
|
||||
private ?array $loggers = null;
|
||||
|
||||
public function __construct(iterable $loggers)
|
||||
{
|
||||
foreach ($loggers as $logger) {
|
||||
if (!$logger instanceof LoggerInterface) {
|
||||
throw new \InvalidArgumentException(\sprintf('Only instances of %s should be passed to the constructor of %s. An instance of %s was passed instead.', LoggerInterface::class, self::class, $logger::class));
|
||||
}
|
||||
|
||||
$this->loggers[] = $logger;
|
||||
}
|
||||
}
|
||||
|
||||
public function log($level, $message, array $context = []): void
|
||||
{
|
||||
foreach ($this->loggers as $logger) {
|
||||
$logger->log($level, $message, $context);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,42 +4,27 @@ declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Manager;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Entity\ScheduledDataflow;
|
||||
use CodeRhapsodie\DataflowBundle\Entity\Job;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\ScheduledDataflowRepository;
|
||||
use CodeRhapsodie\DataflowBundle\Entity\ScheduledDataflow;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
use Doctrine\DBAL\Driver\Connection;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\ScheduledDataflowRepository;
|
||||
use Doctrine\DBAL\Connection;
|
||||
|
||||
/**
|
||||
* Handles scheduled dataflows execution dates based on their frequency.
|
||||
*/
|
||||
class ScheduledDataflowManager implements ScheduledDataflowManagerInterface
|
||||
{
|
||||
/** @var ScheduledDataflowRepository */
|
||||
private $scheduledDataflowRepository;
|
||||
|
||||
/** @var JobRepository */
|
||||
private $jobRepository;
|
||||
|
||||
/** @var Connection */
|
||||
private $connection;
|
||||
|
||||
public function __construct(Connection $connection, ScheduledDataflowRepository $scheduledDataflowRepository, JobRepository $jobRepository)
|
||||
public function __construct(private Connection $connection, private ScheduledDataflowRepository $scheduledDataflowRepository, private JobRepository $jobRepository)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
$this->scheduledDataflowRepository = $scheduledDataflowRepository;
|
||||
$this->jobRepository = $jobRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function createJobsFromScheduledDataflows(): void
|
||||
{
|
||||
$this->connection->beginTransaction();
|
||||
try {
|
||||
foreach ($this->scheduledDataflowRepository->findReadyToRun() as $scheduled) {
|
||||
if (null !== $this->jobRepository->findPendingForScheduledDataflow($scheduled)) {
|
||||
if ($this->jobRepository->findPendingForScheduledDataflow($scheduled) !== null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -53,9 +38,6 @@ class ScheduledDataflowManager implements ScheduledDataflowManagerInterface
|
||||
$this->connection->commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ScheduledDataflow $scheduled
|
||||
*/
|
||||
private function updateScheduledDataflowNext(ScheduledDataflow $scheduled): void
|
||||
{
|
||||
$interval = \DateInterval::createFromDateString($scheduled->getFrequency());
|
||||
@@ -70,9 +52,6 @@ class ScheduledDataflowManager implements ScheduledDataflowManagerInterface
|
||||
$this->scheduledDataflowRepository->save($scheduled);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ScheduledDataflow $scheduled
|
||||
*/
|
||||
private function createPendingForScheduled(ScheduledDataflow $scheduled): void
|
||||
{
|
||||
$this->jobRepository->save(Job::createFromScheduledDataflow($scheduled));
|
||||
|
||||
17
src/MessengerMode/JobMessage.php
Normal file
17
src/MessengerMode/JobMessage.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\MessengerMode;
|
||||
|
||||
class JobMessage
|
||||
{
|
||||
public function __construct(private int $jobId)
|
||||
{
|
||||
}
|
||||
|
||||
public function getJobId(): int
|
||||
{
|
||||
return $this->jobId;
|
||||
}
|
||||
}
|
||||
22
src/MessengerMode/JobMessageHandler.php
Normal file
22
src/MessengerMode/JobMessageHandler.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\MessengerMode;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Processor\JobProcessorInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
|
||||
#[AsMessageHandler]
|
||||
class JobMessageHandler
|
||||
{
|
||||
public function __construct(private JobRepository $repository, private JobProcessorInterface $processor)
|
||||
{
|
||||
}
|
||||
|
||||
public function __invoke(JobMessage $message)
|
||||
{
|
||||
$this->processor->process($this->repository->find($message->getJobId()));
|
||||
}
|
||||
}
|
||||
88
src/Processor/JobProcessor.php
Normal file
88
src/Processor/JobProcessor.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Processor;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\AutoUpdateCountInterface;
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Result;
|
||||
use CodeRhapsodie\DataflowBundle\Entity\Job;
|
||||
use CodeRhapsodie\DataflowBundle\Event\Events;
|
||||
use CodeRhapsodie\DataflowBundle\Event\ProcessingEvent;
|
||||
use CodeRhapsodie\DataflowBundle\Gateway\JobGateway;
|
||||
use CodeRhapsodie\DataflowBundle\Logger\BufferHandler;
|
||||
use CodeRhapsodie\DataflowBundle\Logger\DelegatingLogger;
|
||||
use CodeRhapsodie\DataflowBundle\Registry\DataflowTypeRegistryInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
use Monolog\Logger;
|
||||
use Psr\Log\LoggerAwareInterface;
|
||||
use Psr\Log\LoggerAwareTrait;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
class JobProcessor implements JobProcessorInterface, LoggerAwareInterface
|
||||
{
|
||||
use LoggerAwareTrait;
|
||||
|
||||
public function __construct(
|
||||
private JobRepository $repository,
|
||||
private DataflowTypeRegistryInterface $registry,
|
||||
private EventDispatcherInterface $dispatcher,
|
||||
private JobGateway $jobGateway,
|
||||
) {
|
||||
}
|
||||
|
||||
public function process(Job $job): void
|
||||
{
|
||||
$this->beforeProcessing($job);
|
||||
|
||||
$dataflowType = $this->registry->getDataflowType($job->getDataflowType());
|
||||
if ($dataflowType instanceof AutoUpdateCountInterface) {
|
||||
$dataflowType->setRepository($this->repository);
|
||||
}
|
||||
|
||||
$loggers = [new Logger('dataflow_internal', [$bufferHandler = new BufferHandler()])];
|
||||
if (isset($this->logger)) {
|
||||
$loggers[] = $this->logger;
|
||||
}
|
||||
$logger = new DelegatingLogger($loggers);
|
||||
|
||||
if ($dataflowType instanceof LoggerAwareInterface) {
|
||||
$dataflowType->setLogger($logger);
|
||||
}
|
||||
|
||||
$result = $dataflowType->process($job->getOptions(), $job->getId());
|
||||
|
||||
if (!$dataflowType instanceof LoggerAwareInterface) {
|
||||
foreach ($result->getExceptions() as $index => $e) {
|
||||
$logger->error($e, ['index' => $index]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->afterProcessing($job, $result, $bufferHandler);
|
||||
}
|
||||
|
||||
private function beforeProcessing(Job $job): void
|
||||
{
|
||||
$this->dispatcher->dispatch(new ProcessingEvent($job), Events::BEFORE_PROCESSING);
|
||||
|
||||
$job
|
||||
->setStatus(Job::STATUS_RUNNING)
|
||||
->setStartTime(new \DateTime())
|
||||
;
|
||||
$this->jobGateway->save($job);
|
||||
}
|
||||
|
||||
private function afterProcessing(Job $job, Result $result, BufferHandler $bufferLogger): void
|
||||
{
|
||||
$job
|
||||
->setEndTime($result->getEndTime())
|
||||
->setStatus(Job::STATUS_COMPLETED)
|
||||
->setCount($result->getSuccessCount())
|
||||
->setExceptions($bufferLogger->clearBuffer())
|
||||
;
|
||||
|
||||
$this->jobGateway->save($job);
|
||||
|
||||
$this->dispatcher->dispatch(new ProcessingEvent($job), Events::AFTER_PROCESSING);
|
||||
}
|
||||
}
|
||||
12
src/Processor/JobProcessorInterface.php
Normal file
12
src/Processor/JobProcessorInterface.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Processor;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Entity\Job;
|
||||
|
||||
interface JobProcessorInterface
|
||||
{
|
||||
public function process(Job $job): void;
|
||||
}
|
||||
@@ -4,8 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Registry;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Exceptions\UnknownDataflowTypeException;
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\DataflowTypeInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Exceptions\UnknownDataflowTypeException;
|
||||
|
||||
/**
|
||||
* Array based dataflow types registry.
|
||||
@@ -13,14 +13,11 @@ use CodeRhapsodie\DataflowBundle\DataflowType\DataflowTypeInterface;
|
||||
class DataflowTypeRegistry implements DataflowTypeRegistryInterface
|
||||
{
|
||||
/** @var array|DataflowTypeInterface[] */
|
||||
private $fqcnRegistry = [];
|
||||
private array $fqcnRegistry = [];
|
||||
|
||||
/** @var array|DataflowTypeInterface[] */
|
||||
private $aliasesRegistry = [];
|
||||
private array $aliasesRegistry = [];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDataflowType(string $fqcnOrAlias): DataflowTypeInterface
|
||||
{
|
||||
if (isset($this->fqcnRegistry[$fqcnOrAlias])) {
|
||||
@@ -31,23 +28,17 @@ class DataflowTypeRegistry implements DataflowTypeRegistryInterface
|
||||
return $this->aliasesRegistry[$fqcnOrAlias];
|
||||
}
|
||||
|
||||
throw new UnknownDataflowTypeException($fqcnOrAlias);
|
||||
throw UnknownDataflowTypeException::create($fqcnOrAlias, [...array_keys($this->fqcnRegistry), ...array_keys($this->aliasesRegistry)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function listDataflowTypes(): iterable
|
||||
{
|
||||
return $this->fqcnRegistry;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function registerDataflowType(DataflowTypeInterface $dataflowType): void
|
||||
{
|
||||
$this->fqcnRegistry[get_class($dataflowType)] = $dataflowType;
|
||||
$this->fqcnRegistry[$dataflowType::class] = $dataflowType;
|
||||
foreach ($dataflowType->getAliases() as $alias) {
|
||||
$this->aliasesRegistry[$alias] = $dataflowType;
|
||||
}
|
||||
|
||||
@@ -13,10 +13,6 @@ interface DataflowTypeRegistryInterface
|
||||
{
|
||||
/**
|
||||
* Get a registered dataflow type from its FQCN or one of its aliases.
|
||||
*
|
||||
* @param string $fqcnOrAlias
|
||||
*
|
||||
* @return DataflowTypeInterface
|
||||
*/
|
||||
public function getDataflowType(string $fqcnOrAlias): DataflowTypeInterface;
|
||||
|
||||
@@ -29,8 +25,6 @@ interface DataflowTypeRegistryInterface
|
||||
|
||||
/**
|
||||
* Registers a dataflow type.
|
||||
*
|
||||
* @param DataflowTypeInterface $dataflowType
|
||||
*/
|
||||
public function registerDataflowType(DataflowTypeInterface $dataflowType): void;
|
||||
}
|
||||
|
||||
@@ -9,10 +9,12 @@ namespace CodeRhapsodie\DataflowBundle\Repository;
|
||||
*/
|
||||
trait InitFromDbTrait
|
||||
{
|
||||
abstract private function getFields(): array;
|
||||
|
||||
private function initDateTime(array $datas): array
|
||||
{
|
||||
foreach (static::FIELDS_TYPE as $key => $type) {
|
||||
if ('datetime' === $type && null !== $datas[$key]) {
|
||||
foreach ($this->getFields() as $key => $type) {
|
||||
if ($type === 'datetime' && $datas[$key] !== null) {
|
||||
$datas[$key] = new \DateTime($datas[$key]);
|
||||
}
|
||||
}
|
||||
@@ -22,10 +24,10 @@ trait InitFromDbTrait
|
||||
|
||||
private function initArray(array $datas): array
|
||||
{
|
||||
if (!is_array($datas['options'])) {
|
||||
if (!\is_array($datas['options'])) {
|
||||
$datas['options'] = $this->strToArray($datas['options']);
|
||||
}
|
||||
if (array_key_exists('exceptions', $datas) && !is_array($datas['exceptions'])) {
|
||||
if (\array_key_exists('exceptions', $datas) && !\is_array($datas['exceptions'])) {
|
||||
$datas['exceptions'] = $this->strToArray($datas['exceptions']);
|
||||
}
|
||||
|
||||
@@ -34,12 +36,12 @@ trait InitFromDbTrait
|
||||
|
||||
private function strToArray($value): array
|
||||
{
|
||||
if (null === $value) {
|
||||
if ($value === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$array = json_decode($value, true);
|
||||
$array = json_decode($value, true, 512, \JSON_THROW_ON_ERROR);
|
||||
|
||||
return (false === $array) ? [] : $array;
|
||||
return ($array === false) ? [] : $array;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@ namespace CodeRhapsodie\DataflowBundle\Repository;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Entity\Job;
|
||||
use CodeRhapsodie\DataflowBundle\Entity\ScheduledDataflow;
|
||||
use Doctrine\DBAL\Driver\Connection;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\ParameterType;
|
||||
use Doctrine\DBAL\Query\QueryBuilder;
|
||||
|
||||
/**
|
||||
@@ -20,40 +21,18 @@ class JobRepository
|
||||
|
||||
public const TABLE_NAME = 'cr_dataflow_job';
|
||||
|
||||
private const FIELDS_TYPE = [
|
||||
'id' => \PDO::PARAM_INT,
|
||||
'status' => \PDO::PARAM_INT,
|
||||
'label' => \PDO::PARAM_STR,
|
||||
'dataflow_type' => \PDO::PARAM_STR,
|
||||
'options' => \PDO::PARAM_STR,
|
||||
'requested_date' => 'datetime',
|
||||
'scheduled_dataflow_id' => \PDO::PARAM_INT,
|
||||
'count' => \PDO::PARAM_INT,
|
||||
'exceptions' => \PDO::PARAM_STR,
|
||||
'start_time' => 'datetime',
|
||||
'end_time' => 'datetime',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var \Doctrine\DBAL\Connection
|
||||
*/
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* JobRepository constructor.
|
||||
*
|
||||
* @param Connection $connection
|
||||
*/
|
||||
public function __construct(Connection $connection)
|
||||
public function __construct(private Connection $connection)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
public function find(int $jobId)
|
||||
{
|
||||
$qb = $this->createQueryBuilder();
|
||||
$qb
|
||||
->andWhere($qb->expr()->eq('id', $qb->createNamedParameter($jobId, \PDO::PARAM_INT)))
|
||||
->andWhere($qb->expr()->eq('id', $qb->createNamedParameter($jobId, ParameterType::INTEGER)))
|
||||
;
|
||||
|
||||
return $this->returnFirstOrNull($qb);
|
||||
@@ -64,12 +43,12 @@ class JobRepository
|
||||
$qb = $this->createQueryBuilder();
|
||||
$qb
|
||||
->andWhere($qb->expr()->isNull('scheduled_dataflow_id'))
|
||||
->andWhere($qb->expr()->eq('status', $qb->createNamedParameter(Job::STATUS_PENDING, \PDO::PARAM_INT)));
|
||||
$stmt = $qb->execute();
|
||||
if (0 === $stmt->rowCount()) {
|
||||
->andWhere($qb->expr()->eq('status', $qb->createNamedParameter(Job::STATUS_PENDING, ParameterType::INTEGER)));
|
||||
$stmt = $qb->executeQuery();
|
||||
if ($stmt->rowCount() === 0) {
|
||||
return [];
|
||||
}
|
||||
while (false !== ($row = $stmt->fetch(\PDO::FETCH_ASSOC))) {
|
||||
while (false !== ($row = $stmt->fetchAssociative())) {
|
||||
yield Job::createFromArray($this->initDateTime($this->initArray($row)));
|
||||
}
|
||||
}
|
||||
@@ -78,8 +57,8 @@ class JobRepository
|
||||
{
|
||||
$qb = $this->createQueryBuilder();
|
||||
$qb
|
||||
->andWhere($qb->expr()->eq('scheduled_dataflow_id', $qb->createNamedParameter($scheduled->getId(), \PDO::PARAM_INT)))
|
||||
->andWhere($qb->expr()->eq('status', $qb->createNamedParameter(Job::STATUS_PENDING, \PDO::PARAM_INT)));
|
||||
->andWhere($qb->expr()->eq('scheduled_dataflow_id', $qb->createNamedParameter($scheduled->getId(), ParameterType::INTEGER)))
|
||||
->andWhere($qb->expr()->eq('status', $qb->createNamedParameter(Job::STATUS_PENDING, ParameterType::INTEGER)));
|
||||
|
||||
return $this->returnFirstOrNull($qb);
|
||||
}
|
||||
@@ -88,7 +67,7 @@ class JobRepository
|
||||
{
|
||||
$qb = $this->createQueryBuilder();
|
||||
$qb->andWhere($qb->expr()->lte('requested_date', $qb->createNamedParameter(new \DateTime(), 'datetime')))
|
||||
->andWhere($qb->expr()->eq('status', $qb->createNamedParameter(Job::STATUS_PENDING, \PDO::PARAM_INT)))
|
||||
->andWhere($qb->expr()->eq('status', $qb->createNamedParameter(Job::STATUS_PENDING, ParameterType::INTEGER)))
|
||||
->orderBy('requested_date', 'ASC')
|
||||
->setMaxResults(1)
|
||||
;
|
||||
@@ -99,7 +78,7 @@ class JobRepository
|
||||
public function findLastForDataflowId(int $dataflowId): ?Job
|
||||
{
|
||||
$qb = $this->createQueryBuilder();
|
||||
$qb->andWhere($qb->expr()->eq('scheduled_dataflow_id', $qb->createNamedParameter($dataflowId, \PDO::PARAM_INT)))
|
||||
$qb->andWhere($qb->expr()->eq('scheduled_dataflow_id', $qb->createNamedParameter($dataflowId, ParameterType::INTEGER)))
|
||||
->orderBy('requested_date', 'DESC')
|
||||
->setMaxResults(1)
|
||||
;
|
||||
@@ -113,11 +92,11 @@ class JobRepository
|
||||
$qb
|
||||
->orderBy('requested_date', 'DESC')
|
||||
->setMaxResults(20);
|
||||
$stmt = $qb->execute();
|
||||
if (0 === $stmt->rowCount()) {
|
||||
$stmt = $qb->executeQuery();
|
||||
if ($stmt->rowCount() === 0) {
|
||||
return [];
|
||||
}
|
||||
while (false !== ($row = $stmt->fetch(\PDO::FETCH_ASSOC))) {
|
||||
while (false !== ($row = $stmt->fetchAssociative())) {
|
||||
yield Job::createFromArray($row);
|
||||
}
|
||||
}
|
||||
@@ -125,14 +104,14 @@ class JobRepository
|
||||
public function findForScheduled(int $id): iterable
|
||||
{
|
||||
$qb = $this->createQueryBuilder();
|
||||
$qb->andWhere($qb->expr()->eq('scheduled_dataflow_id', $qb->createNamedParameter($id, \PDO::PARAM_INT)))
|
||||
$qb->andWhere($qb->expr()->eq('scheduled_dataflow_id', $qb->createNamedParameter($id, ParameterType::INTEGER)))
|
||||
->orderBy('requested_date', 'DESC')
|
||||
->setMaxResults(20);
|
||||
$stmt = $qb->execute();
|
||||
if (0 === $stmt->rowCount()) {
|
||||
$stmt = $qb->executeQuery();
|
||||
if ($stmt->rowCount() === 0) {
|
||||
return [];
|
||||
}
|
||||
while (false !== ($row = $stmt->fetch(\PDO::FETCH_ASSOC))) {
|
||||
while (false !== ($row = $stmt->fetchAssociative())) {
|
||||
yield Job::createFromArray($row);
|
||||
}
|
||||
}
|
||||
@@ -142,20 +121,25 @@ class JobRepository
|
||||
$datas = $job->toArray();
|
||||
unset($datas['id']);
|
||||
|
||||
if (is_array($datas['options'])) {
|
||||
$datas['options'] = json_encode($datas['options']);
|
||||
if (\is_array($datas['options'])) {
|
||||
$datas['options'] = json_encode($datas['options'], \JSON_THROW_ON_ERROR);
|
||||
}
|
||||
if (is_array($datas['exceptions'])) {
|
||||
$datas['exceptions'] = json_encode($datas['exceptions']);
|
||||
if (\is_array($datas['exceptions'])) {
|
||||
$datas['exceptions'] = json_encode($datas['exceptions'], \JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
if (null === $job->getId()) {
|
||||
$this->connection->insert(static::TABLE_NAME, $datas, static::FIELDS_TYPE);
|
||||
if ($job->getId() === null) {
|
||||
$this->connection->insert(static::TABLE_NAME, $datas, $this->getFields());
|
||||
$job->setId((int) $this->connection->lastInsertId());
|
||||
|
||||
return;
|
||||
}
|
||||
$this->connection->update(static::TABLE_NAME, $datas, ['id' => $job->getId()], static::FIELDS_TYPE);
|
||||
$this->connection->update(static::TABLE_NAME, $datas, ['id' => $job->getId()], $this->getFields());
|
||||
}
|
||||
|
||||
public function updateCount(int $jobId, int $count): void
|
||||
{
|
||||
$this->connection->update(static::TABLE_NAME, ['count' => $count], ['id' => $jobId]);
|
||||
}
|
||||
|
||||
public function createQueryBuilder($alias = null): QueryBuilder
|
||||
@@ -169,11 +153,28 @@ class JobRepository
|
||||
|
||||
private function returnFirstOrNull(QueryBuilder $qb): ?Job
|
||||
{
|
||||
$stmt = $qb->execute();
|
||||
if (0 === $stmt->rowCount()) {
|
||||
$stmt = $qb->executeQuery();
|
||||
if ($stmt->rowCount() === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Job::createFromArray($this->initDateTime($this->initArray($stmt->fetch(\PDO::FETCH_ASSOC))));
|
||||
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',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,8 @@ declare(strict_types=1);
|
||||
namespace CodeRhapsodie\DataflowBundle\Repository;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Entity\ScheduledDataflow;
|
||||
use Doctrine\DBAL\Driver\Connection;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\ParameterType;
|
||||
use Doctrine\DBAL\Query\QueryBuilder;
|
||||
|
||||
/**
|
||||
@@ -19,28 +20,11 @@ class ScheduledDataflowRepository
|
||||
|
||||
public const TABLE_NAME = 'cr_dataflow_scheduled';
|
||||
|
||||
private const FIELDS_TYPE = [
|
||||
'id' => \PDO::PARAM_INT,
|
||||
'label' => \PDO::PARAM_STR,
|
||||
'dataflow_type' => \PDO::PARAM_STR,
|
||||
'options' => \PDO::PARAM_STR,
|
||||
'frequency' => \PDO::PARAM_STR,
|
||||
'next' => 'datetime',
|
||||
'enabled' => \PDO::PARAM_BOOL,
|
||||
];
|
||||
/**
|
||||
* @var \Doctrine\DBAL\Connection
|
||||
*/
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* JobRepository constructor.
|
||||
*
|
||||
* @param Connection $connection
|
||||
*/
|
||||
public function __construct(Connection $connection)
|
||||
public function __construct(private Connection $connection)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,15 +36,15 @@ class ScheduledDataflowRepository
|
||||
{
|
||||
$qb = $this->createQueryBuilder();
|
||||
$qb->andWhere($qb->expr()->lte('next', $qb->createNamedParameter(new \DateTime(), 'datetime')))
|
||||
->andWhere($qb->expr()->eq('enabled', 1))
|
||||
->andWhere($qb->expr()->eq('enabled', $qb->createNamedParameter(1, ParameterType::INTEGER)))
|
||||
->orderBy('next', 'ASC')
|
||||
;
|
||||
;
|
||||
|
||||
$stmt = $qb->execute();
|
||||
if (0 === $stmt->rowCount()) {
|
||||
$stmt = $qb->executeQuery();
|
||||
if ($stmt->rowCount() === 0) {
|
||||
return [];
|
||||
}
|
||||
while (false !== ($row = $stmt->fetch(\PDO::FETCH_ASSOC))) {
|
||||
while (false !== ($row = $stmt->fetchAssociative())) {
|
||||
yield ScheduledDataflow::createFromArray($this->initDateTime($this->initArray($row)));
|
||||
}
|
||||
}
|
||||
@@ -68,7 +52,7 @@ class ScheduledDataflowRepository
|
||||
public function find(int $scheduleId): ?ScheduledDataflow
|
||||
{
|
||||
$qb = $this->createQueryBuilder();
|
||||
$qb->andWhere($qb->expr()->eq('id', $qb->createNamedParameter($scheduleId, \PDO::PARAM_INT)))
|
||||
$qb->andWhere($qb->expr()->eq('id', $qb->createNamedParameter($scheduleId, ParameterType::INTEGER)))
|
||||
->setMaxResults(1)
|
||||
;
|
||||
|
||||
@@ -80,12 +64,12 @@ class ScheduledDataflowRepository
|
||||
$qb = $this->createQueryBuilder();
|
||||
$qb->orderBy('label', 'ASC');
|
||||
|
||||
$stmt = $qb->execute();
|
||||
if (0 === $stmt->rowCount()) {
|
||||
$stmt = $qb->executeQuery();
|
||||
if ($stmt->rowCount() === 0) {
|
||||
return [];
|
||||
}
|
||||
while (false !== ($row = $stmt->fetch(\PDO::FETCH_ASSOC))) {
|
||||
yield ScheduledDataflow::createFromArray($this->initDateTime($this->initOptions($row)));
|
||||
while (false !== ($row = $stmt->fetchAssociative())) {
|
||||
yield ScheduledDataflow::createFromArray($this->initDateTime($this->initArray($row)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,7 +82,7 @@ class ScheduledDataflowRepository
|
||||
->orderBy('w.label', 'ASC')
|
||||
->groupBy('w.id');
|
||||
|
||||
return $query->execute()->fetchAll(\PDO::FETCH_ASSOC);
|
||||
return $query->executeQuery()->fetchAllAssociative();
|
||||
}
|
||||
|
||||
public function save(ScheduledDataflow $scheduledDataflow)
|
||||
@@ -106,17 +90,17 @@ class ScheduledDataflowRepository
|
||||
$datas = $scheduledDataflow->toArray();
|
||||
unset($datas['id']);
|
||||
|
||||
if (is_array($datas['options'])) {
|
||||
$datas['options'] = json_encode($datas['options']);
|
||||
if (\is_array($datas['options'])) {
|
||||
$datas['options'] = json_encode($datas['options'], \JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
if (null === $scheduledDataflow->getId()) {
|
||||
$this->connection->insert(static::TABLE_NAME, $datas, static::FIELDS_TYPE);
|
||||
if ($scheduledDataflow->getId() === null) {
|
||||
$this->connection->insert(static::TABLE_NAME, $datas, $this->getFields());
|
||||
$scheduledDataflow->setId((int) $this->connection->lastInsertId());
|
||||
|
||||
return;
|
||||
}
|
||||
$this->connection->update(static::TABLE_NAME, $datas, ['id' => $scheduledDataflow->getId()], static::FIELDS_TYPE);
|
||||
$this->connection->update(static::TABLE_NAME, $datas, ['id' => $scheduledDataflow->getId()], $this->getFields());
|
||||
}
|
||||
|
||||
public function delete(int $id): void
|
||||
@@ -144,11 +128,24 @@ class ScheduledDataflowRepository
|
||||
|
||||
private function returnFirstOrNull(QueryBuilder $qb): ?ScheduledDataflow
|
||||
{
|
||||
$stmt = $qb->execute();
|
||||
if (0 === $stmt->rowCount()) {
|
||||
$stmt = $qb->executeQuery();
|
||||
if ($stmt->rowCount() === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ScheduledDataflow::createFromArray($this->initDateTime($this->initArray($stmt->fetch(\PDO::FETCH_ASSOC))));
|
||||
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,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
5
src/Resources/config/exceptions_services.yaml
Normal file
5
src/Resources/config/exceptions_services.yaml
Normal file
@@ -0,0 +1,5 @@
|
||||
services:
|
||||
CodeRhapsodie\DataflowBundle\ExceptionsHandler\ExceptionHandlerInterface: '@CodeRhapsodie\DataflowBundle\ExceptionsHandler\FilesystemExceptionHandler'
|
||||
CodeRhapsodie\DataflowBundle\ExceptionsHandler\FilesystemExceptionHandler:
|
||||
arguments:
|
||||
$filesystem: ~ # Filled in compiler pass
|
||||
12
src/Resources/config/messenger_services.yaml
Normal file
12
src/Resources/config/messenger_services.yaml
Normal file
@@ -0,0 +1,12 @@
|
||||
services:
|
||||
CodeRhapsodie\DataflowBundle\Runner\PendingDataflowRunnerInterface: '@CodeRhapsodie\DataflowBundle\Runner\MessengerDataflowRunner'
|
||||
CodeRhapsodie\DataflowBundle\Runner\MessengerDataflowRunner:
|
||||
arguments:
|
||||
$repository: '@CodeRhapsodie\DataflowBundle\Repository\JobRepository'
|
||||
$bus: ~ # Filled in compiler pass
|
||||
|
||||
CodeRhapsodie\DataflowBundle\MessengerMode\JobMessageHandler:
|
||||
arguments:
|
||||
$repository: '@CodeRhapsodie\DataflowBundle\Repository\JobRepository'
|
||||
$processor: '@CodeRhapsodie\DataflowBundle\Processor\JobProcessorInterface'
|
||||
tags: ['messenger.message_handler']
|
||||
@@ -23,11 +23,12 @@ services:
|
||||
arguments:
|
||||
$registry: '@CodeRhapsodie\DataflowBundle\Registry\DataflowTypeRegistryInterface'
|
||||
$connectionFactory: '@CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory'
|
||||
$jobRepository: '@CodeRhapsodie\DataflowBundle\Repository\JobRepository'
|
||||
tags: ['console.command']
|
||||
|
||||
CodeRhapsodie\DataflowBundle\Command\JobShowCommand:
|
||||
arguments:
|
||||
$jobRepository: '@CodeRhapsodie\DataflowBundle\Repository\JobRepository'
|
||||
$jobGateway: '@CodeRhapsodie\DataflowBundle\Gateway\JobGateway'
|
||||
$connectionFactory: '@CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory'
|
||||
tags: ['console.command']
|
||||
|
||||
@@ -45,9 +46,15 @@ services:
|
||||
tags: ['console.command']
|
||||
|
||||
CodeRhapsodie\DataflowBundle\Command\SchemaCommand:
|
||||
deprecated:
|
||||
package: 'code-rhapsodie/dataflow-bundle'
|
||||
version: '5.0'
|
||||
tags: ['console.command']
|
||||
|
||||
CodeRhapsodie\DataflowBundle\Command\DatabaseSchemaCommand:
|
||||
arguments:
|
||||
$connectionFactory: '@CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory'
|
||||
tags: ['console.command']
|
||||
tags: [ 'console.command' ]
|
||||
|
||||
CodeRhapsodie\DataflowBundle\Repository\ScheduledDataflowRepository:
|
||||
lazy: true
|
||||
@@ -61,7 +68,7 @@ services:
|
||||
|
||||
coderhapsodie.dataflow.connection.internal:
|
||||
lazy: true
|
||||
class: Doctrine\DBAL\Driver\Connection
|
||||
class: Doctrine\DBAL\Connection
|
||||
factory: ['@CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory', 'getConnection']
|
||||
|
||||
CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory:
|
||||
@@ -76,7 +83,20 @@ services:
|
||||
|
||||
CodeRhapsodie\DataflowBundle\Runner\PendingDataflowRunnerInterface: '@CodeRhapsodie\DataflowBundle\Runner\PendingDataflowRunner'
|
||||
CodeRhapsodie\DataflowBundle\Runner\PendingDataflowRunner:
|
||||
arguments:
|
||||
$repository: '@CodeRhapsodie\DataflowBundle\Repository\JobRepository'
|
||||
$processor: '@CodeRhapsodie\DataflowBundle\Processor\JobProcessorInterface'
|
||||
|
||||
CodeRhapsodie\DataflowBundle\Processor\JobProcessorInterface: '@CodeRhapsodie\DataflowBundle\Processor\JobProcessor'
|
||||
CodeRhapsodie\DataflowBundle\Processor\JobProcessor:
|
||||
arguments:
|
||||
$repository: '@CodeRhapsodie\DataflowBundle\Repository\JobRepository'
|
||||
$registry: '@CodeRhapsodie\DataflowBundle\Registry\DataflowTypeRegistryInterface'
|
||||
$dispatcher: '@event_dispatcher'
|
||||
$jobGateway: '@CodeRhapsodie\DataflowBundle\Gateway\JobGateway'
|
||||
CodeRhapsodie\DataflowBundle\ExceptionsHandler\ExceptionHandlerInterface: '@CodeRhapsodie\DataflowBundle\ExceptionsHandler\NullExceptionHandler'
|
||||
CodeRhapsodie\DataflowBundle\ExceptionsHandler\NullExceptionHandler:
|
||||
CodeRhapsodie\DataflowBundle\Gateway\JobGateway:
|
||||
arguments:
|
||||
$repository: '@CodeRhapsodie\DataflowBundle\Repository\JobRepository'
|
||||
$exceptionHandler: '@CodeRhapsodie\DataflowBundle\ExceptionsHandler\ExceptionHandlerInterface'
|
||||
|
||||
26
src/Runner/MessengerDataflowRunner.php
Normal file
26
src/Runner/MessengerDataflowRunner.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Runner;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\Entity\Job;
|
||||
use CodeRhapsodie\DataflowBundle\MessengerMode\JobMessage;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
|
||||
class MessengerDataflowRunner implements PendingDataflowRunnerInterface
|
||||
{
|
||||
public function __construct(private JobRepository $repository, private MessageBusInterface $bus)
|
||||
{
|
||||
}
|
||||
|
||||
public function runPendingDataflows(): void
|
||||
{
|
||||
while (null !== ($job = $this->repository->findNextPendingDataflow())) {
|
||||
$this->bus->dispatch(new JobMessage($job->getId()));
|
||||
$job->setStatus(Job::STATUS_QUEUED);
|
||||
$this->repository->save($job);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,81 +4,19 @@ declare(strict_types=1);
|
||||
|
||||
namespace CodeRhapsodie\DataflowBundle\Runner;
|
||||
|
||||
use CodeRhapsodie\DataflowBundle\DataflowType\Result;
|
||||
use CodeRhapsodie\DataflowBundle\Entity\Job;
|
||||
use CodeRhapsodie\DataflowBundle\Event\Events;
|
||||
use CodeRhapsodie\DataflowBundle\Event\ProcessingEvent;
|
||||
use CodeRhapsodie\DataflowBundle\Registry\DataflowTypeRegistryInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Processor\JobProcessorInterface;
|
||||
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
class PendingDataflowRunner implements PendingDataflowRunnerInterface
|
||||
{
|
||||
/** @var JobRepository */
|
||||
private $repository;
|
||||
|
||||
/** @var DataflowTypeRegistryInterface */
|
||||
private $registry;
|
||||
|
||||
/** @var EventDispatcherInterface */
|
||||
private $dispatcher;
|
||||
|
||||
public function __construct(JobRepository $repository, DataflowTypeRegistryInterface $registry, EventDispatcherInterface $dispatcher)
|
||||
public function __construct(private JobRepository $repository, private JobProcessorInterface $processor)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
$this->registry = $registry;
|
||||
$this->dispatcher = $dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function runPendingDataflows(): void
|
||||
{
|
||||
while (null !== ($job = $this->repository->findNextPendingDataflow())) {
|
||||
$this->beforeProcessing($job);
|
||||
|
||||
$dataflowType = $this->registry->getDataflowType($job->getDataflowType());
|
||||
$result = $dataflowType->process($job->getOptions());
|
||||
|
||||
$this->afterProcessing($job, $result);
|
||||
$this->processor->process($job);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Job $job
|
||||
*/
|
||||
private function beforeProcessing(Job $job): void
|
||||
{
|
||||
$this->dispatcher->dispatch(Events::BEFORE_PROCESSING, new ProcessingEvent($job));
|
||||
|
||||
$job
|
||||
->setStatus(Job::STATUS_RUNNING)
|
||||
->setStartTime(new \DateTime())
|
||||
;
|
||||
$this->repository->save($job);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Job $job
|
||||
* @param Result $result
|
||||
*/
|
||||
private function afterProcessing(Job $job, Result $result): void
|
||||
{
|
||||
$exceptions = [];
|
||||
/** @var \Exception $exception */
|
||||
foreach ($result->getExceptions() as $exception) {
|
||||
$exceptions[] = (string) $exception;
|
||||
}
|
||||
|
||||
$job
|
||||
->setEndTime($result->getEndTime())
|
||||
->setStatus(Job::STATUS_COMPLETED)
|
||||
->setCount($result->getSuccessCount())
|
||||
->setExceptions($exceptions)
|
||||
;
|
||||
$this->repository->save($job);
|
||||
|
||||
$this->dispatcher->dispatch(Events::AFTER_PROCESSING, new ProcessingEvent($job));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,10 +19,10 @@ class DataflowSchemaProvider
|
||||
{
|
||||
$schema = new Schema();
|
||||
$tableJob = $schema->createTable(JobRepository::TABLE_NAME);
|
||||
$tableJob->addColumn('id', 'integer', array(
|
||||
$tableJob->addColumn('id', 'integer', [
|
||||
'autoincrement' => true,
|
||||
));
|
||||
$tableJob->setPrimaryKey(array('id'));
|
||||
]);
|
||||
$tableJob->setPrimaryKey(['id']);
|
||||
|
||||
$tableJob->addColumn('scheduled_dataflow_id', 'integer', ['notnull' => false]);
|
||||
$tableJob->addColumn('status', 'integer', ['notnull' => true]);
|
||||
@@ -36,10 +36,10 @@ class DataflowSchemaProvider
|
||||
$tableJob->addColumn('end_time', 'datetime', ['notnull' => false]);
|
||||
|
||||
$tableSchedule = $schema->createTable(ScheduledDataflowRepository::TABLE_NAME);
|
||||
$tableSchedule->addColumn('id', 'integer', array(
|
||||
$tableSchedule->addColumn('id', 'integer', [
|
||||
'autoincrement' => true,
|
||||
));
|
||||
$tableSchedule->setPrimaryKey(array('id'));
|
||||
]);
|
||||
$tableSchedule->setPrimaryKey(['id']);
|
||||
$tableSchedule->addColumn('label', 'string', ['notnull' => true, 'length' => 255]);
|
||||
$tableSchedule->addColumn('dataflow_type', 'string', ['notnull' => true, 'length' => 255]);
|
||||
$tableSchedule->addColumn('options', 'json', ['notnull' => true]);
|
||||
@@ -47,7 +47,8 @@ class DataflowSchemaProvider
|
||||
$tableSchedule->addColumn('next', 'datetime', ['notnull' => false]);
|
||||
$tableSchedule->addColumn('enabled', 'boolean', ['notnull' => true]);
|
||||
|
||||
$tableJob->addForeignKeyConstraint($tableSchedule, ['scheduled_dataflow_id'], ['id']);
|
||||
$tableJob->addForeignKeyConstraint($tableSchedule->getName(), ['scheduled_dataflow_id'], ['id']);
|
||||
$tableJob->addIndex(['status'], 'idx_status');
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
@@ -10,20 +10,22 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
|
||||
class FrequencyValidator extends ConstraintValidator
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($value, Constraint $constraint)
|
||||
public function validate(mixed $value, Constraint $constraint)
|
||||
{
|
||||
if (!$constraint instanceof Frequency) {
|
||||
throw new UnexpectedTypeException($constraint, Frequency::class);
|
||||
}
|
||||
|
||||
if (null === $value) {
|
||||
if ($value === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$interval = @\DateInterval::createFromDateString($value);
|
||||
try {
|
||||
$interval = \DateInterval::createFromDateString($value);
|
||||
} catch (\Exception) {
|
||||
$interval = false;
|
||||
}
|
||||
|
||||
if (!$interval) {
|
||||
$this->context->buildViolation($constraint->message)
|
||||
->setParameter('{{ string }}', $value)
|
||||
@@ -42,8 +44,6 @@ class FrequencyValidator extends ConstraintValidator
|
||||
->setParameter('{{ string }}', $value)
|
||||
->addViolation()
|
||||
;
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user