Skip to content

Commit cf64ee9

Browse files
committed
Merge branch '1.1.0-dev'
2 parents 4e49140 + 89ba14b commit cf64ee9

File tree

10 files changed

+95
-141
lines changed

10 files changed

+95
-141
lines changed

CHANGELOG.md

Lines changed: 0 additions & 21 deletions
This file was deleted.

README.md

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Laravel package to add ability to track `Job` progress, status and result dispat
2222

2323
## Requirements
2424

25-
- PHP >= 5.6.4
26-
- Laravel >= 5.3
25+
- PHP >= 7.1
26+
- Laravel >= 5.5
2727

2828
## Installation
2929

@@ -45,7 +45,7 @@ Add the following to your `config/app.php`:
4545
]
4646
```
4747

48-
#### 2. Publish migration and config
48+
#### 2. Publish migration and config (optional)
4949

5050
```bash
5151
php artisan vendor:publish --provider="Imtigger\LaravelJobStatus\LaravelJobStatusServiceProvider"
@@ -57,27 +57,38 @@ php artisan vendor:publish --provider="Imtigger\LaravelJobStatus\LaravelJobStatu
5757
php artisan migrate
5858
```
5959

60-
#### 4. Improve job_id capture (optional)
60+
#### 4. Use a custom JobStatus model (optional)
61+
62+
To use your own JobStatus model you can change the model in `config/job-status.php`
63+
64+
```php
65+
return [
66+
'model' => App\JobStatus::class,
67+
];
68+
69+
```
70+
71+
#### 5. Improve job_id capture (optional)
6172

6273
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.
6374

6475
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`.
6576
```php
6677
'providers' => [
6778
...
68-
Imtigger\LaravelJobStatus\LaravelJobStatusServiceProvider::class,
79+
\Imtigger\LaravelJobStatus\LaravelJobStatusBusServiceProvider::class,,
6980
]
7081
```
7182

72-
#### 5. Use a custom JobStatus model
83+
#### 6. Setup dedicated database connection (optional)
7384

74-
To use your own JobStatus model you can change the model in `config/job-status.php`
85+
Laravel support only one transcation per database connection.
7586

76-
```php
77-
return [
78-
'model' => App\JobStatus::class,
79-
];
80-
```
87+
All changes made by JobStatus are also within transaction and therefore invisible to other connnections (e.g. progress page)
88+
89+
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.
90+
91+
Then set your connection to `'database_connection' => 'mysql-job-status'` in `config/job-status.php`
8192

8293
### Usage
8394

