Skip to content

Commit

Permalink
Update documentation (yiisoft#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fantom409 authored Sep 29, 2020
1 parent 28b9e3c commit 45a2937
Show file tree
Hide file tree
Showing 56 changed files with 192 additions and 4,020 deletions.
68 changes: 57 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,42 @@ or add
"yiisoft/yii-queue": "~3.0"
```

to the require section of your `composer.json` file.
to the `require` section of your `composer.json` file.

Basic Usage
-----------

Each task which is sent to queue should be defined as a separate class.
For example, if you need to download and save a file the class may look like the following:
Each queue task consists of two parts:
1. Data payload. It is a class implementing `PayloadInterface`.
2. Payload handler. It is a callable called by a `WorkerInterface` which handles every queue message.

For example, if you need to download and save a file, the payload may look like the following:

```php
class DownloadJob implements Yiisoft\Yii\Queue\Payload\PayloadInterface
{
public $url;
public $file;
public const NAME = 'file-download';

public string $url;
public string $fileName;

public function __construct(string $url, string $file)
public function __construct(string $url, string $fileName)
{
$this->url = $url;
$this->file = $file;
$this->fileName = $fileName;
}

public function getName(): string
{
return 'earlyDefinedQueueHandlerName';
return self::NAME;
}

public function getData()
public function getData(): array
{
file_put_contents($this->file, file_get_contents($this->url));
return [
'destinationFile' => $this->fileName,
'url' => $this->url
];
}

public function getMeta(): array
Expand All @@ -70,13 +78,51 @@ class DownloadJob implements Yiisoft\Yii\Queue\Payload\PayloadInterface
}
```

And its handler may look like the following:

```php
class FileDownloader
{
private string $absolutePath;

public function __construct(string $absolutePath)
{
$this->absolutePath = $absolutePath;
}

public function handle(\Yiisoft\Yii\Queue\Message\MessageInterface $downloadMessage): void
{
$fileName = $downloadMessage->getPayloadData()['destinationFile'];
$path = "$this->absolutePath/$fileName";
file_put_contents($path, file_get_contents($downloadMessage->getPayloadData()['url']));
}
}
```

The last thing we should do is to create configuration for the `Yiisoft\Yii\Queue\Worker\Worker`:
```php
$handlers = [DownloadJob::NAME => [new FileDownloader('/path/to/save/files'), 'handle']];
$worker = new \Yiisoft\Yii\Queue\Worker\Worker(
$handlers, // Here it is
$dispatcher,
$logger,
$injector,
$container
);
```

Here's how to send a task into the queue:

```php
$queue->push(
new DownloadJob('http://example.com/image.jpg', '/tmp/image.jpg')
new DownloadJob('http://example.com/image.jpg', 'new-image-name.jpg')
);
```





To push a job into the queue that should run after 5 minutes:

```php
Expand Down
30 changes: 0 additions & 30 deletions docs/guide-ja/README.md

This file was deleted.

22 changes: 0 additions & 22 deletions docs/guide-ja/debug.md

This file was deleted.

62 changes: 0 additions & 62 deletions docs/guide-ja/driver-amqp-interop.md

This file was deleted.

61 changes: 0 additions & 61 deletions docs/guide-ja/driver-beanstalk.md

This file was deleted.

Loading

0 comments on commit 45a2937

Please sign in to comment.