Skip to content

Commit cdf9c4b

Browse files
authored
Merge pull request #38 from webparking/feature/better-id-capture
Overhaul, new major release?
2 parents d2bc166 + fc4f7e7 commit cdf9c4b

28 files changed

+979
-114
lines changed

.travis.yml

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,51 @@
11
language: php
22

3-
php:
4-
- 5.6
5-
- 7.1
6-
7-
sudo: false
8-
9-
# cache vendor dirs
103
cache:
114
directories:
12-
- vendor
135
- $HOME/.composer/cache
146

15-
before_script:
16-
- composer self-update
17-
- composer install --no-interaction
7+
services:
8+
- mysql
9+
10+
matrix:
11+
fast_finish: true
12+
include:
13+
- php: 7.1
14+
env: LARAVEL='5.5.*' TESTBENCH='3.5.*' COMPOSER_FLAGS='--prefer-lowest'
15+
- php: 7.1
16+
env: LARAVEL='5.6.*' TESTBENCH='3.6.*'
17+
- php: 7.1
18+
env: LARAVEL='5.7.*' TESTBENCH='3.7.*'
19+
- php: 7.1
20+
env: LARAVEL='5.8.*' TESTBENCH='3.8.*'
21+
- php: 7.2
22+
env: LARAVEL='5.5.*' TESTBENCH='3.5.*'
23+
- php: 7.2
24+
env: LARAVEL='5.6.*' TESTBENCH='3.6.*'
25+
- php: 7.2
26+
env: LARAVEL='5.7.*' TESTBENCH='3.7.*'
27+
- php: 7.2
28+
env: LARAVEL='5.8.*' TESTBENCH='3.8.*'
29+
- php: 7.3
30+
env: LARAVEL='5.5.*' TESTBENCH='3.5.*'
31+
- php: 7.3
32+
env: LARAVEL='5.6.*' TESTBENCH='3.6.*'
33+
- php: 7.3
34+
env: LARAVEL='5.7.*' TESTBENCH='3.7.*'
35+
- php: 7.3
36+
env: LARAVEL='5.8.*' TESTBENCH='3.8.*'
37+
38+
before_install:
39+
- mysql -e 'CREATE DATABASE job_status;'
40+
- travis_retry composer self-update
41+
- COMPOSER_MEMORY_LIMIT=-1 travis_retry composer require --no-update --no-interaction "illuminate/support:${LARAVEL}" "illuminate/database:${LARAVEL}" "orchestra/testbench:${TESTBENCH}"
42+
43+
install:
44+
- COMPOSER_MEMORY_LIMIT=-1 travis_retry composer update --no-interaction --no-plugins --no-suggest --prefer-source ${COMPOSER_FLAGS}
1845

1946
script:
20-
- composer test
47+
- vendor/bin/php-cs-fixer fix --config=.php_cs -v --dry-run --stop-on-violation
48+
- vendor/bin/phpunit
49+
50+
notifications:
51+
email: false

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,28 @@ php artisan vendor:publish --provider="Imtigger\LaravelJobStatus\LaravelJobStatu
5757
php artisan migrate
5858
```
5959

60+
#### 4. Improve job_id capture (optional)
61+
62+
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.
63+
64+
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`.
65+
```php
66+
'providers' => [
67+
...
68+
Imtigger\LaravelJobStatus\LaravelJobStatusServiceProvider::class,
69+
]
70+
```
71+
72+
#### 5. Use a custom JobStatus model
73+
74+
To use your own JobStatus model you can change the model in `config/job-status.php`
75+
76+
```php
77+
return [
78+
'model' => App\JobStatus::class,
79+
];
80+
```
81+
6082
### Usage
6183

6284
In your `Job`, use `Trackable` trait and call `$this->prepareStatus()` in constructor.