@@ -148,7 +159,9 @@ class YourController {
148159
<?php
149160
$jobStatus = JobStatus::find($jobStatusId);
150161
```
151-
### Common Caveat
162+
### Troubleshooting
163+
164+
#### Call to undefined method ...->getJobStatusId()
152165

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

@@ -157,14 +170,23 @@ Laravel provide many ways to dispatch Jobs. Not all methods return your Job obje
157170
YourJob::dispatch(); // Returns PendingDispatch instead of YourJob object, leaving no way to retrive `$job->getJobStatusId();`
158171
```
159172

160-
Workarounds: Create your own key
173+
If you really need to dispatch job in this way, workarounds needed: Create your own key
161174

162175
1. Create migration adding extra key to job_statuses table.
163176

164177
2. In your job, generate your own unique key and pass into `prepareStatus();`, `$this->prepareStatus(['key' => $params['key']]);`
165178

166179
3. Find JobStatus another way: `$jobStatus = JobStatus::whereKey($key)->firstOrFail();`
167180

181+
#### Status not updating until transaction commited
182+
183+
On version >= 1.1, dedicated database connection support is added.
184+
185+
Therefore JobStatus updates can be saved instantly even within your application transaction.
186+
187+
Read setup step 6 for instructions.
188+
189+
168190
## Documentations
169191

170192
```php

composer.json

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,53 @@
11
{
2-
"name": "imtigger/laravel-job-status",
3-
"description": "Laravel Job Status",
4-
"license": "MIT",
5-
"authors": [
6-
{
7-
"name": "Tiger",
8-
"email": "tiger@tiger-workshop.com"
9-
}
10-
],
11-
"keywords": [
12-
"laravel",
13-
"job",
14-
"queue"
15-
],
16-
"require": {
17-
"php": ">=7.1",
18-
"illuminate/contracts": ">=5.5",
19-
"illuminate/database": ">=5.5",
20-
"illuminate/queue": ">=5.5",
21-
"illuminate/support": ">=5.5",
22-
"nesbot/carbon": ">=1.21",
23-
"ext-json": "*"
24-
},
25-
"require-dev": {
26-
"phpunit/phpunit": ">=5.7",
27-
"orchestra/testbench": ">=3.5",
28-
"orchestra/database": ">=3.5",
29-
"friendsofphp/php-cs-fixer": "^2.11",
30-
"sempro/phpunit-pretty-print": "^1.1"
31-
},
32-
"extra": {
33-
"laravel": {
34-
"providers": [
35-
"Imtigger\\LaravelJobStatus\\LaravelJobStatusServiceProvider"
36-
]
37-
}
38-
},
39-
"autoload": {
40-
"psr-4": {
41-
"Imtigger\\LaravelJobStatus\\": "src"
42-
}
43-
},
44-
"autoload-dev": {
45-
"psr-4": {
46-
"Imtigger\\LaravelJobStatus\\Tests\\": "tests",
47-
"Imtigger\\LaravelJobStatus\\Tests\\Data\\": "tests/_data"
48-
}
49-
},
50-
"scripts": {
51-
"php-cs-fixer": "vendor/bin/php-cs-fixer fix --config=.php_cs",
52-
"test": "composer php-cs-fixer && vendor/bin/phpunit"
2+
"name": "imtigger/laravel-job-status",
3+
"description": "Laravel Job Status",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Tiger",
8+
"email": "tiger@tiger-workshop.com"
539
}
10+
],
11+
"keywords": [
12+
"laravel",
13+
"job",
14+
"queue"
15+
],
16+
"require": {
17+
"php": ">=7.1",
18+
"illuminate/contracts": ">=5.5",
19+
"illuminate/database": ">=5.5",
20+
"illuminate/queue": ">=5.5",
21+
"illuminate/support": ">=5.5",
22+
"nesbot/carbon": ">=1.21",
23+
"ext-json": "*"
24+
},
25+
"require-dev": {
26+
"phpunit/phpunit": ">=5.7",
27+
"orchestra/testbench": ">=3.5",
28+
"orchestra/database": ">=3.5",
29+
"friendsofphp/php-cs-fixer": "^2.11"
30+
},
31+
"extra": {
32+
"laravel": {
33+
"providers": [
34+
"Imtigger\\LaravelJobStatus\\LaravelJobStatusServiceProvider"
35+
]
36+
}
37+
},
38+
"autoload": {
39+
"psr-4": {
40+
"Imtigger\\LaravelJobStatus\\": "src"
41+
}
42+
},
43+
"autoload-dev": {
44+
"psr-4": {
45+
"Imtigger\\LaravelJobStatus\\Tests\\": "tests",
46+
"Imtigger\\LaravelJobStatus\\Tests\\Data\\": "tests/_data"
47+
}
48+
},
49+
"scripts": {
50+
"php-cs-fixer": "vendor/bin/php-cs-fixer fix --config=.php_cs",
51+
"test": "composer php-cs-fixer && vendor/bin/phpunit"
52+
}
5453
}

config/job-status.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
return [
44
'model' => \Imtigger\LaravelJobStatus\JobStatus::class,
55
'event_manager' => \Imtigger\LaravelJobStatus\EventManagers\DefaultEventManager::class,
6+
'database_connection' => null
67
];

phpunit.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
beStrictAboutOutputDuringTests="true"
88
beStrictAboutTodoAnnotatedTests="true"
99
verbose="true"
10-
colors="true"
11-
printerClass="Sempro\PHPUnitPrettyPrinter\PrettyPrinter">
10+
colors="true">
1211
<testsuite name="default">
1312
<directory suffix="Test.php">tests</directory>
1413
</testsuite>

src/JobStatusUpdater.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private function getJobStatus($job)
8383
/** @var JobStatus $entityClass */
8484
$entityClass = app(config('job-status.model'));
8585

86-
return $entityClass::query()->where('id', '=', $id)->first();
86+
return $entityClass::on(config('job-status.database_connection'))->whereKey($id)->first();
8787
}
8888

8989
return null;

src/Trackable.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@
22

33
namespace Imtigger\LaravelJobStatus;
44

5-
use Illuminate\Queue\SerializesModels;
6-
75
trait Trackable
86
{
9-
use SerializesModels {
10-
__sleep as traitSleep;
11-
}
12-
137
/** @var int $statusId */
148
protected $statusId;
159
protected $progressNow = 0;
@@ -55,6 +49,10 @@ protected function update(array $data)
5549

5650
protected function prepareStatus(array $data = [])
5751
{
52+
if (!$this->shouldTrack) {
53+
return;
54+
}
55+
5856
/** @var JobStatus $entityClass */
5957
$entityClass = app(config('job-status.model'));
6058

@@ -74,13 +72,4 @@ public function getJobStatusId()
7472
{
7573
return $this->statusId;
7674
}
77-
78-
public function __sleep()
79-
{
80-
if (!$this->statusId && $this->shouldTrack) {
81-
$this->prepareStatus();
82-
}
83-
84-
return $this->traitSleep();
85-
}
8675
}

tests/Feature/TrackableTest.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Imtigger\LaravelJobStatus\Tests\Data\TestJob;
1010
use Imtigger\LaravelJobStatus\Tests\Data\TestJobWithDatabase;
1111
use Imtigger\LaravelJobStatus\Tests\Data\TestJobWithException;
12-
use Imtigger\LaravelJobStatus\Tests\Data\TestJobWithoutConstruct;
1312
use Imtigger\LaravelJobStatus\Tests\Data\TestJobWithoutTracking;
1413

1514
class TrackableTest extends TestCase
@@ -64,20 +63,7 @@ public function testStatusFailed()
6463
]);
6564
}
6665

67-
public function testWithoutPrepareStatus()
68-
{
69-
$job = new TestJobWithoutConstruct();
70-
71-
$this->assertNull($job->getJobStatusId());
72-
73-
$this->assertEquals(0, JobStatus::query()->count());
74-
75-
app(Dispatcher::class)->dispatch($job);
76-
77-
$this->assertEquals(1, JobStatus::query()->count());
78-
}
79-
80-
public function testWithoutPrepareStatusAndTrackingDisabled()
66+
public function testTrackingDisabled()
8167
{
8268
$job = new TestJobWithoutTracking();
8369

tests/_data/TestJobWithoutConstruct.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/_data/TestJobWithoutTracking.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class TestJobWithoutTracking implements ShouldQueue, TrackableJob
1919
public function __construct()
2020
{
2121
$this->shouldTrack = false;
22+
$this->prepareStatus();
2223
}
2324

2425
public function handle()

0 commit comments

Comments
 (0)