server/worker_task.rs: implement UPID parser

This commit is contained in:
Dietmar Maurer 2019-04-04 13:49:35 +02:00
parent 49cf9f3d0b
commit 9a0d0ff774
1 changed files with 40 additions and 4 deletions

View File

@ -1,5 +1,6 @@
use failure::*; use failure::*;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use regex::Regex;
use chrono::Local; use chrono::Local;
use tokio::sync::oneshot; use tokio::sync::oneshot;
@ -7,10 +8,13 @@ use futures::*;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering, ATOMIC_USIZE_INIT}; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
use serde_json::{json, Value};
use crate::tools::{self, FileLogger}; use crate::tools::{self, FileLogger};
const PROXMOX_BACKUP_TASK_DIR: &str = "/var/log/proxmox-backup/tasks"; macro_rules! PROXMOX_BACKUP_TASK_DIR { () => ("/var/log/proxmox-backup/tasks") }
macro_rules! PROXMOX_BACKUP_TASK_LOCK_FN { () => (concat!(PROXMOX_BACKUP_TASK_DIR!(), "/.active.lock")) }
macro_rules! PROXMOX_BACKUP_ACTIVE_TASK_FN { () => (concat!(PROXMOX_BACKUP_TASK_DIR!(), "/active")) }
lazy_static! { lazy_static! {
static ref WORKER_TASK_LIST: Mutex<HashMap<usize, Arc<WorkerTask>>> = Mutex::new(HashMap::new()); static ref WORKER_TASK_LIST: Mutex<HashMap<usize, Arc<WorkerTask>>> = Mutex::new(HashMap::new());
@ -30,6 +34,38 @@ pub struct UPID {
pub node: String, pub node: String,
} }
impl std::str::FromStr for UPID {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
lazy_static! {
static ref REGEX: Regex = Regex::new(concat!(
r"^UPID:(?P<node>[a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?):(?P<pid>[0-9A-Fa-f]{8}):",
r"(?P<pstart>[0-9A-Fa-f]{8,9}):(?P<task_id>[0-9A-Fa-f]{8,16}):(?P<starttime>[0-9A-Fa-f]{8}):",
r"(?P<wtype>[^:\s]+):(?P<wid>[^:\s]*):(?P<username>[^:\s]+):$"
)).unwrap();
}
if let Some(cap) = REGEX.captures(s) {
return Ok(UPID {
pid: i32::from_str_radix(&cap["pid"], 16).unwrap(),
pstart: u64::from_str_radix(&cap["pstart"], 16).unwrap(),
starttime: i64::from_str_radix(&cap["starttime"], 16).unwrap(),
task_id: usize::from_str_radix(&cap["task_id"], 16).unwrap(),
worker_type: cap["wtype"].to_string(),
worker_id: if cap["wid"].is_empty() { None } else { Some(cap["wid"].to_string()) },
username: cap["username"].to_string(),
node: cap["node"].to_string(),
});
} else {
bail!("unable to parse UPID '{}'", s);
}
}
}
impl std::fmt::Display for UPID { impl std::fmt::Display for UPID {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
@ -39,8 +75,8 @@ impl std::fmt::Display for UPID {
// Note: pstart can be > 32bit if uptime > 497 days, so this can result in // Note: pstart can be > 32bit if uptime > 497 days, so this can result in
// more that 8 characters for pstart // more that 8 characters for pstart
write!(f, "UPID:{}:{:08X}:{:08X}:{:08X}:{}:{}:{}:", write!(f, "UPID:{}:{:08X}:{:08X}:{:08X}:{:08X}:{}:{}:{}:",
self.node, self.pid, self.pstart, self.starttime, self.worker_type, wid, self.username) self.node, self.pid, self.pstart, self.task_id, self.starttime, self.worker_type, wid, self.username)
} }
} }
@ -115,7 +151,7 @@ impl WorkerTask {
node: tools::nodename().to_owned(), node: tools::nodename().to_owned(),
}; };
let mut path = std::path::PathBuf::from(PROXMOX_BACKUP_TASK_DIR); let mut path = std::path::PathBuf::from(PROXMOX_BACKUP_TASK_DIR!());
path.push(format!("{:02X}", upid.pstart % 256)); path.push(format!("{:02X}", upid.pstart % 256));
let _ = std::fs::create_dir_all(&path); // ignore errors here let _ = std::fs::create_dir_all(&path); // ignore errors here