Skip to content

Commit

Permalink
Add cron job creation functionality
Browse files Browse the repository at this point in the history
Implemented a new method in `AcquiaCommand.php` to create cron jobs and added a corresponding command in `CronCommand.php`. The new command allows users to create scheduled cron jobs by interacting with the application and environment selection helpers.
  • Loading branch information
emerham committed Sep 26, 2024
1 parent 2ac6be9 commit 12b3ad8
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/Commands/AcquiaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,25 @@ protected function getCrons(string $envUuid) {
return $cronList;
}

/**
* Create a cron job.
*
* @param string $envUuid
* The environment UUID.
* @param string $command
* The cron command to execute.
* @param string $frequency
* The frequency of the cron job.
* @param string $label
* The label for the cron job.
*
* @return \AcquiaCloudApi\Response\OperationResponse
*/
protected function createCron(string $envUuid, string $command, string $frequency, string $label) {
$cron = new Crons($this->client);
return $cron->create($envUuid, $command, $frequency, $label);
}

/**
* Create a new Database.
*
Expand Down
97 changes: 92 additions & 5 deletions src/Commands/CronCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace OsuWams\Commands;


use Consolidation\OutputFormatters\FormatterManager;
use Consolidation\OutputFormatters\Options\FormatterOptions;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Exception;
use Symfony\Component\Console\Question\ChoiceQuestion;

class CronCommand extends AcquiaCommand {

Expand All @@ -18,8 +17,8 @@ public function __construct() {
/**
* Get a list of Scheduled Jobs.
*
* Optional arguments: app,env. If app and/or env are not provided, a helper will ask you to select
* from a generated list.
* Optional arguments: app,env. If app and/or env are not provided, a helper
* will ask you to select from a generated list.
*
* @command cron:list
*
Expand All @@ -45,7 +44,8 @@ public function listCrons(array $options = [
'env' => NULL,
'format' => 'table',
'fields' => 'label,command',
]) {
]
) {
if (is_null($options['app'])) {
$this->say('Getting Applications...');
$appHelper = new ChoiceQuestion('Select which Acquia Cloud Application you want to operate on', $this->getApplicationsId());
Expand Down Expand Up @@ -96,4 +96,91 @@ public function listCrons(array $options = [
$formatterManager->write($this->output, $opts->getFormat(), new RowsOfFields($cronList), $opts);
}

/**
* Create a new scheduled cron job.
*
* Optional arguments: app, env, domain.
* If app, env, and/or domain are not provided, a helper will ask you to
* select from a generated list.
*
* @command cron:create
*
* @param array $options
* @option $app The Acquia Cloud Application name: prod:shortname
* @option $env The Environment short name: dev|prod|test
* @option $domain The Domain name associated with the application
*
* @return void
*/
public function cronCreate(array $options = [
'app' => NULL,
'env' => NULL,
'domain' => NULL,
]
) {
if (is_null($options['app'])) {
$this->say('Getting Applications...');
$appHelper = new ChoiceQuestion('Select which Acquia Cloud Application you want to operate on', $this->getApplicationsId());
$appName = $this->doAsk($appHelper);
}
else {
$appName = $options['app'];
}
// Attempt to get the UUID of this application.
try {
$appUuId = $this->getUuidFromName($appName);
}
catch (Exception $e) {
$this->say('Incorrect Application ID.');
exit($e->getCode());
}
if (is_null($options['env'])) {
// Get a list of environments for this App UUID.
$this->writeln('Getting Environment ID\'s...');
$envList = $this->getEnvironments($appUuId);
// Get the Env for the scheduled jobs.
$envHelper = new ChoiceQuestion('Which Environment do you want to create the cron for...', $envList);
$environment = $this->doAsk($envHelper);
}
else {
$environment = $options['env'];
}
try {
$envUuId = $this->getEnvUuIdFromApp($appUuId, $environment);
}
catch (Exception $e) {
$this->say('Incorrect Environment and Application id.');
exit($e->getCode());
}
if (is_null($options['domain'])) {
$this->writeln("Getting list of Domains...");
$domainList = $this->getDomains($envUuId);
$domainHelper = new ChoiceQuestion("Which domain do you want to make a Cron job for?", $domainList);
$domain = $this->doAsk($domainHelper);
}
else {
$domain = $options['domain'];
}
$site = explode(":", $appName)[1];
$randomHour = random_int(0, 23);
$randomMinute = random_int(0, 59);
$defaultLabel = "Cron - $domain";
$defaultCommand = "/usr/local/bin/cron-wrapper.sh $site.$environment https://$domain";
$cronLabel = $this->askDefault("Enter the cron label:", $defaultLabel);
$cronCommand = $this->askDefault("Enter the cron command.", $defaultCommand);
$cronMinute = $this->askDefault("Enter the cron minute: (0-59)", $randomMinute);
$cronHour = $this->askDefault("Enter the cron hour: (0-23)", $randomHour);
$cronDayMonth = $this->askDefault('Enter the cron day of month: (1-31)', '*');
$cronMonth = $this->askDefault('Enter the cron month: (1-12)', '*');
$cronDayWeek = $this->askDefault('Enter the cron day of week: (0-6)', '*');
$frequency = "$cronMinute $cronHour $cronDayMonth $cronMonth $cronDayWeek";
$makeItSo = $this->confirm("Create the cron job with the label '$cronLabel'.\nCommand of '$cronCommand'.\nWith the frequency of '$frequency'?");
if ($makeItSo) {
$this->createCron($envUuId, $cronCommand, $frequency, $cronLabel);
}
else {
$this->say("Aborting.");
}
}

}

0 comments on commit 12b3ad8

Please sign in to comment.