diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 9191a80..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,21 +0,0 @@ -# Changelog - -## 0.1.11 -- Added ```incrementProgress(int $offset, int $every)``` method -- Added ability to override JobStatus class, with fallback when config is not set -- Database migration is now published instead of loaded directly, to allow customization - -## 0.1.10 -- Fixed compatibility of PHP 5.6 - -## 0.1.9 -- Automatic package discovery for Laravel 5.5, thanks to @PixellUp - -## 0.1.8 -- Fixed Job ID is not stored correctly in some case - -## 0.1.7 -- Fixed error for Job do not use Trackable - -## 0.1.6 -- Initial release diff --git a/README.md b/README.md index cf6e3cd..36207c5 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,8 @@ Laravel package to add ability to track `Job` progress, status and result dispat ## Requirements -- PHP >= 5.6.4 -- Laravel >= 5.3 +- PHP >= 7.1 +- Laravel >= 5.5 ## Installation @@ -45,7 +45,7 @@ Add the following to your `config/app.php`: ] ``` -#### 2. Publish migration and config +#### 2. Publish migration and config (optional) ```bash php artisan vendor:publish --provider="Imtigger\LaravelJobStatus\LaravelJobStatusServiceProvider" @@ -57,7 +57,18 @@ php artisan vendor:publish --provider="Imtigger\LaravelJobStatus\LaravelJobStatu php artisan migrate ``` -#### 4. Improve job_id capture (optional) +#### 4. Use a custom JobStatus model (optional) + +To use your own JobStatus model you can change the model in `config/job-status.php` + +```php +return [ + 'model' => App\JobStatus::class, +]; + +``` + +#### 5. Improve job_id capture (optional) The first laravel event that can be captured to insert the job_id into the JobStatus model is the Queue::before event. This means that the JobStatus won't have a job_id until it is being processed for the first time. @@ -65,19 +76,19 @@ If you would like the job_id to be stored immediately you can add the `LaravelJo ```php 'providers' => [ ... - Imtigger\LaravelJobStatus\LaravelJobStatusServiceProvider::class, + \Imtigger\LaravelJobStatus\LaravelJobStatusBusServiceProvider::class,, ] ``` -#### 5. Use a custom JobStatus model +#### 6. Setup dedicated database connection (optional) -To use your own JobStatus model you can change the model in `config/job-status.php` +Laravel support only one transcation per database connection. -```php -return [ - 'model' => App\JobStatus::class, -]; -``` +All changes made by JobStatus are also within transaction and therefore invisible to other connnections (e.g. progress page) + +If your job will update progress within transaction, copy your connection in `config/database.php` under another name like `'mysql-job-status'` with same config. + +Then set your connection to `'database_connection' => 'mysql-job-status'` in `config/job-status.php` ### Usage @@ -148,7 +159,9 @@ class YourController { getJobStatusId() Laravel provide many ways to dispatch Jobs. Not all methods return your Job object, for example: @@ -157,7 +170,7 @@ Laravel provide many ways to dispatch Jobs. Not all methods return your Job obje YourJob::dispatch(); // Returns PendingDispatch instead of YourJob object, leaving no way to retrive `$job->getJobStatusId();` ``` -Workarounds: Create your own key +If you really need to dispatch job in this way, workarounds needed: Create your own key 1. Create migration adding extra key to job_statuses table. @@ -165,6 +178,15 @@ Workarounds: Create your own key 3. Find JobStatus another way: `$jobStatus = JobStatus::whereKey($key)->firstOrFail();` +#### Status not updating until transaction commited + +On version >= 1.1, dedicated database connection support is added. + +Therefore JobStatus updates can be saved instantly even within your application transaction. + +Read setup step 6 for instructions. + + ## Documentations ```php diff --git a/composer.json b/composer.json index 0e5cae5..e4436e7 100644 --- a/composer.json +++ b/composer.json @@ -1,54 +1,53 @@ { - "name": "imtigger/laravel-job-status", - "description": "Laravel Job Status", - "license": "MIT", - "authors": [ - { - "name": "Tiger", - "email": "tiger@tiger-workshop.com" - } - ], - "keywords": [ - "laravel", - "job", - "queue" - ], - "require": { - "php": ">=7.1", - "illuminate/contracts": ">=5.5", - "illuminate/database": ">=5.5", - "illuminate/queue": ">=5.5", - "illuminate/support": ">=5.5", - "nesbot/carbon": ">=1.21", - "ext-json": "*" - }, - "require-dev": { - "phpunit/phpunit": ">=5.7", - "orchestra/testbench": ">=3.5", - "orchestra/database": ">=3.5", - "friendsofphp/php-cs-fixer": "^2.11", - "sempro/phpunit-pretty-print": "^1.1" - }, - "extra": { - "laravel": { - "providers": [ - "Imtigger\\LaravelJobStatus\\LaravelJobStatusServiceProvider" - ] - } - }, - "autoload": { - "psr-4": { - "Imtigger\\LaravelJobStatus\\": "src" - } - }, - "autoload-dev": { - "psr-4": { - "Imtigger\\LaravelJobStatus\\Tests\\": "tests", - "Imtigger\\LaravelJobStatus\\Tests\\Data\\": "tests/_data" - } - }, - "scripts": { - "php-cs-fixer": "vendor/bin/php-cs-fixer fix --config=.php_cs", - "test": "composer php-cs-fixer && vendor/bin/phpunit" + "name": "imtigger/laravel-job-status", + "description": "Laravel Job Status", + "license": "MIT", + "authors": [ + { + "name": "Tiger", + "email": "tiger@tiger-workshop.com" } + ], + "keywords": [ + "laravel", + "job", + "queue" + ], + "require": { + "php": ">=7.1", + "illuminate/contracts": ">=5.5", + "illuminate/database": ">=5.5", + "illuminate/queue": ">=5.5", + "illuminate/support": ">=5.5", + "nesbot/carbon": ">=1.21", + "ext-json": "*" + }, + "require-dev": { + "phpunit/phpunit": ">=5.7", + "orchestra/testbench": ">=3.5", + "orchestra/database": ">=3.5", + "friendsofphp/php-cs-fixer": "^2.11" + }, + "extra": { + "laravel": { + "providers": [ + "Imtigger\\LaravelJobStatus\\LaravelJobStatusServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Imtigger\\LaravelJobStatus\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "Imtigger\\LaravelJobStatus\\Tests\\": "tests", + "Imtigger\\LaravelJobStatus\\Tests\\Data\\": "tests/_data" + } + }, + "scripts": { + "php-cs-fixer": "vendor/bin/php-cs-fixer fix --config=.php_cs", + "test": "composer php-cs-fixer && vendor/bin/phpunit" + } } diff --git a/config/job-status.php b/config/job-status.php index 21078b6..6e7d199 100644 --- a/config/job-status.php +++ b/config/job-status.php @@ -3,4 +3,5 @@ return [ 'model' => \Imtigger\LaravelJobStatus\JobStatus::class, 'event_manager' => \Imtigger\LaravelJobStatus\EventManagers\DefaultEventManager::class, + 'database_connection' => null ]; diff --git a/phpunit.xml b/phpunit.xml index 8a62f1a..5aa83eb 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,8 +7,7 @@ beStrictAboutOutputDuringTests="true" beStrictAboutTodoAnnotatedTests="true" verbose="true" - colors="true" - printerClass="Sempro\PHPUnitPrettyPrinter\PrettyPrinter"> + colors="true"> tests diff --git a/src/JobStatusUpdater.php b/src/JobStatusUpdater.php index b466ba1..aaaff92 100644 --- a/src/JobStatusUpdater.php +++ b/src/JobStatusUpdater.php @@ -83,7 +83,7 @@ private function getJobStatus($job) /** @var JobStatus $entityClass */ $entityClass = app(config('job-status.model')); - return $entityClass::query()->where('id', '=', $id)->first(); + return $entityClass::on(config('job-status.database_connection'))->whereKey($id)->first(); } return null; diff --git a/src/Trackable.php b/src/Trackable.php index c00959e..a3581c3 100644 --- a/src/Trackable.php +++ b/src/Trackable.php @@ -2,14 +2,8 @@ namespace Imtigger\LaravelJobStatus; -use Illuminate\Queue\SerializesModels; - trait Trackable { - use SerializesModels { - __sleep as traitSleep; - } - /** @var int $statusId */ protected $statusId; protected $progressNow = 0; @@ -55,6 +49,10 @@ protected function update(array $data) protected function prepareStatus(array $data = []) { + if (!$this->shouldTrack) { + return; + } + /** @var JobStatus $entityClass */ $entityClass = app(config('job-status.model')); @@ -74,13 +72,4 @@ public function getJobStatusId() { return $this->statusId; } - - public function __sleep() - { - if (!$this->statusId && $this->shouldTrack) { - $this->prepareStatus(); - } - - return $this->traitSleep(); - } } diff --git a/tests/Feature/TrackableTest.php b/tests/Feature/TrackableTest.php index 7b295b6..b820fd4 100644 --- a/tests/Feature/TrackableTest.php +++ b/tests/Feature/TrackableTest.php @@ -9,7 +9,6 @@ use Imtigger\LaravelJobStatus\Tests\Data\TestJob; use Imtigger\LaravelJobStatus\Tests\Data\TestJobWithDatabase; use Imtigger\LaravelJobStatus\Tests\Data\TestJobWithException; -use Imtigger\LaravelJobStatus\Tests\Data\TestJobWithoutConstruct; use Imtigger\LaravelJobStatus\Tests\Data\TestJobWithoutTracking; class TrackableTest extends TestCase @@ -64,20 +63,7 @@ public function testStatusFailed() ]); } - public function testWithoutPrepareStatus() - { - $job = new TestJobWithoutConstruct(); - - $this->assertNull($job->getJobStatusId()); - - $this->assertEquals(0, JobStatus::query()->count()); - - app(Dispatcher::class)->dispatch($job); - - $this->assertEquals(1, JobStatus::query()->count()); - } - - public function testWithoutPrepareStatusAndTrackingDisabled() + public function testTrackingDisabled() { $job = new TestJobWithoutTracking(); diff --git a/tests/_data/TestJobWithoutConstruct.php b/tests/_data/TestJobWithoutConstruct.php deleted file mode 100644 index 093f055..0000000 --- a/tests/_data/TestJobWithoutConstruct.php +++ /dev/null @@ -1,22 +0,0 @@ -shouldTrack = false; + $this->prepareStatus(); } public function handle()