From c5b47a01ed775ff82f5b784d44113091b97c8e2d Mon Sep 17 00:00:00 2001 From: Jonathan Martin Date: Wed, 28 Sep 2016 11:52:31 +0100 Subject: [PATCH] Code cleanup to pass PSR-2 and Code Sniffer checks. Also injecting the config class instead of using the config() helper. --- src/Console/AWS/ConfigureLeaderCommand.php | 57 +++++++++++-------- .../SetupLeaderSelectionCRONCommand.php | 29 ++++++++-- src/elasticbeanstalkcron.php | 4 +- 3 files changed, 59 insertions(+), 31 deletions(-) diff --git a/src/Console/AWS/ConfigureLeaderCommand.php b/src/Console/AWS/ConfigureLeaderCommand.php index ac8e0cc..3f251b3 100644 --- a/src/Console/AWS/ConfigureLeaderCommand.php +++ b/src/Console/AWS/ConfigureLeaderCommand.php @@ -1,6 +1,5 @@ 'us-east-1', - 'version' => 'latest' - ]); + + $client = new Ec2Client([ + 'region' => 'us-east-1', + 'version' => 'latest', + ]); + $this->ecClient = $client; } @@ -45,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) { @@ -66,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']); }); @@ -85,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']; @@ -94,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 { @@ -103,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'); @@ -119,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.'); } - } -} \ No newline at end of file +} diff --git a/src/Console/System/SetupLeaderSelectionCRONCommand.php b/src/Console/System/SetupLeaderSelectionCRONCommand.php index 2413a0b..79f973a 100644 --- a/src/Console/System/SetupLeaderSelectionCRONCommand.php +++ b/src/Console/System/SetupLeaderSelectionCRONCommand.php @@ -3,6 +3,7 @@ namespace FoxxMD\LaravelElasticBeanstalkCron\Console\System; use Illuminate\Console\Command; +use Illuminate\Contracts\Config\Repository as ConfigRepository; class SetupLeaderSelectionCRONCommand extends Command { @@ -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() @@ -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!'); } -} \ No newline at end of file +} diff --git a/src/elasticbeanstalkcron.php b/src/elasticbeanstalkcron.php index fde1c8e..5a80f85 100644 --- a/src/elasticbeanstalkcron.php +++ b/src/elasticbeanstalkcron.php @@ -1,8 +1,8 @@ 5 -); \ No newline at end of file +]; \ No newline at end of file