Skip to content

Commit 687b31a

Browse files
committed
batch switch backend
1 parent e4149f1 commit 687b31a

1 file changed

Lines changed: 34 additions & 7 deletions

File tree

src/Keboola/Console/Command/OrganizationStorageBackend.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@ protected function configure()
3939
InputArgument::OPTIONAL,
4040
'Keboola Connection Hostname Suffix',
4141
'keboola.com'
42-
)
43-
;
42+
);
4443
}
4544

46-
4745
protected function execute(InputInterface $input, OutputInterface $output)
4846
{
4947
$storageBackendId = $input->getArgument(self::ARGUMENT_STORAGE_BACKEND_ID);
@@ -73,16 +71,45 @@ protected function execute(InputInterface $input, OutputInterface $output)
7371
$storageBackendId
7472
)
7573
);
76-
$params = [$project['id'], $storageBackendId];
74+
$params = [(string) $project['id'], (string) $storageBackendId];
7775
if ($force) {
7876
$params[] = '--force';
79-
$manageClient->runCommand([
77+
$result = $manageClient->runCommand([
8078
'command' => 'manage:switch-storage-backend',
81-
'parameters' => $params
79+
'parameters' => $params,
8280
]);
83-
$manageClient->assignProjectStorageBackend($project['id'], $storageBackendId);
81+
$output->writeln(sprintf('INFO: Storage backend switch for project "%s" in progress using command "%s".', $project['id'], $result['commandExecutionId']));
82+
83+
if ($this->waitForProjectToMigrate($manageClient, $project['id'], (int) $storageBackendId, $output)) {
84+
$output->writeln(sprintf('SUSCCESS: Storage backend switch for project "%s" in progress using command "%s".', $project['id'], $result['commandExecutionId']));
85+
} else {
86+
$output->writeln(sprintf('ERROR: Storage backend switch for project "%s" to backend "%s" timed out.', $project['id'], $storageBackendId));
87+
}
8488
}
8589
}
8690
$output->writeln('All done.');
8791
}
92+
93+
private function waitForProjectToMigrate(
94+
Client $manageClient,
95+
int $projectId,
96+
int $requestedProjectId,
97+
OutputInterface $output,
98+
): bool {
99+
$timeout = 300;
100+
$start = time();
101+
while (time() - $start < $timeout) {
102+
$projectDetail = $manageClient->getProject($projectId);
103+
$currentBackednId = (int) $projectDetail['backends']['snowflake']['id'];
104+
105+
if ($currentBackednId === $requestedProjectId) {
106+
$output->writeln(sprintf(' - Project "%s" migrated successfully to backend "%s".', $projectId, $requestedProjectId));
107+
return true;
108+
}
109+
$output->writeln(sprintf(' - Project "%s" did not migrate to backend "%s" yet...waiting', $projectId, $requestedProjectId));
110+
sleep(2);
111+
}
112+
113+
return false;
114+
}
88115
}

0 commit comments

Comments
 (0)