1
0
Fork 0
mirror of synced 2024-09-30 09:18:14 +13:00

Merge pull request #789 from appwrite/bug-wrong-functions-count-and-deletion

Bug: Wrong functions count and deletion when deleting old containers
This commit is contained in:
Eldad A. Fux 2020-12-30 14:39:35 +02:00 committed by GitHub
commit 200658cadf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -283,7 +283,7 @@ class FunctionsV1
*/
public function execute(string $trigger, string $projectId, string $executionId, Database $database, Document $function, string $event = '', string $payload = ''): void
{
global $register, $list;
global $list;
$environments = Config::getParam('environments');
@ -489,29 +489,21 @@ class FunctionsV1
if(\count($list) > $max) {
Console::info('Starting containers cleanup');
$sorted = [];
foreach($list as $env) {
$sorted[] = [
'name' => $env['name'],
'created' => (int)($env['appwrite-created'] ?? 0)
];
}
\usort($sorted, function ($item1, $item2) {
return $item1['created'] <=> $item2['created'];
\usort($list, function ($item1, $item2) {
return (int)($item1['appwrite-created'] ?? 0) <=> (int)($item2['appwrite-created'] ?? 0);
});
while(\count($sorted) > $max) {
$first = \array_shift($sorted);
while(\count($list) > $max) {
$first = \array_shift($list);
$stdout = '';
$stderr = '';
if(Console::execute("docker stop {$first['name']}", '', $stdout, $stderr, 30) !== 0) {
if(Console::execute("docker rm -f {$first['name']}", '', $stdout, $stderr, 30) !== 0) {
Console::error('Failed to remove container: '.$stderr);
}
Console::info('Removed container: '.$first['name']);
else {
Console::info('Removed container: '.$first['name']);
}
}
}
}