forked from deployphp/recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rabbit.php
60 lines (46 loc) · 1.94 KB
/
rabbit.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
<?php
/* (c) Tomas Majer <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer;
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;
desc('Notifying RabbitMQ channel about deployment');
task('deploy:rabbit', function () {
if (!class_exists('PhpAmqpLib\Connection\AMQPConnection')) {
throw new \RuntimeException("<comment>Please install php package</comment> <info>videlalvaro/php-amqplib</info> <comment>to use rabbitmq</comment>");
}
$config = get('rabbit', []);
if (!isset($config['message'])) {
$releasePath = get('release_path');
$host = config()->getHost();
$prod = get('env', 'production');
$config['message'] = "Deployment to '{$host}' on *{$prod}* was successful\n($releasePath)";
}
$defaultConfig = array(
'host' => 'localhost',
'port' => 5672,
'username' => 'guest',
'password' => 'guest',
'vhost' => '/',
);
$config = array_merge($defaultConfig, $config);
if (!is_array($config) ||
!isset($config['channel']) ||
!isset($config['host']) ||
!isset($config['port']) ||
!isset($config['username']) ||
!isset($config['password']) ||
!isset($config['vhost']) )
{
throw new \RuntimeException("<comment>Please configure rabbit config:</comment> <info>set('rabbit', array('channel' => 'channel', 'host' => 'host', 'port' => 'port', 'username' => 'username', 'password' => 'password'));</info>");
}
$connection = new AMQPConnection($config['host'], $config['port'], $config['username'], $config['password'], $config['vhost']);
$channel = $connection->channel();
$msg = new AMQPMessage($config['message']);
$channel->basic_publish($msg, $config['channel'], $config['channel']);
$channel->close();
$connection->close();
});