|
| 1 | +use std::{process::Command, sync::Arc}; |
| 2 | + |
| 3 | +use anyhow::Result; |
| 4 | +use bytes::Bytes; |
| 5 | +use chrono::{DateTime, Local}; |
| 6 | +use log::{debug, warn}; |
| 7 | + |
| 8 | +use crate::{ |
| 9 | + backend::{FileType, ReadBackend, WriteBackend}, |
| 10 | + id::Id, |
| 11 | + CommandInput, |
| 12 | +}; |
| 13 | + |
| 14 | +/// A backend which warms up files by simply accessing them. |
| 15 | +#[derive(Clone, Debug)] |
| 16 | +pub struct LockBackend { |
| 17 | + /// The backend to use. |
| 18 | + be: Arc<dyn WriteBackend>, |
| 19 | + /// The command to be called to lock files in the backend |
| 20 | + command: CommandInput, |
| 21 | +} |
| 22 | + |
| 23 | +impl LockBackend { |
| 24 | + /// Creates a new `WarmUpAccessBackend`. |
| 25 | + /// |
| 26 | + /// # Arguments |
| 27 | + /// |
| 28 | + /// * `be` - The backend to use. |
| 29 | + pub fn new_lock(be: Arc<dyn WriteBackend>, command: CommandInput) -> Arc<dyn WriteBackend> { |
| 30 | + Arc::new(Self { be, command }) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl ReadBackend for LockBackend { |
| 35 | + fn location(&self) -> String { |
| 36 | + self.be.location() |
| 37 | + } |
| 38 | + |
| 39 | + fn list_with_size(&self, tpe: FileType) -> Result<Vec<(Id, u32)>> { |
| 40 | + self.be.list_with_size(tpe) |
| 41 | + } |
| 42 | + |
| 43 | + fn read_full(&self, tpe: FileType, id: &Id) -> Result<Bytes> { |
| 44 | + self.be.read_full(tpe, id) |
| 45 | + } |
| 46 | + |
| 47 | + fn read_partial( |
| 48 | + &self, |
| 49 | + tpe: FileType, |
| 50 | + id: &Id, |
| 51 | + cacheable: bool, |
| 52 | + offset: u32, |
| 53 | + length: u32, |
| 54 | + ) -> Result<Bytes> { |
| 55 | + self.be.read_partial(tpe, id, cacheable, offset, length) |
| 56 | + } |
| 57 | + |
| 58 | + fn list(&self, tpe: FileType) -> Result<Vec<Id>> { |
| 59 | + self.be.list(tpe) |
| 60 | + } |
| 61 | + |
| 62 | + fn needs_warm_up(&self) -> bool { |
| 63 | + self.be.needs_warm_up() |
| 64 | + } |
| 65 | + |
| 66 | + fn warm_up(&self, tpe: FileType, id: &Id) -> Result<()> { |
| 67 | + self.be.warm_up(tpe, id) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +fn path(tpe: FileType, id: &Id) -> String { |
| 72 | + let hex_id = id.to_hex(); |
| 73 | + match tpe { |
| 74 | + FileType::Config => "config".into(), |
| 75 | + FileType::Pack => format!("data/{}/{}", &hex_id[0..2], &*hex_id), |
| 76 | + _ => format!("{}/{}", tpe.dirname(), &*hex_id), |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl WriteBackend for LockBackend { |
| 81 | + fn create(&self) -> Result<()> { |
| 82 | + self.be.create() |
| 83 | + } |
| 84 | + |
| 85 | + fn write_bytes(&self, tpe: FileType, id: &Id, cacheable: bool, buf: Bytes) -> Result<()> { |
| 86 | + self.be.write_bytes(tpe, id, cacheable, buf) |
| 87 | + } |
| 88 | + |
| 89 | + fn remove(&self, tpe: FileType, id: &Id, cacheable: bool) -> Result<()> { |
| 90 | + self.be.remove(tpe, id, cacheable) |
| 91 | + } |
| 92 | + |
| 93 | + fn can_lock(&self) -> bool { |
| 94 | + true |
| 95 | + } |
| 96 | + |
| 97 | + fn lock(&self, tpe: FileType, id: &Id, until: Option<DateTime<Local>>) -> Result<()> { |
| 98 | + let until = until.map_or_else(|| String::new(), |u| format!("{}", u.to_rfc3339())); |
| 99 | + let path = path(tpe, id); |
| 100 | + let args = self.command.args().iter().map(|c| { |
| 101 | + c.replace("%id", &id.to_hex()) |
| 102 | + .replace("%type", tpe.dirname()) |
| 103 | + .replace("%path", &path) |
| 104 | + .replace("%until", &until) |
| 105 | + }); |
| 106 | + debug!("calling {:?}...", self.command); |
| 107 | + let status = Command::new(self.command.command()).args(args).status()?; |
| 108 | + if !status.success() { |
| 109 | + warn!("lock command was not successful for {tpe:?}, id: {id}. {status}"); |
| 110 | + } |
| 111 | + Ok(()) |
| 112 | + } |
| 113 | +} |
0 commit comments