Skip to content

Commit

Permalink
Merge branch '1.1.0-dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
imTigger committed Mar 26, 2020
2 parents 4e49140 + 89ba14b commit cf64ee9
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 141 deletions.
21 changes: 0 additions & 21 deletions CHANGELOG.md

This file was deleted.

50 changes: 36 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
Expand All @@ -57,27 +57,38 @@ 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.

If you would like the job_id to be stored immediately you can add the `LaravelJobStatusServiceProvider` to your `config/app.php`, which tells laravel to use our `Dispatcher`.
```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

Expand Down Expand Up @@ -148,7 +159,9 @@ class YourController {
<?php
$jobStatus = JobStatus::find($jobStatusId);
```
### Common Caveat
### Troubleshooting

#### Call to undefined method ...->getJobStatusId()

Laravel provide many ways to dispatch Jobs. Not all methods return your Job object, for example:

Expand All @@ -157,14 +170,23 @@ 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.

2. In your job, generate your own unique key and pass into `prepareStatus();`, `$this->prepareStatus(['key' => $params['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
Expand Down
101 changes: 50 additions & 51 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,54 +1,53 @@
{
"name": "imtigger/laravel-job-status",
"description": "Laravel Job Status",
"license": "MIT",
"authors": [
{
"name": "Tiger",
"email": "[email protected]"
}
],
"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": "[email protected]"
}
],
"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"
}
}
1 change: 1 addition & 0 deletions config/job-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
return [
'model' => \Imtigger\LaravelJobStatus\JobStatus::class,
'event_manager' => \Imtigger\LaravelJobStatus\EventManagers\DefaultEventManager::class,
'database_connection' => null
];
3 changes: 1 addition & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true"
colors="true"
printerClass="Sempro\PHPUnitPrettyPrinter\PrettyPrinter">
colors="true">
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
Expand Down
2 changes: 1 addition & 1 deletion src/JobStatusUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 4 additions & 15 deletions src/Trackable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'));

Expand All @@ -74,13 +72,4 @@ public function getJobStatusId()
{
return $this->statusId;
}

public function __sleep()
{
if (!$this->statusId && $this->shouldTrack) {
$this->prepareStatus();
}

return $this->traitSleep();
}
}
16 changes: 1 addition & 15 deletions tests/Feature/TrackableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down
22 changes: 0 additions & 22 deletions tests/_data/TestJobWithoutConstruct.php

This file was deleted.

1 change: 1 addition & 0 deletions tests/_data/TestJobWithoutTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class TestJobWithoutTracking implements ShouldQueue, TrackableJob
public function __construct()
{
$this->shouldTrack = false;
$this->prepareStatus();
}

public function handle()
Expand Down

0 comments on commit cf64ee9

Please sign in to comment.