Skip to content

Commit c65acc6

Browse files
committed
Removed job ID from execute method
1 parent 1cbdcaa commit c65acc6

12 files changed

Lines changed: 52 additions & 51 deletions

File tree

README.md

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,30 @@ to the require section of your `composer.json` file.
3333
Basic Usage
3434
-----------
3535

36-
Job class example:
36+
Each task which is sent to queue should be defined as a separate class.
37+
For example, if you need to download and save a file the class may look like the following:
3738

3839
```php
3940
class DownloadJob extends Object implements \zhuravljov\yii\queue\Job
4041
{
4142
public $url;
4243
public $file;
4344

44-
/**
45-
* @param Queue $queue which handled the job
46-
* @param string|null $jobId job message ID
47-
*/
48-
public function execute($queue, $jobId)
45+
public function execute($queue)
4946
{
5047
file_put_contents($this->file, file_get_contents($this->url));
5148
}
5249
}
5350
```
5451

55-
Pushes job into queue:
52+
Here's how to send a task into queue:
5653

5754
```php
5855
Yii::$app->queue->push(new DownloadJob([
5956
'url' => 'http://example.com/image.jpg',
6057
'file' => '/tmp/image.jpg',
6158
]));
6259
```
63-
64-
Method of handling a queue depend on selected driver.
65-
6660
Pushes job into queue that run after 5 min:
6761

6862
```php
@@ -72,6 +66,24 @@ Yii::$app->queue->later(new DownloadJob([
7266
]), 5 * 60);
7367
```
7468

75-
But only some drivers support delayed running.
69+
The exact way task is executed depends on the driver used. The most part of drivers can be run using
70+
console commands, which the component registers in your application. For more details see documentation
71+
of a driver.
72+
73+
The component has ability to track status jobs which was pushed into queue.
74+
75+
```php
76+
// Push a job into queue and get massage ID.
77+
$id = Yii::$app->queue->push(new SomeJob());
78+
79+
// The job is waiting for execute.
80+
Yii::$app->queue->isWaiting($id);
81+
82+
// Worker gets the job from queue, end executing it.
83+
Yii::$app->queue->isReserved($id);
84+
85+
// Worker has executed the job.
86+
Yii::$app->queue->isDone($id);
87+
```
7688

7789
For more details see [the guide](docs/guide/README.md).

UPGRADE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Upgrade from 0.x to 1.0
1212

1313
* Some methods and constants was modified.
1414

15-
- Method `Job::run()` modified to `Job::execute($queue, $jobId)`.
15+
- Method `Job::run()` modified to `Job::execute($queue)`.
1616
- Const `Queue::EVENT_BEFORE_WORK` renamed to `Queue::EVENT_BEFORE_EXEC`.
1717
- Const `Queue::EVENT_AFTER_WORK` renamed to `Queue::EVENT_AFTER_EXEC`.
1818
- Const `Queue::EVENT_AFTER_ERROR` renamed to `Queue::EVENT_AFTER_EXEC_ERROR`.

docs/guide-ru/driver-file.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Файловый драйвер
22
================
33

4-
DB дравер для хранения очереди заданий использует файлы.
4+
Файловый драйвер для хранения очереди заданий использует определенную директорию.
55

66
Пример настройки:
77

docs/guide-ru/usage.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ class DownloadJob extends Object implements \zhuravljov\yii\queue\Job
3737
public $url;
3838
public $file;
3939

40-
/**
41-
* @param Queue $queue очередь, которая обрабатывает задание
42-
* @param string|null $jobId задания
43-
*/
44-
public function execute($queue, $jobId)
40+
public function execute($queue)
4541
{
4642
file_put_contents($this->file, file_get_contents($this->url));
4743
}
@@ -204,7 +200,7 @@ class SomeJob extends Object implements \zhuravljov\yii\queue\Job
204200
public $bookId;
205201
public $someUrl;
206202

207-
public function execute($queue, $jobId)
203+
public function execute($queue)
208204
{
209205
$user = User::findOne($this->userId);
210206
$book = Book::findOne($this->bookId);

docs/guide/usage.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ class DownloadJob extends Object implements \zhuravljov\yii\queue\Job
3737
public $url;
3838
public $file;
3939

40-
/**
41-
* @param Queue $queue which handled the job
42-
* @param string|null $jobId job message ID
43-
*/
44-
public function execute($queue, $jobId)
40+
public function execute($queue)
4541
{
4642
file_put_contents($this->file, file_get_contents($this->url));
4743
}
@@ -79,7 +75,7 @@ of a driver.
7975
Job status
8076
----------
8177

82-
The component has ability to track status jobs which is pushed into queue.
78+
The component has ability to track status jobs which was pushed into queue.
8379

8480
```php
8581
// Push a job into queue and get massage ID.
@@ -200,7 +196,7 @@ class SomeJob extends Object implements \zhuravljov\yii\queue\Job
200196
public $bookId;
201197
public $someUrl;
202198

203-
public function execute($queue, $jobId)
199+
public function execute($queue)
204200
{
205201
$user = User::findOne($this->userId);
206202
$book = Book::findOne($this->bookId);

src/Job.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
interface Job
1616
{
1717
/**
18-
* @param Queue $queue which was pushed the job
19-
* @param string|null $jobId job message ID
18+
* @param Queue $queue which pushed and is handling the job
2019
*/
21-
public function execute($queue, $jobId);
20+
public function execute($queue);
2221
}

src/Queue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected function handleMessage($id, $message)
119119
$error = null;
120120
$this->trigger(self::EVENT_BEFORE_EXEC, new JobEvent(['id' => $id, 'job' => $job]));
121121
try {
122-
$job->execute($this, $id);
122+
$job->execute($this);
123123
} catch (\Exception $error) {
124124
$this->trigger(self::EVENT_AFTER_EXEC_ERROR, new ErrorEvent(['id' => $id, 'job' => $job, 'error' => $error]));
125125
}

src/closure/Job.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Job extends Object implements BaseJob
2727
* Unserializes and executes a closure
2828
* @inheritdoc
2929
*/
30-
public function execute($queue, $jobId)
30+
public function execute($queue)
3131
{
3232
$serializer = new Serializer();
3333
$closure = $serializer->unserialize($this->serialized);

src/gii/default/job.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class <?= $jobClass ?> extends <?= $baseClass ?> <?= $implements ?>
3333
/**
3434
* @inheritdoc
3535
*/
36-
public function execute($queue, $jobId)
36+
public function execute($queue)
3737
{
3838
}
3939
}

tests/app/TestJob.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ class TestJob extends Object implements Job
2020
{
2121
public $uid;
2222

23-
public function execute($queue, $jobId)
23+
public function execute($queue)
2424
{
25-
file_put_contents($this->getFileName($jobId), '');
25+
file_put_contents($this->getFileName(), '');
2626
}
2727

28-
public function getFileName($jobId)
28+
public function getFileName()
2929
{
30-
return Yii::getAlias("@runtime/job-{$this->uid}-{$jobId}.lock");
30+
return Yii::getAlias("@runtime/job-{$this->uid}.lock");
3131
}
3232
}

0 commit comments

Comments
 (0)