All files / src/app/api/cron/slo-check route.ts

89.55% Statements 60/67
83.33% Branches 5/6
100% Functions 2/2
89.55% Lines 60/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 681x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 6x 6x 6x 6x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8x               8x 1x 1x 1x 1x 1x  
/**
 * SLO Check Cron Job
 *
 * POST /api/cron/slo-check
 *
 * Called every 5 minutes to check SLOs and trigger alerts for breaches.
 * Integrates with the SLO alerting service for email notifications.
 *
 * Security: Verify CRON_SECRET header for Vercel cron jobs
 */
 
import { NextRequest, NextResponse } from 'next/server';
import { checkAndAlertSLOs } from '@/lib/observability/slo-alerts';
import { logger } from '@/lib/logging';
 
export const dynamic = 'force-dynamic';
export const maxDuration = 60; // Allow up to 60 seconds for cron job
 
export async function POST(request: NextRequest) {
  try {
    // Verify cron secret for security
    const authHeader = request.headers.get('authorization');
    const cronSecret = process.env.CRON_SECRET;
 
    if (cronSecret && authHeader !== `Bearer ${cronSecret}`) {
      return NextResponse.json(
        { error: 'Unauthorized' },
        { status: 401 }
      );
    }
 
    logger.info('Starting SLO check cron job', { category: 'CRON' });
 
    // Check SLOs and send alerts
    const result = await checkAndAlertSLOs({
      enabled: true,
      alertOnWarning: false, // Only alert on critical status
      alertOnCritical: true,
    });
 
    logger.info('SLO check cron job completed', {
      category: 'CRON',
      slosChecked: result.slosChecked,
      alertsTriggered: result.alertsTriggered,
      alertsSkipped: result.alertsSkipped,
      alertsResolved: result.alertsResolved,
    });
 
    return NextResponse.json({
      success: true,
      ...result,
      timestamp: new Date().toISOString(),
    });
  } catch (error) {
    logger.error('SLO check cron job failed', error as Error, { category: 'CRON' });

    return NextResponse.json(
      { error: 'Cron job failed', message: (error as Error).message },
      { status: 500 }
    );
  }
}
 
// Also support GET for manual triggers
export async function GET(request: NextRequest) {
  return POST(request);
}