暂存
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace app\process;
|
||||
|
||||
use app\service\Embedding\ChunkEmbeddingRepository;
|
||||
use app\service\Search\ChunkSearchIndexRepository;
|
||||
use app\service\Task\ProofDbTaskQueue;
|
||||
use Throwable;
|
||||
|
||||
class ProofDbTaskDispatcher
|
||||
{
|
||||
private ProofDbTaskQueue $queue;
|
||||
private ChunkEmbeddingRepository $embeddings;
|
||||
private ChunkSearchIndexRepository $searchIndex;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->queue = new ProofDbTaskQueue();
|
||||
$this->embeddings = new ChunkEmbeddingRepository();
|
||||
$this->searchIndex = new ChunkSearchIndexRepository();
|
||||
}
|
||||
|
||||
public function onWorkerStart(): void
|
||||
{
|
||||
while (true) {
|
||||
$this->dispatchOnce();
|
||||
sleep($this->queue->dispatcherIntervalSeconds());
|
||||
}
|
||||
}
|
||||
|
||||
private function dispatchOnce(): void
|
||||
{
|
||||
try {
|
||||
foreach ($this->embeddings->queuePendingArchiveTasks($this->queue->dispatcherBatchSize()) as $archiveUid) {
|
||||
$this->queue->push([
|
||||
'task_type' => 'embedding',
|
||||
'target_type' => 'archive',
|
||||
'target_uid' => $archiveUid,
|
||||
'attempt' => 1,
|
||||
'queued_at' => date(DATE_ATOM),
|
||||
]);
|
||||
}
|
||||
|
||||
foreach ($this->searchIndex->queuePendingArchiveTasks($this->queue->dispatcherBatchSize()) as $archiveUid) {
|
||||
$this->queue->push([
|
||||
'task_type' => 'search_index',
|
||||
'target_type' => 'archive',
|
||||
'target_uid' => $archiveUid,
|
||||
'attempt' => 1,
|
||||
'queued_at' => date(DATE_ATOM),
|
||||
]);
|
||||
}
|
||||
} catch (Throwable $exception) {
|
||||
sleep($this->queue->idleSleepSeconds());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace app\process;
|
||||
|
||||
use app\service\Embedding\ChunkEmbeddingHandler;
|
||||
use app\service\Search\ChunkSearchIndexHandler;
|
||||
use app\service\Task\ProofDbTaskQueue;
|
||||
use Throwable;
|
||||
use Workerman\Timer;
|
||||
|
||||
class ProofDbTaskWorker
|
||||
{
|
||||
private ProofDbTaskQueue $queue;
|
||||
private ChunkEmbeddingHandler $embeddings;
|
||||
private ChunkSearchIndexHandler $searchIndex;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->queue = new ProofDbTaskQueue();
|
||||
$this->embeddings = new ChunkEmbeddingHandler();
|
||||
$this->searchIndex = new ChunkSearchIndexHandler();
|
||||
}
|
||||
|
||||
public function onWorkerStart(): void
|
||||
{
|
||||
Timer::add(10, fn (): int => $this->queue->releaseDueDelayed());
|
||||
|
||||
while (true) {
|
||||
$this->queue->releaseDueDelayed();
|
||||
$task = $this->queue->pop($this->queue->blockTimeout());
|
||||
if ($task === null) {
|
||||
sleep($this->queue->idleSleepSeconds());
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->handle($task);
|
||||
}
|
||||
}
|
||||
|
||||
private function handle(array $task): void
|
||||
{
|
||||
try {
|
||||
if (($task['task_type'] ?? null) === 'embedding') {
|
||||
$this->embeddings->handle($task);
|
||||
}
|
||||
|
||||
if (($task['task_type'] ?? null) === 'search_index') {
|
||||
$this->searchIndex->handle($task);
|
||||
}
|
||||
|
||||
$this->queue->clearRetry($task);
|
||||
} catch (Throwable $exception) {
|
||||
$this->queue->retryLater($task, $exception->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user