Files
temporalio-test/app/Temporal/EloquentQuery/EloquentQueryWorkflow.php
2026-05-09 01:18:51 +02:00

101 lines
3.4 KiB
PHP

<?php
namespace App\Temporal\EloquentQuery;
use Carbon\CarbonInterval;
use Temporal\Activity\ActivityOptions;
use Temporal\Common\RetryOptions;
use Temporal\Workflow;
class EloquentQueryWorkflow implements EloquentQueryWorkflowInterface
{
private string $status = 'pending';
private string $currentStep = '';
private int $completedSteps = 0;
private array $stepResults = [];
private int $totalQueriesRun = 0;
private int $totalRowsAffected = 0;
private float $totalExecutionTimeMs = 0;
/** @var EloquentQueryActivityInterface */
private $activityStub;
private const STEPS = [
'runInventoryAnalytics' => 'Inventory Analytics',
'runOrderDeepLoad' => 'Order Deep Load',
'runProductScoring' => 'Product Scoring',
'runStockAudit' => 'Stock Audit',
'runPriceRecalculation' => 'Price Recalculation',
'runSummaryReport' => 'Summary Report',
];
public function __construct()
{
$this->activityStub = Workflow::newActivityStub(
EloquentQueryActivityInterface::class,
ActivityOptions::new()
->withStartToCloseTimeout(CarbonInterval::minutes(2))
->withHeartbeatTimeout(CarbonInterval::seconds(30))
->withRetryOptions(
RetryOptions::new()->withMaximumAttempts(3)
)
);
}
public function runPipeline(array $simulationConfig = []): \Generator
{
$this->status = 'running';
try {
foreach (self::STEPS as $method => $label) {
$this->currentStep = $label;
$result = yield $this->activityStub->$method($simulationConfig);
$this->stepResults[$method] = [
'label' => $label,
'queriesRun' => $result['queriesRun'] ?? 0,
'rowsAffected' => $result['rowsAffected'] ?? 0,
'executionTimeMs' => $result['executionTimeMs'] ?? 0,
'data' => $result['data'] ?? [],
'attempt' => $result['attempt'] ?? 1,
];
$this->totalQueriesRun += $result['queriesRun'] ?? 0;
$this->totalRowsAffected += $result['rowsAffected'] ?? 0;
$this->totalExecutionTimeMs += $result['executionTimeMs'] ?? 0;
$this->completedSteps++;
}
} catch (\Throwable $e) {
$this->status = 'failed';
throw $e;
}
$this->status = 'completed';
$this->currentStep = '';
return [
'status' => 'completed',
'completedSteps' => $this->completedSteps,
'totalQueriesRun' => $this->totalQueriesRun,
'totalRowsAffected' => $this->totalRowsAffected,
'totalExecutionTimeMs' => $this->totalExecutionTimeMs,
'stepResults' => $this->stepResults,
];
}
public function getProgress(): array
{
return [
'status' => $this->status,
'currentStep' => $this->currentStep,
'completedSteps' => $this->completedSteps,
'totalSteps' => count(self::STEPS),
'stepResults' => $this->stepResults,
'totalQueriesRun' => $this->totalQueriesRun,
'totalRowsAffected' => $this->totalRowsAffected,
'totalExecutionTimeMs' => $this->totalExecutionTimeMs,
];
}
}