Skip to content

Commit

Permalink
rsc: Add job search and remove (#1562)
Browse files Browse the repository at this point in the history
  • Loading branch information
V-FEXrt authored May 10, 2024
1 parent 4c2cb51 commit 8bd3141
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
23 changes: 22 additions & 1 deletion rust/rsc/src/common/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rand_core::{OsRng, RngCore};
use sea_orm::ExecResult;
use sea_orm::{
prelude::Uuid, ActiveModelTrait, ActiveValue::*, ColumnTrait, ConnectionTrait, DbBackend,
DbErr, DeleteResult, EntityTrait, PaginatorTrait, QueryFilter, QuerySelect, Statement,
DbErr, DeleteResult, EntityTrait, PaginatorTrait, QueryFilter, QueryOrder, QuerySelect, Statement,
};
use tracing;

Expand Down Expand Up @@ -187,7 +187,20 @@ pub async fn read_dbonly_blob_store<T: ConnectionTrait>(
// ---------- Create ----------

// ---------- Read ----------
pub async fn read_job<T: ConnectionTrait>(db: &T, id: Uuid) -> Result<Option<job::Model>, DbErr> {
job::Entity::find_by_id(id).one(db).await
}

pub async fn search_jobs_by_label<T: ConnectionTrait>(
db: &T,
label: &String,
) -> Result<Vec<job::Model>, DbErr> {
job::Entity::find()
.filter(job::Column::Label.like(label))
.order_by_asc(job::Column::Label)
.all(db)
.await
}
// ---------- Update ----------

// ---------- Delete ----------
Expand Down Expand Up @@ -225,6 +238,14 @@ pub async fn delete_all_jobs<T: ConnectionTrait>(db: &T, chunk_size: u16) -> Res
}
}

pub async fn delete_job<T: ConnectionTrait>(
db: &T,
job: job::Model,
) -> Result<DeleteResult, DbErr> {
tracing::info!(%job.id, "Deleting job");
job::Entity::delete_by_id(job.id).exec(db).await
}

// --------------------------------------------------
// ---------- Visible File ----------
// --------------------------------------------------
Expand Down
68 changes: 68 additions & 0 deletions rust/rsc/src/rsc_tool/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,51 @@ async fn remove_local_blob_store(
Ok(())
}

async fn list_jobs_matching_label(db: &DatabaseConnection, label: &String) -> Result<(), DbErr> {
let mut jobs: Vec<_> = database::search_jobs_by_label(db, label)
.await?
.into_iter()
.map(|x| vec![format!("{}", x.id), textwrap::wrap(&x.label, 60).join("\n")])
.collect();

let headers = vec!["Id".into(), "Label".into()];
jobs.insert(0, headers);

table::print_table(jobs);

Ok(())
}

async fn remove_job(
id: &String,
db: &DatabaseConnection,
) -> Result<(), Box<dyn std::error::Error>> {
let uuid = Uuid::parse_str(id)?;
let Some(job) = database::read_job(db, uuid).await? else {
println!("{} is not a valid job", id);
std::process::exit(2);
};

// We only want to prompt the user if the user can type into the terminal
if std::io::stdin().is_terminal() {
let should_delete = Confirm::new("Are you sure you want to delete this job?")
.with_default(false)
.with_help_message(format!("id = {}, label = {:?}", job.id, job.label).as_str())
.prompt()?;

if !should_delete {
println!("Aborting removal");
return Ok(());
}
}

// Ok now that we're really sure we want to delete this key
database::delete_job(db, job).await?;

println!("Job {} was successfully removed", id);
Ok(())
}

async fn remove_all_jobs(db: &DatabaseConnection) -> Result<(), Box<dyn std::error::Error>> {
// Only let a human perform this action
if !std::io::stdin().is_terminal() {
Expand Down Expand Up @@ -263,6 +308,9 @@ enum DBModelList {

/// List all local blob stores
LocalBlobStore(NullOpts),

/// List jobs matching the label
JobsMatching(ListJobsMatchingOpts),
}

#[derive(Debug, Subcommand)]
Expand All @@ -282,6 +330,9 @@ enum DBModelRemove {
/// Remove a local blob store
LocalBlobStore(RemoveByIdOpts),

/// Remove a job
Job(RemoveByIdOpts),

/// DANGER Remove all cached jobs
DangerJobsAll(NullOpts),
}
Expand Down Expand Up @@ -317,6 +368,17 @@ struct AddApiKeyOpts {
desc: String,
}

#[derive(Debug, Parser)]
struct ListJobsMatchingOpts {
#[arg(
required = true,
help = "The label to search for. '%' is match any characters",
value_name = "LABEL",
long
)]
label: String,
}

#[derive(Debug, Parser)]
struct AddLocalBlobStoreOpts {
#[arg(
Expand Down Expand Up @@ -383,6 +445,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
DBCommand::List(ListOpts {
db_command: DBModelList::LocalBlobStore(_),
}) => list_local_blob_stores(&db).await?,
DBCommand::List(ListOpts {
db_command: DBModelList::JobsMatching(ListJobsMatchingOpts { label }),
}) => list_jobs_matching_label(&db, &label).await?,

// Add Commands
DBCommand::Add(AddOpts {
Expand All @@ -399,6 +464,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
DBCommand::Remove(RemoveOpts {
db_command: DBModelRemove::LocalBlobStore(args),
}) => remove_local_blob_store(&args.id, &db).await?,
DBCommand::Remove(RemoveOpts {
db_command: DBModelRemove::Job(args),
}) => remove_job(&args.id, &db).await?,
DBCommand::Remove(RemoveOpts {
db_command: DBModelRemove::DangerJobsAll(_),
}) => remove_all_jobs(&db).await?,
Expand Down

0 comments on commit 8bd3141

Please sign in to comment.