-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2be7881
Showing
36 changed files
with
1,890 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#.github/workflows/php.yml | ||
name: Tests | ||
|
||
on: | ||
push: ~ | ||
pull_request: ~ | ||
|
||
jobs: | ||
build: | ||
runs-on: ${{ matrix.operating-system }} | ||
name: PHP ${{ matrix.php }} and Symfony ${{ matrix.symfony }} | ||
strategy: | ||
matrix: | ||
operating-system: [ ubuntu-latest, macos-latest ] | ||
php: [ '8.3' ] | ||
symfony: [ '6.4.*', '7.0.*' ] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Setup PHP ${{ matrix.php }} | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php }} | ||
tools: flex | ||
|
||
- name: Download dependencies (Symfony ${{ matrix.symfony }}) | ||
env: | ||
SYMFONY_REQUIRE: ${{ matrix.symfony }} | ||
uses: ramsey/composer-install@v2 | ||
|
||
- name: Tests on ${{ matrix.operating-system }} | ||
run: ./vendor/bin/phpunit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
vendor/ | ||
tests/var/ | ||
.phpunit.result.cache | ||
composer.lock | ||
tests/app/var/data.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
# JobBundle | ||
|
||
JobBundle provides a way to handle long-running tasks in symfony. | ||
|
||
## Resources | ||
|
||
- [Documentation](./docs/index.md) | ||
- [Report issues](https://github.com/SoureCode/JobBundle/issues) | ||
- [Send Pull Requests](https://github.com/SoureCode/JobBundle/pulls) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"name": "sourecode/job-bundle", | ||
"type": "symfony-bundle", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"SoureCode\\Bundle\\Job\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"SoureCode\\Bundle\\Job\\Tests\\": "tests/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "chapterjason", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=8.3", | ||
"symfony/http-kernel": "^6.4 || ^7.0", | ||
"symfony/console": "^6.4 || ^7.0", | ||
"symfony/process": "^6.4 || ^7.0", | ||
"symfony/config": "^6.4 || ^7.0", | ||
"symfony/dependency-injection": "^6.4 || ^7.0", | ||
"symfony/finder": "^6.4 || ^7.0", | ||
"doctrine/orm": "^2.16 || ^3.0", | ||
"doctrine/doctrine-bundle": "^2.10", | ||
"symfony/messenger": "^6.4 || ^7.0", | ||
"symfony/serializer": "^6.4 || ^7.0", | ||
"symfony/property-access": "^6.4 || ^7.0" | ||
}, | ||
"require-dev": { | ||
"nyholm/symfony-bundle-test": "^2.0", | ||
"phpunit/phpunit": "^9.5", | ||
"symfony/phpunit-bridge": "^6.4 || ^7.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
# JobBundle | ||
|
||
## Requirements | ||
|
||
- PHP 8.2 or higher | ||
- Symfony 6.3 or higher | ||
|
||
## Commands | ||
|
||
- [`job:run`](./job-run.md) - Runs a job. | ||
|
||
## Examples | ||
|
||
```php | ||
use SoureCode\Bundle\Job\Manager\JobManager; | ||
|
||
$jobManager = $container->get(JobManager::class); | ||
|
||
// run a job bound to given entity | ||
$jobManager->dispatch($entity, new MakeJob()); | ||
|
||
// Run a job bound to a string key | ||
$jobManager->dispatch("test", new DoJob()); | ||
``` | ||
|
||
## Installation | ||
|
||
Make sure Composer is installed globally, as explained in the | ||
[installation chapter](https://getcomposer.org/doc/00-intro.md) | ||
of the Composer documentation. | ||
|
||
### Applications that use Symfony Flex | ||
|
||
Open a command console, enter your project directory and execute: | ||
|
||
```console | ||
composer require sourecode/job-bundle | ||
``` | ||
|
||
### Applications that don't use Symfony Flex | ||
|
||
#### Step 1: Download the Bundle | ||
|
||
Open a command console, enter your project directory and execute the | ||
following command to download the latest stable version of this bundle: | ||
|
||
```console | ||
composer require sourecode/job-bundle | ||
``` | ||
|
||
#### Step 2: Enable the Bundle | ||
|
||
Then, enable the bundle by adding it to the list of registered bundles | ||
in the `config/bundles.php` file of your project: | ||
|
||
```php | ||
// config/bundles.php | ||
|
||
return [ | ||
// ... | ||
\SoureCode\Bundle\Job\SoureCodeJobBundle::class => ['all' => true], | ||
]; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
# Command: job:run | ||
|
||
## Usage | ||
|
||
```shell | ||
Description: | ||
Run a job | ||
|
||
Usage: | ||
job:run <id> | ||
|
||
Arguments: | ||
id The job id | ||
|
||
Options: | ||
-h, --help Display help for the given command. When no command is given display help for the list command | ||
-q, --quiet Do not output any message | ||
-V, --version Display this application version | ||
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output | ||
-n, --no-interaction Do not ask any interactive question | ||
-e, --env=ENV The Environment name. [default: "dev"] | ||
--no-debug Switch off debug mode. | ||
--profile Enables profiling (requires debug). | ||
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="false" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
bootstrap="./vendor/autoload.php" | ||
> | ||
<php> | ||
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" /> | ||
</php> | ||
<coverage> | ||
<include> | ||
<directory suffix=".php">./</directory> | ||
</include> | ||
<exclude> | ||
<directory>vendor</directory> | ||
<directory>tests</directory> | ||
</exclude> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env bash | ||
|
||
CURRENT_DIRECTORY="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
PROJECT_DIRECTORY="$(dirname "$CURRENT_DIRECTORY")" | ||
DOCS_DIRECTORY="$PROJECT_DIRECTORY/docs" | ||
TESTS_DIRECTORY="$PROJECT_DIRECTORY/tests" | ||
BIN_DIRECTORY="$TESTS_DIRECTORY/app/bin" | ||
CONSOLE="$BIN_DIRECTORY/console" | ||
|
||
COMMANDS="job:run" | ||
|
||
NEWLINE=$'\n' | ||
|
||
for COMMAND in $COMMANDS; do | ||
MD_FILE_NAME="$(echo "$COMMAND" | tr ':' '-')" | ||
MARKDOWN="${NEWLINE}# Command: ${COMMAND}${NEWLINE}${NEWLINE}## Usage${NEWLINE}${NEWLINE}\`\`\`shell${NEWLINE}" | ||
USAGE="$("$CONSOLE" "$COMMAND" --help -vvv)" | ||
MARKDOWN="${MARKDOWN}${USAGE}${NEWLINE}\`\`\`${NEWLINE}${NEWLINE}" | ||
|
||
echo "$MARKDOWN" >"$DOCS_DIRECTORY/$MD_FILE_NAME.md" | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace SoureCode\Bundle\Job\Attribute; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
class AsJobHandler | ||
{ | ||
public function __construct( | ||
/** | ||
* @var class-string | ||
*/ | ||
public string $handle, | ||
) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace SoureCode\Bundle\Job\Command; | ||
|
||
use SoureCode\Bundle\Job\Job\Runner; | ||
use SoureCode\Bundle\Job\Manager\JobManager; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Command\SignalableCommandInterface; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
#[AsCommand( | ||
name: 'job:run', | ||
description: 'Run a job', | ||
)] | ||
final class JobRunCommand extends Command implements SignalableCommandInterface | ||
{ | ||
|
||
public function __construct( | ||
private readonly JobManager $jobManager, | ||
private readonly Runner $jobRunner, | ||
) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->addArgument('id', InputArgument::REQUIRED, 'The job id'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$id = (int)$input->getArgument('id'); | ||
$job = $this->jobManager->get($id); | ||
|
||
return $this->jobRunner->run($job); | ||
} | ||
|
||
public function getSubscribedSignals(): array | ||
{ | ||
return [ | ||
2, // SIGINT | ||
15, // SIGTERM | ||
]; | ||
} | ||
|
||
public function handleSignal(int $signal, int|false $previousExitCode = 0): false|int | ||
{ | ||
return $this->jobRunner->signal($signal); | ||
} | ||
} |
Oops, something went wrong.