You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: monitor.rs
    path of file: ./repos/crates.io/src/bin
    
    the code of the file until where you have to start completion: //! Checks for any invariants we expect to be true, and pages whoever is on call
//! if they are not.
//!
//! Usage:
//!     cargo run --bin monitor

use anyhow::Result;
use crates_io::worker::jobs;
use crates_io::{admin::on_call, db, schema::*};
use crates_io_env_vars::{var, var_parsed};
use crates_io_worker::BackgroundJob;
use diesel::prelude::*;

fn main() -> Result<()> {
    let conn = &mut db::oneoff_connection()?;

    check_failing_background_jobs(conn)?;
    check_stalled_update_downloads(conn)?;
    check_spam_attack(conn)?;
    Ok(())
}

/// Check for old background jobs that are not currently running.
///
/// This check includes `skip_locked` in the query and will o
    
fn check_failing_background_jobs(conn: &mut PgConnection) -> Result<()> {
    use diesel::dsl::*;
    use diesel::sql_types::Integer;

    const EVENT_KEY: &str = "background_jobs";

    println!("Checking for failed background jobs");

    // Max job execution time in minutes
    let max_job_time = var_parsed("MAX_JOB_TIME")?.unwrap_or(15);

    let stalled_jobs: Vec<i32> = background_jobs::table
        .select(1.into_sql::<Integer>())
        .filter(background_jobs::created_at.lt(now - max_job_time.minutes()))
        .filter(background_jobs::priority.ge(0))
        .for_update()
        .skip_locked()
        .load(conn)?;

    let stalled_job_count = stalled_jobs.len();

    let event = if stalled_job_count > 0 {
        on_call::Event::Trigger {
            incident_key: Some(EVENT_KEY.into()),
            description: format!(
                "{stalled_job_count} jobs have been in the queue for more than {max_job_time} minutes"
            ),
        }
    } else {
        on_call::Event::Resolve {
            incident_key: EVENT_KEY.into(),
            description: Some("No stalled background jobs".into()),
        }
    };

    log_and_trigger_event(event)?;
    Ok(())
}