hotload/
hot_batch.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
200
201
202
use std::{ops::Deref, path::Path, sync::Arc};

use dlopen2::wrapper::{Container, WrapperApi};
use fast_able::vec::SyncVec;
use notify::{Event, RecursiveMode, Watcher};

use crate::{
    event::{EventNewDll, EventOldDll},
    types::{get_file_name, Hotload, UA},
    R,
};

pub const MAX_LOAD_INDEX: usize = 10;

pub struct HotloadBatch<API: WrapperApi> {
    inner_batch: Arc<SyncVec<Hotload<API>>>,
    dir: String,
    pub watch: UA<notify::RecommendedWatcher>,
}

// impl<API: WrapperApi + 'static> Deref for HotloadBatch<API> {
//     type Target = Container<API>;
//     fn deref(&self) -> &Self::Target {
//         let inner = &*self.inner;
//         let inner = &inner.container[inner.load_index];
//         inner.as_ref().expect("container not loaded")
//     }
// }

// // inpl DerefMut
// impl<API: WrapperApi + 'static> DerefMut for HotloadBatch<API> {
//     fn deref_mut(&mut self) -> &mut Self::Target {
//         let inner = self.inner.get_mut();
//         let inner = &mut inner.container[inner.load_index];
//         inner.as_mut().expect("container not loaded")
//     }
// }

const HOTLOAD_CACHE_DIR: &str = ".hotload_cache_dir";

impl<API: WrapperApi + 'static + Send> HotloadBatch<API> {
    pub fn new(dir_path: impl Into<String>) -> R<Self> {
        let path = dir_path.into();

        // 遍历文件夹里所有 动态库
        let mut batch_file = vec![];
        let p = Path::new(&path).to_path_buf();
        let dir_iter = std::fs::read_dir(p.clone())?;
        for ele in dir_iter {
            let ele = ele?;
            if !ele.file_type()?.is_file() {
                continue;
            }
            batch_file.push(ele.path());
        }

        let dir_cahe = Path::new(&path);
        let mut dir_cahe = dir_cahe.to_path_buf();
        dir_cahe.push(HOTLOAD_CACHE_DIR);
        _ = std::fs::create_dir(dir_cahe.clone());

        let r = SyncVec::new();
        for ele in batch_file.iter() {
            let p = ele.to_str().unwrap_or_else(|| "").to_string();
            if p.is_empty() {
                continue;
            }

            static PATHS: fast_able::vec::SyncVec<String> = fast_able::vec::SyncVec::new();
            PATHS.push(p);

            let item = Hotload::new(PATHS[PATHS.len() - 1].as_str());
            r.push(item);
        }

        Ok(Self {
            inner_batch: r.into(),
            dir: path,
            watch: UA::new(),
        })
    }

    /// "dll created" or "file rename to" is actively loaded
    /// frist time load EventOldDll is None
    /// load all "file_name-xxx" dll
    pub fn init_load<
        F: FnMut(EventNewDll<API>, Option<EventOldDll<API>>) + Send + 'static + Clone,
    >(
        &self,
        event_call: F,
    ) -> R<()> {
        for ele in self.inner_batch.iter() {
            ele.init_load(event_call.clone())?;
        }

        let inner_batch = self.inner_batch.clone();
        let mut watcher = notify::recommended_watcher(move |res: Result<Event, notify::Error>| {
            let event = match res {
                Ok(v) => v,
                Err(e) => {
                    error!("watch error: {e:?}");
                    return;
                }
            };

            // The file in the incident is not a monitored file and will not be processed
            let files = event
                .paths
                .iter()
                .filter(|x| x.is_file())
                .collect::<Vec<_>>();
            if files.is_empty() {
                return;
            }

            let mut is_action = false;
            match &event.kind {
                notify::EventKind::Create(_e) => is_action = true,
                notify::EventKind::Modify(notify::event::ModifyKind::Name(
                    notify::event::RenameMode::To,
                )) => is_action = true,
                _ => (),
            };

            if !is_action {
                return;
            }

            for ele in files {
                let full_path = ele.to_str().unwrap_or_else(|| "").to_string();
                if full_path.is_empty() {
                    continue;
                }

                let change_file_name = ele
                    .as_path()
                    .file_name()
                    .and_then(|x| x.to_str())
                    .unwrap_or_else(|| "")
                    .to_string();
                if change_file_name.is_empty() {
                    continue;
                }

                if inner_batch
                    .iter()
                    .find(|x| {
                        get_file_name(&x.inner.path).unwrap_or_else(|_| "".to_string())
                            == change_file_name
                    })
                    .is_some()
                {
                    continue;
                }

                debug!("event: {:?}; load new dll", event);

                static PATHS: fast_able::vec::SyncVec<String> = fast_able::vec::SyncVec::new();
                PATHS.push(full_path);

                let item = Hotload::<API>::new(PATHS[PATHS.len() - 1].as_str());
                if let Err(e) = item.init_load(event_call.clone()) {
                    error!("{e:?}; item.init_load(event_call.clone()): {change_file_name}")
                } else {
                    inner_batch.push(item);
                }
            }
        })?;

        // watch directory
        let dir = &self.dir;
        debug!("hoload watch path: {dir:?}");
        watcher.watch(Path::new(&dir), RecursiveMode::NonRecursive)?;
        self.watch.set(watcher);

        Ok(())
    }

    pub fn get(&self, index: usize) -> Option<&Container<API>> {
        self.inner_batch.get(index).map(|x| x.deref())
    }

    pub fn iter(&self) -> std::slice::Iter<'_, Hotload<API>> {
        self.inner_batch.iter()
    }
}

// IntoIterator
// impl<API: WrapperApi, 'a> IntoIterator for HotloadBatch<API> {
//     type IntoIter = std::slice::Iter<'a, Self::Item>;
//     type Item = Hotload<API>;
//     fn into_iter(self) -> Self::IntoIter {
//         self.inner_batch.into_iter()
//     }
// }

// impl<API: WrapperApi> Iterator for Hotload<API> {
//     type Item =  Hotload<API>;
//     fn next(&mut self) -> Option<Self::Item> {
//         todo!()
//     }
// }