Skip to content

Commit f12a4df

Browse files
committed
usage examples, fixes #5
1 parent 68d5cf6 commit f12a4df

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

README.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,67 @@ Install: `php composer.phar require bazilio/yii2-async:dev-master`
5656

5757

5858
#####Usage:
59-
For code examples look into tests:
59+
60+
#### Create and send:
61+
Test class example:
62+
```php
63+
class DownloadTask extends AsyncTask
64+
{
65+
public $url;
66+
public $file;
67+
public static $queueName = 'downloads';
68+
69+
public function execute()
70+
{
71+
return file_put_contents($this->file, file_get_contents($this->url));
72+
}
73+
}
74+
75+
// create task
76+
$task = new DownloadTask(['url' => 'http://localhost/', 'file' => '/tmp/localhost.html']);
77+
\Yii::$app->async->sendTask($task);
78+
```
79+
80+
Or call external method:
81+
```php
82+
$task = new AsyncExecuteTask([
83+
'class' => 'common\components\MyDownloaderComponent',
84+
'method' => 'download',
85+
'arguments' => ['url' => 'http://localhost/', 'file' => '/tmp/localhost.html']
86+
]);
87+
88+
89+
$task::$queueName = 'downloads';
90+
91+
if (YII_ENV !== 'prod') {
92+
$task->execute();
93+
} else {
94+
Yii::$app->async->sendTask($task);
95+
}
96+
```
97+
98+
#### Execute:
99+
100+
Bash way:
101+
102+
```bash
103+
# Process and exit on finish
104+
./yii async-worker/execute downloads
105+
# Process and wait for new tasks (only redis)
106+
./yii async-worker/daemon downloads
107+
```
108+
109+
Code way:
110+
111+
```php
112+
while ($task = \Yii::$app->async->receiveTask('downloads')) {
113+
if ($task->execute()) {
114+
\Yii::$app->async->acknowledgeTask($task);
115+
}
116+
}
117+
```
118+
119+
For more code examples look into tests:
60120
- [BaseTestClass](tests/unit/BaseTestClass.php)
61121

62122

0 commit comments

Comments
 (0)