This commit is contained in:
2026-05-11 15:23:34 +08:00
parent ed70a140a2
commit 547cbbb4a5
25 changed files with 721 additions and 332 deletions
+36 -2
View File
@@ -24,6 +24,17 @@ class AiMetadata
public function onWorkerStart(): void
{
Timer::add(10, fn (): int => $this->queue->releaseDueDelayed());
Timer::add(max(30, (int) config('queue.ai_metadata.dispatcher_interval_seconds', 60)), function (): void {
try {
if (!$this->enrichment->isEnabled()) {
return;
}
foreach ($this->archives->queuePendingAiMetadataArchives((int) config('queue.ai_metadata.dispatcher_batch_size', 20)) as $archiveUid) {
$this->queue->push($archiveUid);
}
} catch (Throwable) {
}
});
while (true) {
$this->queue->releaseDueDelayed();
@@ -47,10 +58,18 @@ class AiMetadata
}
if (!$this->archives->archiveNeedsMetadata($archive)) {
$this->archives->markAiMetadataStatus($archiveUid, 'completed', null, ['attempted' => false]);
$this->queue->clearRetry($archiveUid);
return;
}
if (!$this->enrichment->isEnabled()) {
$this->archives->markAiMetadataStatus($archiveUid, 'disabled', 'AI metadata enrichment is not enabled.');
$this->queue->clearRetry($archiveUid);
return;
}
$this->archives->markAiMetadataStatus($archiveUid, 'processing');
$payload = $archive;
$payload['content'] = $this->archives->findChunksText($archiveUid);
@@ -58,7 +77,15 @@ class AiMetadata
$aiMeta = $enriched['metadata']['ai_enrichment'] ?? [];
if (($aiMeta['attempted'] ?? false) !== true || ($aiMeta['error'] ?? null)) {
$this->queue->retryLater($archiveUid, $aiMeta['error'] ?? 'AI metadata enrichment did not complete.');
$retryable = ($aiMeta['enabled'] ?? true) === true && ($aiMeta['error'] ?? null) !== null;
$status = $retryable ? 'failed_retryable' : 'failed_terminal';
$error = $aiMeta['error'] ?? 'AI metadata enrichment did not complete.';
$this->archives->markAiMetadataStatus($archiveUid, $status, $error, $aiMeta);
if ($retryable) {
$this->queue->retryLater($archiveUid, $error);
} else {
$this->queue->clearRetry($archiveUid);
}
return;
}
@@ -70,9 +97,16 @@ class AiMetadata
}
$this->archives->updateMetadata($archiveUid, $fields, $aiMeta);
$this->archives->markAiMetadataStatus($archiveUid, 'completed', null, $aiMeta);
$this->queue->clearRetry($archiveUid);
} catch (Throwable $exception) {
$this->queue->retryLater($archiveUid, $exception->getMessage());
$status = $this->archives->isAiMetadataRetryable($exception) ? 'failed_retryable' : 'failed_terminal';
$this->archives->markAiMetadataStatus($archiveUid, $status, $exception->getMessage());
if ($status === 'failed_retryable') {
$this->queue->retryLater($archiveUid, $exception->getMessage());
} else {
$this->queue->clearRetry($archiveUid);
}
}
}
}