Skip to content

Commit

Permalink
Merge branch 'feature/psr-2'
Browse files Browse the repository at this point in the history
Conflicts:
	src/Console/AWS/ConfigureLeaderCommand.php
  • Loading branch information
Jono20201 committed Sep 28, 2016
2 parents 13fe144 + c5b47a0 commit 3d3f52b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 32 deletions.
56 changes: 31 additions & 25 deletions src/Console/AWS/ConfigureLeaderCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace FoxxMD\LaravelElasticBeanstalkCron\Console\AWS;

use Aws\Ec2\Ec2Client;
Expand All @@ -23,18 +22,20 @@ class ConfigureLeaderCommand extends Command
*/
protected $description = 'Configure leader ec2 instance';

/**
* @var Ec2Client
*/
protected $ecClient;

public function __construct()
{
parent::__construct();

$region = getenv('AWS_REGION') ?: 'us-east-1';
$client = new Ec2Client([
'region' => getenv('AWS_REGION') ?: 'us-east-1',
'version' => 'latest',
]);

$client = new Ec2Client([
'region' => $region,
'version' => 'latest'
]);
$this->ecClient = $client;
}

Expand All @@ -48,17 +49,19 @@ public function handle()
$ch = curl_init('http://169.254.169.254/latest/meta-data/instance-id'); //magic ip from AWS
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($result = curl_exec($ch)) {

if ($result = curl_exec($ch)) {
$this->info('Instance ID: ' . $result);

// Get this instance metadata so we can find the environment it's running in
$tags = $info = $this->ecClient->describeInstances(['Filters' => [
['Name' => 'instance-id',
'Values' => [$result]
]
]
])->get('Reservations')[0]['Instances'][0]['Tags'];
$tags = $info = $this->ecClient->describeInstances([
'Filters' => [
[
'Name' => 'instance-id',
'Values' => [$result],
],
],
])->get('Reservations')[0]['Instances'][0]['Tags'];

// Get environment name
$environmentName = F\first($tags, function ($tagArray) {
Expand All @@ -69,12 +72,14 @@ public function handle()
$this->info('Getting Instances with Environment: ' . $environmentName);

// Get instances that have this environment tagged
$info = $this->ecClient->describeInstances(['Filters' => [
['Name' => 'tag-value',
'Values' => [$environmentName]
]
]
]);
$info = $this->ecClient->describeInstances([
'Filters' => [
[
'Name' => 'tag-value',
'Values' => [$environmentName],
],
],
]);
$instances = F\map($info->get('Reservations'), function ($i) {
return current($i['Instances']);
});
Expand All @@ -88,7 +93,8 @@ public function handle()
$leader = false;

if (!empty($candidateInstances)) { //there are instances running
if (count($candidateInstances) > 1) { // if there is more than one we sort by launch time and get the oldest
if (count($candidateInstances) > 1) {
// if there is more than one we sort by launch time and get the oldest
$this->info('More than one instance running, finding the oldest...');
$oldestInstance = F\sort($candidateInstances, function ($left, $right) {
return $left['LaunchTime'] > $right['LaunchTime'];
Expand All @@ -97,7 +103,8 @@ public function handle()
$this->info('Only one instance running...');
$oldestInstance = $candidateInstances[0];
}
if ($oldestInstance['InstanceId'] == $result) { // if this instance is the oldest instance it's the leader
if ($oldestInstance['InstanceId'] == $result) {
// if this instance is the oldest instance it's the leader
$leader = true;
}
} else {
Expand All @@ -106,7 +113,8 @@ public function handle()
}


// No leader is running so we'll setup this one as the leader and create a cron entry to run the scheduler
// No leader is running so we'll setup this one as the leader
// and create a cron entry to run the scheduler
if ($leader) {
$this->info('We are the Leader! Initiating Cron Setup');
$this->call('system:start:cron');
Expand All @@ -122,10 +130,8 @@ public function handle()
// Probably be run from your local machine
$this->error('Did not detect an ec2 environment. Exiting.');
}

} else {
$this->info('USE_CRON env var not set. Exiting.');
}

}
}
}
29 changes: 24 additions & 5 deletions src/Console/System/SetupLeaderSelectionCRONCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace FoxxMD\LaravelElasticBeanstalkCron\Console\System;

use Illuminate\Console\Command;
use Illuminate\Contracts\Config\Repository as ConfigRepository;

class SetupLeaderSelectionCRONCommand extends Command
{
Expand All @@ -19,11 +20,23 @@ class SetupLeaderSelectionCRONCommand extends Command
*
* @var string
*/
protected $description = 'Configure this system\'s CRON to periodically (every 5 minutes) run leader selection.';
protected $description = "Configure this system's CRON to periodically (every 5 minutes) run leader selection.";

public function __construct()
/**
* @var ConfigRepository
*/
private $config;

/**
* SetupLeaderSelectionCRONCommand constructor.
*
* @param ConfigRepository $config
*/
public function __construct(ConfigRepository $config)
{
parent::__construct();

$this->config = $config;
}

public function handle()
Expand All @@ -42,13 +55,19 @@ public function handle()
if (!is_null($output) && strpos($output, 'aws:configure:leader') !== false) {
$this->info('Already found Leader Selection entry! Not adding.');
} else {
$interval = $this->config->get('elasticbeanstalkcron.interval', 5);

// using opt..envvars makes sure that environmental variables are loaded before we run artisan
// http://georgebohnisch.com/laravel-task-scheduling-working-aws-elastic-beanstalk-cron/
$interval = config('elasticbeanstalkcron.interval', 5);
file_put_contents('/tmp/crontab.txt', $output . "*/$interval * * * * . /opt/elasticbeanstalk/support/envvars && /usr/bin/php /var/app/current/artisan aws:configure:leader >> /dev/null 2>&1" . PHP_EOL);
file_put_contents(
'/tmp/crontab.txt',
$output . "*/$interval * * * * . /opt/elasticbeanstalk/support/envvars &&" .
" /usr/bin/php /var/app/current/artisan aws:configure:leader >> /dev/null 2>&1" . PHP_EOL
);

echo exec('crontab /tmp/crontab.txt');
}

$this->info('Leader Selection CRON Done!');
}
}
}
4 changes: 2 additions & 2 deletions src/elasticbeanstalkcron.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

return array(
return [
/*
* The interval, in minutes, that a Leader Selection check should be run by the CRON
*/
'interval' => 5
);
];

0 comments on commit 3d3f52b

Please sign in to comment.