mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 21:12:57 -05:00
File formats: a DWG being placed can still be opened, and a worker ends by its deadline
- File > Open of a DWG drawing that is being converted for Place no longer does nothing: only conversions started for opening count as that file already being opened. - A DWG worker gets an absolute deadline (`--dwg-worker-until`, in milliseconds since the UNIX epoch) instead of a time limit counted from its own start. `thumbnail` sets it when its own timer starts, so a worker started late no longer outlives the thumbnail process. A worker whose deadline has passed exits with code 1 at once.
This commit is contained in:
1 parent
5436a18547
commit
d4670a94cf
5 files changed
+61
-26
No files matched your search
@@ -421,21 +421,25 @@ fn info_reads_a_dwg_and_a_damaged_one_is_an_error_not_a_crash() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A DWG worker started with a time limit (as `thumbnail` starts one) exits with code 1 once the
|
||||
/// time is up, even while it still waits for its drawing, so `thumbnail`'s exit leaves no
|
||||
/// conversion running.
|
||||
/// A DWG worker started with a deadline (as `thumbnail` starts one) exits with code 1 once the
|
||||
/// deadline passes, even while it still waits for its drawing, and at once when it has already
|
||||
/// passed, so `thumbnail`'s exit leaves no conversion running.
|
||||
#[cfg(not(any(target_arch = "wasm32", all(target_os = "freebsd", target_arch = "aarch64"))))]
|
||||
#[test]
|
||||
fn a_dwg_worker_with_a_time_limit_exits_when_the_time_is_up() {
|
||||
let mut worker = Command::new(crate::bin())
|
||||
.args(["--dwg-worker", "--dwg-worker-seconds", "1"])
|
||||
.stdin(std::process::Stdio::piped())
|
||||
.stdout(std::process::Stdio::null())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
// Its input stays open, so the worker never gets the whole request.
|
||||
let input = worker.stdin.take();
|
||||
let status = worker.wait().unwrap();
|
||||
drop(input);
|
||||
assert_eq!(status.code(), Some(1), "{status:?}");
|
||||
let now = std::time::SystemTime::now();
|
||||
for until in [now + std::time::Duration::from_secs(1), now - std::time::Duration::from_secs(1)] {
|
||||
let millis = until.duration_since(std::time::UNIX_EPOCH).unwrap().as_millis().to_string();
|
||||
let mut worker = Command::new(crate::bin())
|
||||
.args(["--dwg-worker", "--dwg-worker-until", &millis])
|
||||
.stdin(std::process::Stdio::piped())
|
||||
.stdout(std::process::Stdio::null())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
// Its input stays open, so the worker never gets the whole request.
|
||||
let input = worker.stdin.take();
|
||||
let status = worker.wait().unwrap();
|
||||
drop(input);
|
||||
assert_eq!(status.code(), Some(1), "{status:?}");
|
||||
}
|
||||
}
|
||||
+18
-11
@@ -10,9 +10,9 @@ use std::sync::{Arc, LazyLock, Mutex, PoisonError, Weak};
|
||||
|
||||
pub use craft_dwg::{WORKER_ARG, allow_in_process, use_own_worker};
|
||||
|
||||
/// The worker argument after [`WORKER_ARG`] that gives the most seconds the worker runs
|
||||
/// The worker argument after [`WORKER_ARG`] that gives its deadline in milliseconds since the UNIX epoch
|
||||
/// ([`use_own_worker_for`]).
|
||||
const WORKER_SECONDS_ARG: &str = "--dwg-worker-seconds";
|
||||
const WORKER_UNTIL_ARG: &str = "--dwg-worker-until";
|
||||
|
||||
/// Serve one conversion and return the exit code if this process was started as a worker. A
|
||||
/// worker started by [`use_own_worker_for`] exits with code 1 once its time is up, wherever the
|
||||
@@ -22,25 +22,32 @@ pub fn serve_if_asked() -> Option<i32> {
|
||||
if args.next()? != WORKER_ARG {
|
||||
return None;
|
||||
}
|
||||
if args.next().is_some_and(|a| a == WORKER_SECONDS_ARG)
|
||||
&& let Some(seconds) = args.next().and_then(|s| s.to_str()?.parse::<u64>().ok())
|
||||
if args.next().is_some_and(|a| a == WORKER_UNTIL_ARG)
|
||||
&& let Some(millis) = args.next().and_then(|s| s.to_str()?.parse::<u64>().ok())
|
||||
&& let Some(until) = std::time::UNIX_EPOCH.checked_add(std::time::Duration::from_millis(millis))
|
||||
{
|
||||
if until <= std::time::SystemTime::now() {
|
||||
return Some(1);
|
||||
}
|
||||
std::thread::spawn(move || {
|
||||
std::thread::sleep(std::time::Duration::from_secs(seconds));
|
||||
std::thread::sleep(until.duration_since(std::time::SystemTime::now()).unwrap_or_default());
|
||||
std::process::exit(1);
|
||||
});
|
||||
}
|
||||
craft_dwg::serve_if_asked()
|
||||
}
|
||||
|
||||
/// [`use_own_worker`], each worker exiting once `limit` (in whole seconds, at least 1) is up, so a
|
||||
/// program that ends after `limit` leaves no conversion running. If this program's path can't be
|
||||
/// found, the worker setting does not change. Web builds have no worker.
|
||||
/// [`use_own_worker`], each worker exiting once `limit` from this call is up (a deadline passed to it
|
||||
/// in milliseconds since the UNIX epoch), however late it starts, so a program that ends after
|
||||
/// `limit` leaves no conversion running. If this program's path can't be found, or the deadline
|
||||
/// can't be written, the worker setting does not change. Web builds have no worker.
|
||||
pub fn use_own_worker_for(limit: std::time::Duration) {
|
||||
#[cfg(not(any(target_arch = "wasm32", all(target_os = "freebsd", target_arch = "aarch64"))))]
|
||||
if let Ok(program) = std::env::current_exe() {
|
||||
let seconds = limit.as_secs().max(1).to_string();
|
||||
craft_dwg::worker::use_worker(program, vec![WORKER_ARG.into(), WORKER_SECONDS_ARG.into(), seconds.into()]);
|
||||
if let Ok(program) = std::env::current_exe()
|
||||
&& let Some(until) = std::time::SystemTime::now().checked_add(limit).and_then(|until| until.duration_since(std::time::UNIX_EPOCH).ok())
|
||||
&& let Ok(millis) = u64::try_from(until.as_millis())
|
||||
{
|
||||
craft_dwg::worker::use_worker(program, vec![WORKER_ARG.into(), WORKER_UNTIL_ARG.into(), millis.to_string().into()]);
|
||||
}
|
||||
#[cfg(any(target_arch = "wasm32", all(target_os = "freebsd", target_arch = "aarch64")))]
|
||||
let _ = limit;
|
||||
|
||||
@@ -888,6 +888,23 @@ mod tests {
|
||||
});
|
||||
}
|
||||
|
||||
/// File › Open of a DWG drawing being converted for Place converts it for opening too.
|
||||
#[cfg(not(any(target_arch = "wasm32", all(target_os = "freebsd", target_arch = "aarch64"))))]
|
||||
#[test]
|
||||
fn a_dwg_drawing_being_placed_can_also_be_opened() {
|
||||
vectorcraft_engine::dwg::allow_in_process();
|
||||
let (_dir, plan) = dwg_file("dxf-import-dwg-place-open");
|
||||
crate::opening::tests::with_threads(|| {
|
||||
let mut app = host::app();
|
||||
app.services.read = Some(Box::new(|p: &str| std::fs::read(p).map_err(|e| e.to_string())));
|
||||
app.run("file.new", json!({"width": 600, "height": 400})).unwrap();
|
||||
crate::place::run_for_user(&mut app, &json!({ "path": plan })).unwrap();
|
||||
crate::io::open_file(&mut app, &plan, true).unwrap();
|
||||
assert_eq!(app.opening.converting.len(), 2);
|
||||
finish_conversions(&mut app);
|
||||
});
|
||||
}
|
||||
|
||||
/// A DWG drawing placed while it is converted goes in the document it was placed in, which
|
||||
/// becomes the active one again, on the layer that was current when Place started and, with
|
||||
/// `replace`, in place of the object selected then; it is not placed when that document was
|
||||
|
||||
@@ -641,7 +641,7 @@ fn open_file_with(app: &mut VectorcraftApp, path: &str, background: bool, p: &Va
|
||||
if let Some(r) = try_activate_open_document(app, path, canonical.as_deref())? {
|
||||
return Ok(r);
|
||||
}
|
||||
if app.opening.pending.iter().any(|p| same(&p.path)) || app.opening.converting.iter().any(|c| c.path.as_deref().is_some_and(&same)) {
|
||||
if app.opening.pending.iter().any(|p| same(&p.path)) || app.opening.converting.iter().any(|c| c.opens() && c.path.as_deref().is_some_and(&same)) {
|
||||
return Ok(Value::Null);
|
||||
}
|
||||
// A shortcut set is read by Import Shortcut Set, up to its reader's cap.
|
||||
|
||||
@@ -61,6 +61,13 @@ pub struct Converting {
|
||||
started: f64,
|
||||
}
|
||||
|
||||
impl Converting {
|
||||
/// Is this drawing being converted for opening?
|
||||
pub(crate) fn opens(&self) -> bool {
|
||||
self.place.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
/// Where a DWG drawing placed while it is converted goes, taken when Place started.
|
||||
pub(crate) struct Placement {
|
||||
/// The document ([`vectorcraft_engine::DocState::uid`]) and its
|
||||
|
||||
Reference in new issue
Block a user