Skip to content

Commit

Permalink
allow serverless scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Dec 21, 2023
1 parent 11def5c commit 583c24b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
43 changes: 38 additions & 5 deletions src/Commands/CacheScaleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Laravel\VaporCli\Helpers;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class CacheScaleCommand extends Command
{
Expand All @@ -17,7 +18,9 @@ protected function configure()
$this
->setName('cache:scale')
->addArgument('cache', InputArgument::REQUIRED, 'The cache name / ID')
->addArgument('scale', InputArgument::REQUIRED, 'The number of nodes that should be in the cache cluster')
->addArgument('scale', InputArgument::OPTIONAL, 'The number of nodes that should be in the cache cluster')
->addOption('memory', null, InputOption::VALUE_OPTIONAL, 'The maximum amount of memory that can be used by the serverless cache')
->addOption('cpu', null, InputOption::VALUE_OPTIONAL, 'The maximum amount of ECPUs that can be used by the serverless cache')
->setDescription('Modify the number of nodes in a cache cluster');
}

Expand All @@ -40,13 +43,43 @@ public function handle()

$cache = $this->vapor->cache($cacheId);

$this->vapor->scaleCache(
$cacheId,
$this->argument('scale')
);
if ($cache['type'] === 'redis7.x-serverless') {
$this->scaleServerlessCache($cacheId);
} else {
$this->scaleCacheCluster($cacheId);
}

Helpers::info('Cache modification initiated successfully.');
Helpers::line();
Helpers::line('Caches may take several minutes to finish scaling.');
}

/**
* Scale a serverless cache.
*/
protected function scaleServerlessCache(int $cacheId): void
{
if (is_null($this->option('memory')) || is_null($this->option('cpu'))) {
Helpers::abort('You must specify both the memory and CPU limits. To remove the either limit, set it to 0.');
}

$this->vapor->scaleCache(
$cacheId,
null,
$this->option('memory') === '0' ? null : $this->option('memory'),
$this->option('cpu') === '0' ? null : $this->option('cpu')
);
}

/**
* Scale a cache cluster.
*/
protected function scaleCacheCluster(int $cacheId): void
{
if (! $scale = $this->argument('scale')) {
Helpers::abort('You must specify the number of nodes to scale the cache to.');
}

$this->vapor->scaleCache($cacheId, $scale);
}
}
4 changes: 3 additions & 1 deletion src/ConsoleVaporClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,10 +598,12 @@ public function createCache($networkId, $name, $type, $instanceClass)
* @param int $scale
* @return void
*/
public function scaleCache($cacheId, $scale)
public function scaleCache($cacheId, $scale = null, $memoryLimit = null, $cpuLimit = null)
{
$this->requestWithErrorHandling('put', '/api/caches/'.$cacheId.'/size', [
'scale' => $scale,
'memory_limit' => $memoryLimit,
'cpu_limit' => $cpuLimit,
]);
}

Expand Down

0 comments on commit 583c24b

Please sign in to comment.