|
| 1 | +use crate::errors::{JobError, JobResult}; |
| 2 | +use crate::jobs::job_executor::JobExecutor; |
| 3 | +use crate::jobs::job_instance::JobInstance; |
| 4 | +use crate::models::Job; |
| 5 | +use diesel::pg::PgConnection; |
| 6 | +use log::info; |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | +use std::process::Command; |
| 9 | + |
| 10 | +#[derive(Debug, Serialize, Deserialize)] |
| 11 | +pub struct PruneJob {} |
| 12 | + |
| 13 | +pub struct PruneJobInstance { |
| 14 | + job: Job, |
| 15 | +} |
| 16 | + |
| 17 | +impl PruneJobInstance { |
| 18 | + #[allow(clippy::new_ret_no_self)] |
| 19 | + pub fn new(job: Job) -> Box<dyn JobInstance> { |
| 20 | + match serde_json::from_str::<PruneJob>(&job.contents) { |
| 21 | + Ok(_) => Box::new(PruneJobInstance { job }), |
| 22 | + Err(e) => super::job_instance::InvalidJobInstance::new( |
| 23 | + job, |
| 24 | + JobError::new(&format!("Invalid prune job contents: {}", e)), |
| 25 | + ), |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +impl JobInstance for PruneJobInstance { |
| 31 | + fn get_job_id(&self) -> i32 { |
| 32 | + self.job.id |
| 33 | + } |
| 34 | + |
| 35 | + fn handle_job( |
| 36 | + &mut self, |
| 37 | + executor: &JobExecutor, |
| 38 | + conn: &mut PgConnection, |
| 39 | + ) -> JobResult<serde_json::Value> { |
| 40 | + info!("#{}: Handling Job Prune", &self.job.id); |
| 41 | + |
| 42 | + // Get repo config |
| 43 | + let config = &executor.config; |
| 44 | + let repo = self |
| 45 | + .job |
| 46 | + .repo |
| 47 | + .as_ref() |
| 48 | + .ok_or_else(|| JobError::new("No repo specified"))?; |
| 49 | + let repoconfig = config |
| 50 | + .get_repoconfig(repo) |
| 51 | + .map_err(|_e| JobError::new(&format!("Can't find repo {}", repo)))?; |
| 52 | + |
| 53 | + let repo_path = repoconfig.get_abs_repo_path(); |
| 54 | + |
| 55 | + // First do a dry run |
| 56 | + job_log_and_info!(self.job.id, conn, "Running prune dry-run"); |
| 57 | + let mut cmd = Command::new("flatpak"); |
| 58 | + cmd.arg("build-update-repo") |
| 59 | + .arg("-v") |
| 60 | + .arg("--no-update-summary") |
| 61 | + .arg("--no-update-appstream") |
| 62 | + .arg("--prune-dry-run") |
| 63 | + .arg("--prune-depth=3") |
| 64 | + .arg(&repo_path); |
| 65 | + |
| 66 | + super::utils::do_command(cmd)?; |
| 67 | + |
| 68 | + // Then do the actual prune |
| 69 | + job_log_and_info!(self.job.id, conn, "Running actual prune"); |
| 70 | + let mut cmd = Command::new("flatpak"); |
| 71 | + cmd.arg("build-update-repo") |
| 72 | + .arg("-v") |
| 73 | + .arg("--no-update-summary") |
| 74 | + .arg("--no-update-appstream") |
| 75 | + .arg("--prune") |
| 76 | + .arg("--prune-depth=3") |
| 77 | + .arg(&repo_path); |
| 78 | + |
| 79 | + super::utils::do_command(cmd)?; |
| 80 | + |
| 81 | + Ok(serde_json::json!({})) |
| 82 | + } |
| 83 | + |
| 84 | + // Higher order than Publish/UpdateRepo to prevent them from running while prune is in queue |
| 85 | + fn order(&self) -> i32 { |
| 86 | + 100 |
| 87 | + } |
| 88 | +} |
0 commit comments