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

0% Statements 0/65
100% Branches 0/0
0% Functions 0/1
0% Lines 0/65

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                                                                                                                                   
/**
 * SLO Snapshot Cron Job
 *
 * POST /api/cron/slo-snapshot
 *
 * Called hourly to capture current SLO state and store in database.
 * This enables historical trending and breach detection.
 *
 * Security: Verify CRON_SECRET header for Vercel cron jobs
 */

import { NextRequest, NextResponse } from 'next/server';
import { createSLOSnapshots, cleanupOldSnapshots } from '@/lib/observability/slo-history';
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 snapshot cron job', { category: 'CRON' });

    // Create snapshots for all SLOs
    const snapshotsCreated = await createSLOSnapshots();

    // Cleanup old snapshots (30+ days)
    const { deleted } = await cleanupOldSnapshots();

    logger.info('SLO snapshot cron job completed', {
      category: 'CRON',
      snapshotsCreated,
      oldSnapshotsDeleted: deleted,
    });

    return NextResponse.json({
      success: true,
      snapshotsCreated,
      oldSnapshotsDeleted: deleted,
      timestamp: new Date().toISOString(),
    });
  } catch (error) {
    logger.error('SLO snapshot 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);
}