@@ -56,7 +56,67 @@ Install: `php composer.phar require bazilio/yii2-async:dev-master`
56
56
57
57
58
58
#####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:
60
120
- [ BaseTestClass] ( tests/unit/BaseTestClass.php )
61
121
62
122
0 commit comments