mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
Refactor embedding timeout and clean database logic
- Updated embedding agent timeout to be configurable via environment variable. - Removed commented code in XList for clarity. - Refactored database cleaning logic into separate methods for better readability and maintainability.
This commit is contained in:
@@ -1615,6 +1615,6 @@ Http::setResource('executionsRetentionCount', function (Document $project, array
|
||||
Http::setResource('embeddingAgent', function ($register) {
|
||||
$adapter = new Ollama();
|
||||
$adapter->setEndpoint(System::getEnv('_APP_EMBEDDING_ENDPOINT', 'http://ollama:11434/api/embed'));
|
||||
$adapter->setTimeout(90000);
|
||||
$adapter->setTimeout((int) System::getEnv('_APP_EMBEDDING_TIMEOUT', '30000'));
|
||||
return new Agent($adapter);
|
||||
}, ['register']);
|
||||
|
||||
@@ -91,7 +91,6 @@ class XList extends Action
|
||||
$cursor->setValue($cursorDocument);
|
||||
}
|
||||
|
||||
// use base action to get all types, contexts, etc!
|
||||
$queries[] = Query::equal('type', [$this->getDatabaseType()]);
|
||||
|
||||
try {
|
||||
|
||||
@@ -74,7 +74,7 @@ class Create extends CreateDocumentAction
|
||||
]
|
||||
)
|
||||
])
|
||||
->param('texts', [], fn (array $plan) => new ArrayList(new Text(0), $plan['databasesBatchSize'] ?? APP_LIMIT_DATABASE_BATCH), 'Array of text to generate embeddings.', false, ['plan'])
|
||||
->param('texts', [], fn (array $plan) => new ArrayList(new Text(0), $plan['databasesMaxEmbeddingTexts'] ?? APP_LIMIT_DATABASE_BATCH), 'Array of text to generate embeddings.', false, ['plan'])
|
||||
->param('model', Ollama::MODEL_EMBEDDING_GEMMA, new WhiteList(Ollama::MODELS), 'The embedding model to use for generating vector embeddings.', true)
|
||||
->inject('response')
|
||||
->inject('project')
|
||||
@@ -88,7 +88,7 @@ class Create extends CreateDocumentAction
|
||||
public function action(array $texts, string $model, UtopiaResponse $response, Document $project, Agent $embeddingAgent, StatsUsage $queueForStatsUsage, Log $log, ?Logger $logger): void
|
||||
{
|
||||
$results = [];
|
||||
$embeddingAgent->getAdapter()->setModel($model)->setTimeout(60000);
|
||||
$embeddingAgent->getAdapter()->setModel($model);
|
||||
$dimension = $embeddingAgent->getAdapter()->getEmbeddingDimension();
|
||||
|
||||
$totalDuration = 0;
|
||||
|
||||
@@ -218,6 +218,50 @@ class Deletes extends Action
|
||||
}
|
||||
}
|
||||
|
||||
private function cleanDatabase(
|
||||
Document $databaseDoc,
|
||||
callable $executionActionPerDatabase,
|
||||
bool $projectTables,
|
||||
array $projectCollectionIds
|
||||
): void {
|
||||
$executionActionPerDatabase(
|
||||
$databaseDoc,
|
||||
fn (Database $dbForDatabases) => $this->cleanDatabaseCollections(
|
||||
$dbForDatabases,
|
||||
$projectTables,
|
||||
$projectCollectionIds
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private function cleanDatabaseCollections(
|
||||
Database $dbForDatabases,
|
||||
bool $projectTables,
|
||||
array $projectCollectionIds
|
||||
): void {
|
||||
$dbForDatabases->foreach(
|
||||
Database::METADATA,
|
||||
function (Document $collection) use ($dbForDatabases, $projectTables, $projectCollectionIds) {
|
||||
$collectionId = $collection->getId();
|
||||
|
||||
try {
|
||||
if ($projectTables || !\in_array($collectionId, $projectCollectionIds, true)) {
|
||||
$dbForDatabases->deleteCollection($collectionId);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->deleteByGroup(
|
||||
$collectionId,
|
||||
[Query::orderAsc()],
|
||||
database: $dbForDatabases
|
||||
);
|
||||
} catch (Throwable $e) {
|
||||
Console::error('Error deleting ' . $collectionId . ' ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Database $dbForPlatform
|
||||
* @param callable $getProjectDB
|
||||
@@ -590,32 +634,12 @@ class Deletes extends Action
|
||||
};
|
||||
|
||||
batch(array_map(
|
||||
fn ($databaseDoc) =>
|
||||
fn () => $executionActionPerDatabase(
|
||||
$databaseDoc,
|
||||
function (Database $dbForDatabases) use ($projectTables, $projectCollectionIds) {
|
||||
$dbForDatabases->foreach(
|
||||
Database::METADATA,
|
||||
function (Document $collection) use ($dbForDatabases, $projectTables, $projectCollectionIds) {
|
||||
try {
|
||||
if ($projectTables || !\in_array($collection->getId(), $projectCollectionIds, true)) {
|
||||
$dbForDatabases->deleteCollection($collection->getId());
|
||||
} else {
|
||||
$this->deleteByGroup(
|
||||
$collection->getId(),
|
||||
[Query::orderAsc()],
|
||||
database: $dbForDatabases
|
||||
);
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
Console::error(
|
||||
'Error deleting ' . $collection->getId() . ' ' . $e->getMessage()
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
),
|
||||
fn ($databaseDoc) => fn () => $this->cleanDatabase(
|
||||
$databaseDoc,
|
||||
$executionActionPerDatabase,
|
||||
$projectTables,
|
||||
$projectCollectionIds
|
||||
),
|
||||
$databasesToClean
|
||||
));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user