Files
michaelschiemer/src/Framework/Queue/ValueObjects/JobMetrics.php
Michael Schiemer fc3d7e6357 feat(Production): Complete production deployment infrastructure
- Add comprehensive health check system with multiple endpoints
- Add Prometheus metrics endpoint
- Add production logging configurations (5 strategies)
- Add complete deployment documentation suite:
  * QUICKSTART.md - 30-minute deployment guide
  * DEPLOYMENT_CHECKLIST.md - Printable verification checklist
  * DEPLOYMENT_WORKFLOW.md - Complete deployment lifecycle
  * PRODUCTION_DEPLOYMENT.md - Comprehensive technical reference
  * production-logging.md - Logging configuration guide
  * ANSIBLE_DEPLOYMENT.md - Infrastructure as Code automation
  * README.md - Navigation hub
  * DEPLOYMENT_SUMMARY.md - Executive summary
- Add deployment scripts and automation
- Add DEPLOYMENT_PLAN.md - Concrete plan for immediate deployment
- Update README with production-ready features

All production infrastructure is now complete and ready for deployment.
2025-10-25 19:18:37 +02:00

210 lines
6.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Framework\Queue\ValueObjects;
use App\Framework\Core\ValueObjects\Percentage;
final readonly class JobMetrics
{
public function __construct(
public string $jobId,
public string $queueName,
public string $status,
public int $attempts,
public int $maxAttempts,
public float $executionTimeMs,
public int $memoryUsageBytes,
public ?string $errorMessage,
public string $createdAt,
public ?string $startedAt,
public ?string $completedAt,
public ?string $failedAt,
public array $metadata = []
) {
}
public static function create(
string $jobId,
string $queueName,
string $status = 'pending',
int $attempts = 0,
int $maxAttempts = 3
): self {
$now = date('Y-m-d H:i:s');
return new self(
jobId: $jobId,
queueName: $queueName,
status: $status,
attempts: $attempts,
maxAttempts: $maxAttempts,
executionTimeMs: 0.0,
memoryUsageBytes: 0,
errorMessage: null,
createdAt: $now,
startedAt: null,
completedAt: null,
failedAt: null,
metadata: []
);
}
public function withStarted(float $executionStartTime, int $memoryUsage): self
{
return new self(
jobId: $this->jobId,
queueName: $this->queueName,
status: 'running',
attempts: $this->attempts + 1,
maxAttempts: $this->maxAttempts,
executionTimeMs: $executionStartTime,
memoryUsageBytes: $memoryUsage,
errorMessage: $this->errorMessage,
createdAt: $this->createdAt,
startedAt: date('Y-m-d H:i:s'),
completedAt: $this->completedAt,
failedAt: $this->failedAt,
metadata: $this->metadata
);
}
public function withCompleted(float $totalExecutionTime, int $peakMemoryUsage): self
{
return new self(
jobId: $this->jobId,
queueName: $this->queueName,
status: 'completed',
attempts: $this->attempts,
maxAttempts: $this->maxAttempts,
executionTimeMs: $totalExecutionTime,
memoryUsageBytes: $peakMemoryUsage,
errorMessage: $this->errorMessage,
createdAt: $this->createdAt,
startedAt: $this->startedAt,
completedAt: date('Y-m-d H:i:s'),
failedAt: $this->failedAt,
metadata: $this->metadata
);
}
public function withFailed(string $errorMessage, float $executionTime, int $memoryUsage): self
{
return new self(
jobId: $this->jobId,
queueName: $this->queueName,
status: 'failed',
attempts: $this->attempts,
maxAttempts: $this->maxAttempts,
executionTimeMs: $executionTime,
memoryUsageBytes: $memoryUsage,
errorMessage: $errorMessage,
createdAt: $this->createdAt,
startedAt: $this->startedAt,
completedAt: $this->completedAt,
failedAt: date('Y-m-d H:i:s'),
metadata: $this->metadata
);
}
public function withMetadata(array $metadata): self
{
return new self(
jobId: $this->jobId,
queueName: $this->queueName,
status: $this->status,
attempts: $this->attempts,
maxAttempts: $this->maxAttempts,
executionTimeMs: $this->executionTimeMs,
memoryUsageBytes: $this->memoryUsageBytes,
errorMessage: $this->errorMessage,
createdAt: $this->createdAt,
startedAt: $this->startedAt,
completedAt: $this->completedAt,
failedAt: $this->failedAt,
metadata: array_merge($this->metadata, $metadata)
);
}
public function getSuccessRate(): Percentage
{
if ($this->attempts === 0) {
return Percentage::from(100.0);
}
$successfulAttempts = $this->status === 'completed' ? 1 : 0;
return Percentage::from(($successfulAttempts / max(1, $this->attempts)) * 100);
}
public function getExecutionTimeSeconds(): float
{
return $this->executionTimeMs / 1000.0;
}
public function getMemoryUsageMB(): float
{
return $this->memoryUsageBytes / (1024 * 1024);
}
public function getDuration(): ?int
{
if (! $this->startedAt) {
return null;
}
$endTime = $this->completedAt ?? $this->failedAt ?? date('Y-m-d H:i:s');
return strtotime($endTime) - strtotime($this->startedAt);
}
public function isCompleted(): bool
{
return $this->status === 'completed';
}
public function isFailed(): bool
{
return $this->status === 'failed';
}
public function isRunning(): bool
{
return $this->status === 'running';
}
public function isPending(): bool
{
return $this->status === 'pending';
}
public function hasMaxAttempts(): bool
{
return $this->attempts >= $this->maxAttempts;
}
public function toArray(): array
{
return [
'job_id' => $this->jobId,
'queue_name' => $this->queueName,
'status' => $this->status,
'attempts' => $this->attempts,
'max_attempts' => $this->maxAttempts,
'execution_time_ms' => $this->executionTimeMs,
'execution_time_seconds' => $this->getExecutionTimeSeconds(),
'memory_usage_bytes' => $this->memoryUsageBytes,
'memory_usage_mb' => $this->getMemoryUsageMB(),
'success_rate' => $this->getSuccessRate()->getValue(),
'duration_seconds' => $this->getDuration(),
'error_message' => $this->errorMessage,
'created_at' => $this->createdAt,
'started_at' => $this->startedAt,
'completed_at' => $this->completedAt,
'failed_at' => $this->failedAt,
'metadata' => $this->metadata,
];
}
}