Skip to content

Commit

Permalink
Add a safeguard to prevent multiple ticket creation
Browse files Browse the repository at this point in the history
  • Loading branch information
cholcombe973 committed Sep 20, 2017
1 parent ad282a0 commit 3acf901
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
15 changes: 15 additions & 0 deletions src/in_progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn create_repair_database(db_path: &Path) -> Result<Connection> {
Ok(conn)
}

/// Create a new repair ticket
pub fn record_new_repair_ticket(
conn: &Connection,
ticket_id: &str,
Expand All @@ -53,6 +54,20 @@ pub fn record_new_repair_ticket(
Ok(())
}

/// Check and return if a disk is in the database and awaiting repairs
pub fn is_disk_in_progress(conn: &Connection, dev_path: &Path) -> Result<bool> {
debug!(
"Searching for repair ticket for disk: {}",
dev_path.display()
);
let mut stmt = conn.prepare(
"SELECT id, ticket_id, time_created, disk_path FROM repairs where disk_path=?",
)?;
let in_progress = stmt.exists(&[&dev_path.to_string_lossy().into_owned()])?;
Ok(in_progress)
}

/// Gather all the outstanding repair tickets
pub fn get_outstanding_repair_tickets(conn: &Connection) -> Result<Vec<DiskRepairTicket>> {
let mut tickets: Vec<DiskRepairTicket> = Vec::new();
let mut stmt = conn.prepare(
Expand Down
28 changes: 19 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod test_disk;

use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::path::{Path, PathBuf};

use create_support_ticket::{create_support_ticket, ticket_resolved};
use clap::{Arg, App};
Expand Down Expand Up @@ -60,6 +60,7 @@ fn load_config(config_dir: &str) -> Result<ConfigSettings, String> {

fn check_for_failed_disks(config_dir: &str, simulate: bool) -> Result<(), String> {
let config = load_config(config_dir)?;
let config_location = Path::new(&config.db_location);
//Host information to use in ticket creation
let host_info = Host::new().map_err(|e| e.to_string())?;
let mut description = format!(
Expand All @@ -84,17 +85,26 @@ Details: Disk {} as failed. Please replace if necessary",
for result in test_disk::check_all_disks().map_err(|e| e.to_string())? {
match result {
Ok(status) => {
//
info!("Disk status: {:?}", status);
let mut dev_path = PathBuf::from("/dev");
dev_path.push(status.device.name);

if status.corrupted == true && status.repaired == false {
description.push_str(&format!("Disk path: /dev/{}", status.device.name));
let _ = backend
.remove_disk(
&Path::new(&format!("/dev/{}", status.device.name)),
simulate,
)
.map_err(|e| e.to_string())?;
description.push_str(&format!("Disk path: {}", dev_path.display()));
let _ = backend.remove_disk(&dev_path, simulate).map_err(
|e| e.to_string(),
)?;
if !simulate {
// TODO: Double check that this disk isn't already in progress
info!("Connecting to database to check if disk is in progress");
let conn = in_progress::create_repair_database(&config_location)
.map_err(|e| e.to_string())?;
let tickets = in_progress::is_disk_in_progress(&conn, &dev_path).map_err(
|e| {
e.to_string()
},
)?;

let _ = create_support_ticket(
&config.jira_host,
&config.jira_user,
Expand Down

0 comments on commit 3acf901

Please sign in to comment.