introduce swoole coroutine to migration
This commit is contained in:
parent
383bb3d1c1
commit
968d733881
2 changed files with 30 additions and 19 deletions
|
@ -18,6 +18,7 @@ use Utopia\CLI\Console;
|
||||||
ini_set('memory_limit','512M');
|
ini_set('memory_limit','512M');
|
||||||
ini_set('display_errors', 1);
|
ini_set('display_errors', 1);
|
||||||
ini_set('display_startup_errors', 1);
|
ini_set('display_startup_errors', 1);
|
||||||
|
ini_set('default_socket_timeout', -1);
|
||||||
error_reporting(E_ALL);
|
error_reporting(E_ALL);
|
||||||
|
|
||||||
$http = new Server("0.0.0.0", App::getEnv('PORT', 80));
|
$http = new Server("0.0.0.0", App::getEnv('PORT', 80));
|
||||||
|
|
|
@ -4,9 +4,10 @@ namespace Appwrite\Migration;
|
||||||
|
|
||||||
use Appwrite\Database\Document;
|
use Appwrite\Database\Document;
|
||||||
use Appwrite\Database\Database;
|
use Appwrite\Database\Database;
|
||||||
|
use PDO;
|
||||||
|
use Swoole\Runtime;
|
||||||
use Utopia\CLI\Console;
|
use Utopia\CLI\Console;
|
||||||
use Utopia\Exception;
|
use Utopia\Exception;
|
||||||
use PDO;
|
|
||||||
|
|
||||||
abstract class Migration
|
abstract class Migration
|
||||||
{
|
{
|
||||||
|
@ -18,7 +19,7 @@ abstract class Migration
|
||||||
/**
|
/**
|
||||||
* @var int
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected int $limit = 30;
|
protected int $limit = 50;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Document
|
* @var Document
|
||||||
|
@ -63,7 +64,6 @@ abstract class Migration
|
||||||
*/
|
*/
|
||||||
public function forEachDocument(callable $callback): void
|
public function forEachDocument(callable $callback): void
|
||||||
{
|
{
|
||||||
|
|
||||||
$sum = $this->limit;
|
$sum = $this->limit;
|
||||||
$offset = 0;
|
$offset = 0;
|
||||||
|
|
||||||
|
@ -75,28 +75,38 @@ abstract class Migration
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$sum = \count($all);
|
$sum = \count($all);
|
||||||
|
Runtime::setHookFlags(SWOOLE_HOOK_ALL);
|
||||||
|
|
||||||
Console::log('Migrating: ' . $offset . ' / ' . $this->projectDB->getSum());
|
Console::log('Migrating: ' . $offset . ' / ' . $this->projectDB->getSum());
|
||||||
|
\Co\run(function () use ($all, $callback) {
|
||||||
|
Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
|
||||||
|
|
||||||
foreach ($all as $document) {
|
foreach ($all as $document) {
|
||||||
|
go(function () use ($document, $callback) {
|
||||||
|
|
||||||
$document = call_user_func($callback, $document);
|
$old = $document->getArrayCopy();
|
||||||
|
$new = call_user_func($callback, $document);
|
||||||
|
|
||||||
if (empty($document->getId())) {
|
if (empty($new->getId())) {
|
||||||
throw new Exception('Missing ID');
|
throw new Exception('Missing ID');
|
||||||
}
|
}
|
||||||
|
if (!array_diff($new->getArrayCopy(), $old)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$new = $this->projectDB->overwriteDocument($document->getArrayCopy());
|
$new = $this->projectDB->overwriteDocument($document->getArrayCopy());
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
Console::error('Failed to update document: ' . $th->getMessage());
|
Console::error('Failed to update document: ' . $th->getMessage());
|
||||||
continue;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
if ($new->getId() !== $document->getId()) {
|
if ($document && $new->getId() !== $document->getId()) {
|
||||||
throw new Exception('Duplication Error');
|
throw new Exception('Duplication Error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
$offset += $this->limit;
|
$offset += $this->limit;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue