2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, Error};
|
2019-04-11 06:26:52 +00:00
|
|
|
|
|
|
|
extern crate proxmox_backup;
|
|
|
|
|
|
|
|
extern crate tokio;
|
|
|
|
extern crate nix;
|
|
|
|
|
2021-11-23 16:57:00 +00:00
|
|
|
use proxmox_sys::fs::CreateOptions;
|
2021-10-08 09:19:37 +00:00
|
|
|
use proxmox_lang::try_block;
|
2021-11-23 16:57:00 +00:00
|
|
|
use proxmox_sys::{task_log, WorkerTaskContext};
|
2019-08-05 08:04:12 +00:00
|
|
|
|
2021-09-10 10:45:06 +00:00
|
|
|
use pbs_api_types::{Authid, UPID};
|
|
|
|
|
2021-09-30 10:31:38 +00:00
|
|
|
use proxmox_rest_server::{CommandSocket, WorkerTask};
|
2019-04-11 06:26:52 +00:00
|
|
|
|
2021-09-23 08:09:19 +00:00
|
|
|
fn garbage_collection(worker: &WorkerTask) -> Result<(), Error> {
|
2019-04-11 06:26:52 +00:00
|
|
|
|
2021-09-24 07:30:00 +00:00
|
|
|
task_log!(worker, "start garbage collection");
|
2019-04-11 06:26:52 +00:00
|
|
|
|
|
|
|
for i in 0..50 {
|
2021-09-24 05:40:49 +00:00
|
|
|
worker.check_abort()?;
|
2019-04-11 06:26:52 +00:00
|
|
|
|
2021-09-24 07:30:00 +00:00
|
|
|
task_log!(worker, "progress {}", i);
|
2019-04-11 06:26:52 +00:00
|
|
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(10));
|
|
|
|
}
|
|
|
|
|
2021-09-24 07:30:00 +00:00
|
|
|
task_log!(worker, "end garbage collection");
|
2019-04-11 06:26:52 +00:00
|
|
|
|
2021-01-15 13:38:27 +00:00
|
|
|
Ok(())
|
2019-04-11 06:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-14 08:42:44 +00:00
|
|
|
#[test]
|
|
|
|
#[ignore]
|
2019-04-11 06:26:52 +00:00
|
|
|
fn worker_task_abort() -> Result<(), Error> {
|
2021-09-23 08:09:19 +00:00
|
|
|
let uid = nix::unistd::Uid::current();
|
|
|
|
let gid = nix::unistd::Gid::current();
|
2021-09-24 07:30:00 +00:00
|
|
|
|
2021-09-23 08:09:19 +00:00
|
|
|
let file_opts = CreateOptions::new().owner(uid).group(gid);
|
|
|
|
proxmox_rest_server::init_worker_tasks("./target/tasklogtestdir".into(), file_opts.clone())?;
|
2021-09-24 07:30:00 +00:00
|
|
|
|
2019-04-11 06:26:52 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
|
|
|
let errmsg: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None));
|
|
|
|
let errmsg1 = errmsg.clone();
|
|
|
|
|
2020-12-04 10:53:34 +00:00
|
|
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
2019-08-29 08:05:43 +00:00
|
|
|
rt.block_on(async move {
|
2019-04-11 06:26:52 +00:00
|
|
|
|
2021-09-30 10:31:38 +00:00
|
|
|
let mut commando_sock = CommandSocket::new(
|
2021-09-23 08:09:19 +00:00
|
|
|
proxmox_rest_server::our_ctrl_sock(), nix::unistd::Gid::current());
|
2020-11-02 18:13:36 +00:00
|
|
|
|
2019-04-11 06:26:52 +00:00
|
|
|
let init_result: Result<(), Error> = try_block!({
|
2021-09-23 08:09:19 +00:00
|
|
|
proxmox_rest_server::register_task_control_commands(&mut commando_sock)?;
|
2019-04-11 06:26:52 +00:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Err(err) = init_result {
|
|
|
|
eprintln!("unable to start daemon - {}", err);
|
2019-08-29 08:05:43 +00:00
|
|
|
return;
|
2019-04-11 06:26:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 08:42:44 +00:00
|
|
|
if let Err(err) = commando_sock.spawn() {
|
|
|
|
eprintln!("unable to spawn command socket - {}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-11 06:26:52 +00:00
|
|
|
let errmsg = errmsg1.clone();
|
2021-09-23 08:09:19 +00:00
|
|
|
let res = WorkerTask::new_thread(
|
2020-08-06 13:46:01 +00:00
|
|
|
"garbage_collection",
|
|
|
|
None,
|
2021-09-23 08:09:19 +00:00
|
|
|
Authid::root_auth_id().to_string(),
|
2020-08-06 13:46:01 +00:00
|
|
|
true,
|
|
|
|
move |worker| {
|
|
|
|
println!("WORKER {}", worker);
|
|
|
|
|
|
|
|
let result = garbage_collection(&worker);
|
2021-09-21 05:58:40 +00:00
|
|
|
proxmox_rest_server::request_shutdown();
|
2020-08-06 13:46:01 +00:00
|
|
|
|
|
|
|
if let Err(err) = result {
|
|
|
|
println!("got expected error: {}", err);
|
|
|
|
} else {
|
|
|
|
let mut data = errmsg.lock().unwrap();
|
|
|
|
*data = Some(String::from("thread finished - seems abort did not work as expected"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
);
|
2019-04-11 06:26:52 +00:00
|
|
|
|
|
|
|
match res {
|
|
|
|
Err(err) => {
|
|
|
|
println!("unable to start worker - {}", err);
|
|
|
|
}
|
|
|
|
Ok(wid) => {
|
|
|
|
println!("WORKER: {}", wid);
|
2021-09-30 08:33:57 +00:00
|
|
|
proxmox_rest_server::abort_worker_nowait(wid.parse::<UPID>().unwrap());
|
2021-09-23 08:09:19 +00:00
|
|
|
proxmox_rest_server::wait_for_local_worker(&wid).await.unwrap();
|
2021-09-14 08:42:44 +00:00
|
|
|
}
|
2019-04-11 06:26:52 +00:00
|
|
|
}
|
2019-08-29 08:05:43 +00:00
|
|
|
});
|
2019-04-11 06:26:52 +00:00
|
|
|
|
|
|
|
let data = errmsg.lock().unwrap();
|
|
|
|
match *data {
|
|
|
|
Some(ref err) => bail!("Error: {}", err),
|
|
|
|
None => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|