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

85 lines
2.6 KiB
PHP

<?php
namespace App\Temporal\SystemMonitor;
use App\Models\ImportJob;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Product;
use App\Models\User;
use App\Temporal\Shared\FaultSimulator;
use Illuminate\Support\Facades\DB;
use Temporal\Activity;
class SystemMonitorActivity implements SystemMonitorActivityInterface
{
public function runHealthCheck(array $simulationConfig = []): array
{
FaultSimulator::maybeApply($simulationConfig, 'runHealthCheck');
$checks = [];
$issues = 0;
// 1. DB connectivity
try {
DB::select('SELECT 1');
$checks['db_connected'] = true;
} catch (\Throwable) {
$checks['db_connected'] = false;
$issues += 3; // critical
}
Activity::heartbeat(['step' => 'db_connectivity']);
// 2. Table row counts
$checks['product_count'] = Product::count();
$checks['order_count'] = Order::count();
$checks['order_item_count'] = OrderItem::count();
$checks['user_count'] = User::count();
$checks['import_job_count'] = ImportJob::count();
Activity::heartbeat(['step' => 'row_counts']);
// 3. Stale data check — pending orders older than 24h
$checks['pending_stale_orders'] = Order::where('status', 'pending')
->where('created_at', '<', now()->subHours(24))
->count();
if ($checks['pending_stale_orders'] > 0) {
$issues++;
}
Activity::heartbeat(['step' => 'stale_data']);
// 4. Stock alerts — active products with zero stock
$checks['out_of_stock_products'] = Product::where('stock_quantity', 0)
->where('status', 'active')
->count();
if ($checks['out_of_stock_products'] > 5) {
$issues++;
}
Activity::heartbeat(['step' => 'stock_alerts']);
// 5. Stuck import jobs — started but not updated in 30 min
$checks['stuck_import_jobs'] = ImportJob::where('status', 'started')
->where('updated_at', '<', now()->subMinutes(30))
->count();
if ($checks['stuck_import_jobs'] > 0) {
$issues++;
}
Activity::heartbeat(['step' => 'stuck_jobs']);
// Compute health score (100 = perfect, deduct per issue)
$healthScore = max(0, 100 - ($issues * 10));
return [
'timestamp' => now()->toISOString(),
'checks' => $checks,
'healthScore' => $healthScore,
'issues' => $issues,
'attempt' => Activity::getInfo()->attempt,
];
}
}