Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update documentation #63

Merged
merged 13 commits into from
Sep 29, 2020
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`.
1. Payload handler. It is a callable called by a `WorkerInterface` which handles every queue message.
Fantom409 marked this conversation as resolved.
Show resolved Hide resolved

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 it's handler may look like this:
Fantom409 marked this conversation as resolved.
Show resolved Hide resolved

```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