1 | <?php |
||
2 | /* (c) Tomas Majer <[email protected]> |
||
3 | * |
||
4 | * For the full copyright and license information, please view the LICENSE |
||
5 | * file that was distributed with this source code. |
||
6 | */ |
||
7 | |||
8 | namespace Deployer; |
||
9 | |||
10 | use Deployer\Task\Context; |
||
11 | use PhpAmqpLib\Connection\AMQPConnection; |
||
12 | use PhpAmqpLib\Message\AMQPMessage; |
||
13 | |||
14 | |||
15 | desc('Notifying RabbitMQ channel about deployment'); |
||
16 | task('deploy:rabbit', function () { |
||
17 | |||
18 | if (!class_exists('PhpAmqpLib\Connection\AMQPConnection')) { |
||
19 | throw new \RuntimeException("<comment>Please install php package</comment> <info>videlalvaro/php-amqplib</info> <comment>to use rabbitmq</comment>"); |
||
20 | } |
||
21 | |||
22 | $config = get('rabbit', []); |
||
23 | |||
24 | if (!isset($config['message'])) { |
||
25 | $releasePath = get('release_path'); |
||
26 | $host = Context::get()->getHost(); |
||
27 | |||
28 | $stage = get('stage', false); |
||
29 | $stageInfo = ($stage) ? sprintf(' on *%s*', $stage) : ''; |
||
30 | |||
31 | $message = "Deployment to '%s'%s was successful\n(%s)"; |
||
32 | $config['message'] = sprintf( |
||
33 | $message, |
||
34 | $host, |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
35 | $stageInfo, |
||
36 | $releasePath |
||
37 | ); |
||
38 | } |
||
39 | |||
40 | $defaultConfig = array( |
||
41 | 'host' => 'localhost', |
||
42 | 'port' => 5672, |
||
43 | 'username' => 'guest', |
||
44 | 'password' => 'guest', |
||
45 | 'vhost' => '/', |
||
46 | ); |
||
47 | |||
48 | $config = array_merge($defaultConfig, $config); |
||
49 | |||
50 | if (!is_array($config) || |
||
0 ignored issues
–
show
|
|||
51 | !isset($config['channel']) || |
||
52 | !isset($config['host']) || |
||
53 | !isset($config['port']) || |
||
54 | !isset($config['username']) || |
||
55 | !isset($config['password']) || |
||
56 | !isset($config['vhost']) ) |
||
57 | { |
||
58 | throw new \RuntimeException("<comment>Please configure rabbit config:</comment> <info>set('rabbit', array('channel' => 'channel', 'host' => 'host', 'port' => 'port', 'username' => 'username', 'password' => 'password'));</info>"); |
||
59 | } |
||
60 | |||
61 | $connection = new AMQPConnection($config['host'], $config['port'], $config['username'], $config['password'], $config['vhost']); |
||
62 | $channel = $connection->channel(); |
||
63 | |||
64 | $msg = new AMQPMessage($config['message']); |
||
65 | $channel->basic_publish($msg, $config['channel'], $config['channel']); |
||
66 | |||
67 | $channel->close(); |
||
68 | $connection->close(); |
||
69 | |||
70 | }); |
||
71 |