zng_ext_single_instance/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#![doc(html_favicon_url = "https://raw.githubusercontent.com/zng-ui/zng/main/examples/image/res/zng-logo-icon.png")]
#![doc(html_logo_url = "https://raw.githubusercontent.com/zng-ui/zng/main/examples/image/res/zng-logo.png")]
//!
//! Single app-process instance mode.
//!
//! # Crate
//!
#![doc = include_str!(concat!("../", std::env!("CARGO_PKG_README")))]

use std::{
    io::{Read, Write},
    time::Duration,
};

use zng_app::{
    event::{event, event_args},
    handler::{async_app_hn, clmv},
    AppExtension,
};
use zng_ext_fs_watcher::WATCHER;
use zng_txt::{ToTxt, Txt};

/// Single instance event manager.
///
/// # Events
///
/// Events this extension provides.
///
/// * [`APP_INSTANCE_EVENT`]
#[derive(Default)]
pub struct SingleInstanceManager {}
impl AppExtension for SingleInstanceManager {
    fn init(&mut self) {
        let args: Box<[_]> = std::env::args().map(Txt::from).collect();
        APP_INSTANCE_EVENT.notify(AppInstanceArgs::now(args, 0usize));

        let name = match SINGLE_INSTANCE.lock().as_ref().map(|l| l.name.clone()) {
            Some(n) => n,
            None => return, // app is running in a special process, like a crash dialog
        };

        let args_file = std::env::temp_dir().join(name);
        let mut count = 1usize;
        WATCHER
            .on_file_changed(
                &args_file,
                async_app_hn!(args_file, |_, _| {
                    let args = zng_task::wait(clmv!(args_file, || {
                        for i in 0..5 {
                            if i > 0 {
                                std::thread::sleep(Duration::from_millis(200));
                            }

                            // take args
                            // read all text and truncates the file
                            match std::fs::File::options().read(true).write(true).open(&args_file) {
                                Ok(mut file) => {
                                    let mut s = String::new();
                                    if let Err(e) = file.read_to_string(&mut s) {
                                        tracing::error!("error reading args (retry {i}), {e}");
                                        continue;
                                    }
                                    file.set_len(0).unwrap();
                                    return s;
                                }
                                Err(e) => {
                                    if e.kind() == std::io::ErrorKind::NotFound {
                                        return String::new();
                                    }
                                    tracing::error!("error reading args (retry {i}), {e}")
                                }
                            }
                        }
                        String::new()
                    }))
                    .await;

                    // parse args
                    for line in args.lines() {
                        let line = line.trim();
                        if line.is_empty() {
                            continue;
                        }

                        let args = match serde_json::from_str::<Box<[Txt]>>(line) {
                            Ok(args) => args,
                            Err(e) => {
                                tracing::error!("invalid args, {e}");
                                Box::new([])
                            }
                        };

                        APP_INSTANCE_EVENT.notify(AppInstanceArgs::now(args, count));

                        count += 1;
                    }
                }),
            )
            .perm();
    }
}

event_args! {
    /// Arguments for [`APP_INSTANCE_EVENT`].
    pub struct AppInstanceArgs {
        /// Arguments the app instance was started with.
        ///
        /// See [`std::env::args`] for more details.
        pub args: Box<[Txt]>,

        /// Instance count. Is zero for the current process, in single instance mode
        /// increments for each subsequent attempt to instantiate the app.
        pub count: usize,

        ..

        fn delivery_list(&self, _list: &mut UpdateDeliveryList) {}
    }
}
impl AppInstanceArgs {
    /// If the arguments are for the currently executing process (main).
    ///
    /// This is only `true` once, on the first event on startup.
    pub fn is_current(&self) -> bool {
        self.count == 0
    }
}

event! {
    /// App instance init event, with the arguments.
    ///
    /// This event notifies once on start. If the app is "single instance" this event will also notify for each
    /// new attempt to instantiate while the current process is already running.
    pub static APP_INSTANCE_EVENT: AppInstanceArgs;
}

zng_env::on_process_start!(|args| {
    if args.next_handlers_count > 0 && args.yield_count < zng_env::ProcessStartArgs::MAX_YIELD_COUNT {
        // absolute sure that this is the app-process
        return args.yield_once();
    }

    let mut lock = SINGLE_INSTANCE.lock();
    assert!(lock.is_none(), "single_instance already called in this process");

    let name = std::env::current_exe()
        .and_then(dunce::canonicalize)
        .expect("current exe is required")
        .display()
        .to_txt();
    let name: String = name
        .chars()
        .map(|c| if c.is_ascii_alphanumeric() || c == '-' { c } else { '_' })
        .collect();
    let mut name = name.as_str();
    if name.len() > 128 {
        name = &name[name.len() - 128..];
    }
    let name = zng_txt::formatx!("zng-si-{name}");

    let l = single_instance::SingleInstance::new(&name).expect("failed to create single instance lock");

    if l.is_single() {
        *lock = Some(SingleInstanceData { _lock: l, name });
    } else {
        tracing::info!("another instance running, will send args and exit");

        let args: Box<[_]> = std::env::args().collect();
        let args = format!("\n{}\n", serde_json::to_string(&args).unwrap());

        let try_write = move || -> std::io::Result<()> {
            let mut file = std::fs::File::options()
                .create(true)
                .append(true)
                .open(std::env::temp_dir().join(name.as_str()))?;
            file.write_all(args.as_bytes())
        };

        for i in 0..5 {
            if i > 0 {
                std::thread::sleep(std::time::Duration::from_millis(300));
            }
            match try_write() {
                Ok(_) => zng_env::exit(0),
                Err(e) => {
                    eprintln!("error writing args (retries: {i}), {e}");
                }
            }
        }
        zng_env::exit(1);
    }
});

struct SingleInstanceData {
    _lock: single_instance::SingleInstance,
    name: Txt,
}

static SINGLE_INSTANCE: parking_lot::Mutex<Option<SingleInstanceData>> = parking_lot::Mutex::new(None);