-
Notifications
You must be signed in to change notification settings - Fork 18
/
AsyncComponent.php
76 lines (66 loc) · 1.57 KB
/
AsyncComponent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace bazilio\async;
use bazilio\async\models\AsyncTask;
use bazilio\transports\Transport;
use yii\base\Component;
use Yii;
/**
* Class AsyncComponent
* @package bazilio\async
*/
class AsyncComponent extends Component
{
public $transportClass = 'bazilio\async\transports\AsyncAmqpTransport';
public $transportConfig = [];
/** @var Transport */
protected $transport;
public function init()
{
$this->transport = new $this->transportClass($this->transportConfig);
}
/**
* @param AsyncTask $task
* @return bool|string
* @throws Exception
*/
public function sendTask(AsyncTask $task)
{
if ($task->validate()) {
return $this->transport->send(serialize($task), $task::$queueName);
} else {
throw new Exception(var_export($task->errors, true));
}
}
/**
* @param $queueName
* @param bool $wait Wait for task
* @return AsyncTask|bool
*/
public function receiveTask($queueName, $wait = false)
{
return $this->transport->receive($queueName, $wait);
}
/**
* @param AsyncTask $task
* @return bool
*/
public function acknowledgeTask(AsyncTask $task)
{
return $this->transport->acknowledge($task);
}
/**
* @param string $queueName
* @return bool
*/
public function purge($queueName)
{
return $this->transport->purge($queueName);
}
/**
* @return Transport
*/
public function getTransport()
{
return $this->transport;
}
}