composer.json

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@
1414
"queue"
1515
],
1616
"require": {
17-
"php": ">=5.6.4",
18-
"illuminate/contracts": ">=5.3",
19-
"illuminate/database": ">=5.3",
20-
"illuminate/queue": ">=5.3",
21-
"illuminate/support": ">=5.3",
17+
"php": ">=7.1",
18+
"illuminate/contracts": ">=5.5",
19+
"illuminate/database": ">=5.5",
20+
"illuminate/queue": ">=5.5",
21+
"illuminate/support": ">=5.5",
2222
"nesbot/carbon": ">=1.21",
2323
"ext-json": "*"
2424
},
2525
"require-dev": {
2626
"phpunit/phpunit": ">=5.7",
27-
"orchestra/testbench": ">=3.4",
28-
"friendsofphp/php-cs-fixer": "^2.11"
27+
"orchestra/testbench": ">=3.5",
28+
"orchestra/database": ">=3.5",
29+
"friendsofphp/php-cs-fixer": "^2.11",
30+
"sempro/phpunit-pretty-print": "^1.1"
2931
},
3032
"extra": {
3133
"laravel": {
@@ -41,7 +43,8 @@
4143
},
4244
"autoload-dev": {
4345
"psr-4": {
44-
"Imtigger\\LaravelJobStatus\\": "tests"
46+
"Imtigger\\LaravelJobStatus\\Tests\\": "tests",
47+
"Imtigger\\LaravelJobStatus\\Tests\\Data\\": "tests/_data"
4548
}
4649
},
4750
"scripts": {

config/job-status.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
return [
44
'model' => \Imtigger\LaravelJobStatus\JobStatus::class,
5+
'event_manager' => \Imtigger\LaravelJobStatus\EventManagers\DefaultEventManager::class,
56
];

phpunit.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
beStrictAboutCoversAnnotation="true"
77
beStrictAboutOutputDuringTests="true"
88
beStrictAboutTodoAnnotatedTests="true"
9-
verbose="true">
9+
verbose="true"
10+
colors="true"
11+
printerClass="Sempro\PHPUnitPrettyPrinter\PrettyPrinter">
1012
<testsuite name="default">
1113
<directory suffix="Test.php">tests</directory>
1214
</testsuite>
@@ -16,4 +18,11 @@
1618
<directory suffix=".php">src</directory>
1719
</whitelist>
1820
</filter>
21+
<php>
22+
<env name="DB_HOST" value="127.0.0.1"/>
23+
<env name="DB_DATABASE" value="job_status"/>
24+
<env name="DB_USERNAME" value="root"/>
25+
<env name="DB_PASSWORD" value=""/>
26+
<env name="APP_KEY" value="base64:c4nauF70phCYhDTA71kqmrJy+jv6CA+85PNurbM/PPw="/>
27+
</php>
1928
</phpunit>

src/Dispatcher.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Imtigger\LaravelJobStatus;
4+
5+
use Closure;
6+
use Illuminate\Contracts\Container\Container;
7+
8+
class Dispatcher extends \Illuminate\Bus\Dispatcher
9+
{
10+
private $updater;
11+
12+
public function __construct(Container $container, Closure $queueResolver, JobStatusUpdater $updater)
13+
{
14+
$this->updater = $updater;
15+
16+
parent::__construct($container, $queueResolver);
17+
}
18+
19+
public function dispatch($command)
20+
{
21+
$result = parent::dispatch($command);
22+
23+
$this->updater->update($command, [
24+
'job_id' => $result,
25+
]);
26+
27+
return $result;
28+
}
29+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Imtigger\LaravelJobStatus\EventManagers;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Queue\Events\JobExceptionOccurred;
7+
use Illuminate\Queue\Events\JobFailed;
8+
use Illuminate\Queue\Events\JobProcessed;
9+
use Illuminate\Queue\Events\JobProcessing;
10+
11+
class DefaultEventManager extends EventManager
12+
{
13+
public function before(JobProcessing $event): void
14+
{
15+
$this->getUpdater()->update($event, [
16+
'status' => $this->getEntity()::STATUS_EXECUTING,
17+
'job_id' => $event->job->getJobId(),
18+
'queue' => $event->job->getQueue(),
19+
'started_at' => Carbon::now(),
20+
]);
21+
}
22+
23+
public function after(JobProcessed $event): void
24+
{
25+
$this->getUpdater()->update($event, [
26+
'status' => $this->getEntity()::STATUS_FINISHED,
27+
'finished_at' => Carbon::now(),
28+
]);
29+
}
30+
31+
public function failing(JobFailed $event): void
32+
{
33+
$this->getUpdater()->update($event, [
34+
'status' => $this->getEntity()::STATUS_FAILED,
35+
'finished_at' => Carbon::now(),
36+
]);
37+
}
38+
39+
public function exceptionOccurred(JobExceptionOccurred $event): void
40+
{
41+
$this->getUpdater()->update($event, [
42+
'status' => $this->getEntity()::STATUS_RETRYING,
43+
'finished_at' => Carbon::now(),
44+
'output' => ['message' => $event->exception->getMessage()],
45+
]);
46+
}
47+
}

src/EventManagers/EventManager.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Imtigger\LaravelJobStatus\EventManagers;
4+
5+
use Illuminate\Queue\Events\JobExceptionOccurred;
6+
use Illuminate\Queue\Events\JobFailed;
7+
use Illuminate\Queue\Events\JobProcessed;
8+
use Illuminate\Queue\Events\JobProcessing;
9+
use Imtigger\LaravelJobStatus\JobStatus;
10+
use Imtigger\LaravelJobStatus\JobStatusUpdater;
11+
12+
abstract class EventManager
13+
{
14+
abstract public function before(JobProcessing $event): void;
15+
16+
abstract public function after(JobProcessed $event): void;
17+
18+
abstract public function failing(JobFailed $event): void;
19+
20+
abstract public function exceptionOccurred(JobExceptionOccurred $event): void;
21+
22+
private $updater;
23+
24+
private $entity;
25+
26+
public function __construct(JobStatusUpdater $updater)
27+
{
28+
$this->updater = $updater;
29+
$this->entity = app(config('job-status.model'));
30+
}
31+
32+
/**
33+
* @return JobStatusUpdater
34+
*/
35+
protected function getUpdater()
36+
{
37+
return $this->updater;
38+
}
39+
40+
/**
41+
* @return JobStatus
42+
*/
43+
protected function getEntity()
44+
{
45+
return $this->entity;
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Imtigger\LaravelJobStatus\EventManagers;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Queue\Events\JobExceptionOccurred;
7+
use Illuminate\Queue\Events\JobFailed;
8+
use Illuminate\Queue\Events\JobProcessed;
9+
use Illuminate\Queue\Events\JobProcessing;
10+
11+
class LegacyEventManager extends EventManager
12+
{
13+
public function before(JobProcessing $event): void
14+
{
15+
$this->getUpdater()->update($event, [
16+
'status' => $this->getEntity()::STATUS_EXECUTING,
17+
'job_id' => $event->job->getJobId(),
18+
'queue' => $event->job->getQueue(),
19+
'started_at' => Carbon::now(),
20+
]);
21+
}
22+
23+
public function after(JobProcessed $event): void
24+
{
25+
$this->getUpdater()->update($event, [
26+
'status' => $this->getEntity()::STATUS_FINISHED,
27+
'finished_at' => Carbon::now(),
28+
]);
29+
}
30+
31+
public function failing(JobFailed $event): void
32+
{
33+
$this->getUpdater()->update($event, [
34+
'status' => $this->getEntity()::STATUS_FAILED,
35+
'finished_at' => Carbon::now(),
36+
]);
37+
}
38+
39+
public function exceptionOccurred(JobExceptionOccurred $event): void
40+
{
41+
$this->getUpdater()->update($event, [
42+
'status' => $this->getEntity()::STATUS_FAILED,
43+
'finished_at' => Carbon::now(),
44+
'output' => ['message' => $event->exception->getMessage()],
45+
]);
46+
}
47+
}

src/JobStatus.php

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* @property mixed $is_executing
2525
* @property mixed $is_failed
2626
* @property mixed $is_finished
27+
* @property mixed $is_retrying
2728
* @method static \Illuminate\Database\Query\Builder|\Imtigger\LaravelJobStatus\JobStatus whereAttempts($value)
2829
* @method static \Illuminate\Database\Query\Builder|\Imtigger\LaravelJobStatus\JobStatus whereCreatedAt($value)
2930
* @method static \Illuminate\Database\Query\Builder|\Imtigger\LaravelJobStatus\JobStatus whereFinishedAt($value)
@@ -45,21 +46,17 @@ class JobStatus extends Model
4546
const STATUS_EXECUTING = 'executing';
4647
const STATUS_FINISHED = 'finished';
4748
const STATUS_FAILED = 'failed';
49+
const STATUS_RETRYING = 'retrying';
4850

4951
public $dates = ['started_at', 'finished_at', 'created_at', 'updated_at'];
5052
protected $guarded = [];
5153

52-
/* Accessor */
53-
public function getInputAttribute($value)
54-
{
55-
return json_decode($value, true);
56-
}
57-
58-
public function getOutputAttribute($value)
59-
{
60-
return json_decode($value, true);
61-
}
54+
protected $casts = [
55+
'input' => 'array',
56+
'output' => 'array',
57+
];
6258

59+
/* Accessor */
6360
public function getProgressPercentageAttribute()
6461
{
6562
return $this->progress_max !== 0 ? round(100 * $this->progress_now / $this->progress_max) : 0;
@@ -90,15 +87,9 @@ public function getIsQueuedAttribute()
9087
return $this->status === self::STATUS_QUEUED;
9188
}
9289

93-
/* Mutator */
94-
public function setInputAttribute($value)
95-
{
96-
$this->attributes['input'] = json_encode($value);
97-
}
98-
99-
public function setOutputAttribute($value)
90+
public function getIsRetryingAttribute()
10091
{
101-
$this->attributes['output'] = json_encode($value);
92+
return $this->status === self::STATUS_RETRYING;
10293
}
10394

10495
public static function getAllowedStatuses()
@@ -108,6 +99,7 @@ public static function getAllowedStatuses()
10899
self::STATUS_EXECUTING,
109100
self::STATUS_FINISHED,
110101
self::STATUS_FAILED,
102+
self::STATUS_RETRYING,
111103
];
112104
}
113105
}

0 commit comments

Comments
 (0)