Skip to content

Commit 7a167a2

Browse files
authored
Clear cache request (#117)
1 parent 249e82e commit 7a167a2

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

crates/pet-core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub struct Configuration {
3333
/// These are different from search_paths, as these are specific directories where environments are expected.
3434
/// environment_directories on the other hand can be any directory such as a workspace folder, where envs might never exist.
3535
pub environment_directories: Option<Vec<PathBuf>>,
36+
/// Directory to cache the Python environment details.
37+
pub cache_directory: Option<PathBuf>,
3638
}
3739

3840
pub trait Locator: Send + Sync {

crates/pet/src/jsonrpc.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub fn start_jsonrpc_server() {
8282
handlers.add_request_handler("resolve", handle_resolve);
8383
handlers.add_request_handler("find", handle_find);
8484
handlers.add_request_handler("condaInfo", handle_conda_telemetry);
85+
handlers.add_request_handler("clearCache", handle_clear_cache);
8586
start_server(&handlers)
8687
}
8788

@@ -112,7 +113,8 @@ pub fn handle_configure(context: Arc<Context>, id: u32, params: Value) {
112113
// We will not support changing the cache directories once set.
113114
// No point, supporting such a use case.
114115
if let Some(cache_directory) = configure_options.cache_directory {
115-
set_cache_directory(cache_directory)
116+
set_cache_directory(cache_directory.clone());
117+
cfg.cache_directory = Some(cache_directory);
116118
}
117119
trace!("Configuring locators: {:?}", cfg);
118120
drop(cfg);
@@ -381,3 +383,29 @@ pub fn handle_conda_telemetry(context: Arc<Context>, id: u32, _params: Value) {
381383
send_reply(id, info.into());
382384
});
383385
}
386+
387+
pub fn handle_clear_cache(context: Arc<Context>, id: u32, _params: Value) {
388+
thread::spawn(move || {
389+
if let Some(cache_directory) = context
390+
.configuration
391+
.read()
392+
.unwrap()
393+
.cache_directory
394+
.clone()
395+
{
396+
if let Err(e) = std::fs::remove_dir_all(&cache_directory) {
397+
error!("Failed to clear cache {:?}: {}", cache_directory, e);
398+
send_error(
399+
Some(id),
400+
-4,
401+
format!("Failed to clear cache {:?}: {}", cache_directory, e),
402+
);
403+
} else {
404+
info!("Cleared cache {:?}", cache_directory);
405+
send_reply(id, None::<()>);
406+
}
407+
} else {
408+
send_reply(id, None::<()>);
409+
}
410+
});
411+
}

0 commit comments

Comments
 (0)