Skip to content

Commit 57c2a2d

Browse files
committed
Shutdown properly
1 parent 17ab04c commit 57c2a2d

2 files changed

Lines changed: 26 additions & 15 deletions

File tree

src/main.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ extern crate notify;
22

33
use notify::{Watcher, RecursiveMode, RawEvent, raw_watcher};
44
use notify::op::Op;
5-
use std::sync::mpsc::{channel,Sender};
5+
use std::sync::mpsc::{channel,Sender,Receiver,RecvTimeoutError};
66

77
extern crate app_dirs;
88
use app_dirs::*;
@@ -13,6 +13,7 @@ use std::io::Seek;
1313
use std::io::SeekFrom;
1414
use std::io::Read;
1515
use std::thread;
16+
use std::time::Duration;
1617

1718
#[macro_use] extern crate lazy_static;
1819
extern crate regex;
@@ -156,7 +157,7 @@ impl FileScanner {
156157

157158
const APP_INFO: AppInfo = AppInfo{name: "Logs", author: "CrossoverWorkSmart"};
158159

159-
fn create_log_watch_thread(ui_tx: Sender<MeterControlMessage>)/* -> JoinHandle<()>*/ {
160+
fn create_log_watch_thread(ui_tx: Sender<MeterControlMessage>, shutdown_rx : Receiver<()>) -> thread::JoinHandle<()> {
160161
thread::spawn(move || {
161162
let mut path = get_app_root(AppDataType::UserConfig, &APP_INFO).unwrap();
162163
path.push("deskapp.log");
@@ -174,21 +175,29 @@ fn create_log_watch_thread(ui_tx: Sender<MeterControlMessage>)/* -> JoinHandle<(
174175
// Add a path to be watched. All files and directories at that path and
175176
// below will be monitored for changes.
176177
watcher.watch(path, RecursiveMode::NonRecursive).unwrap();
177-
loop {
178-
match rx.recv() {
178+
'recv_loop: loop {
179+
match rx.recv_timeout(Duration::from_secs(1)) {
179180
Ok(RawEvent { path: Some(_path), op: Ok(op), cookie: _cookie }) => scanner.handle_event(op).unwrap(),
180181
Ok(event) => println!("broken event: {:?}", event),
182+
Err(RecvTimeoutError::Timeout) => (), // that's OK
181183
Err(e) => println!("watch error: {:?}", e),
182184
}
185+
186+
for _ in shutdown_rx.try_iter() {
187+
println!("Received shutdown message.");
188+
break 'recv_loop;
189+
}
183190
}
184-
});
191+
})
185192
}
186193

187194
#[cfg(target_os = "windows")]
188195
fn main() {
189196
let (ui_tx, ui_rx) = channel();
190-
create_log_watch_thread(ui_tx);
191-
ten_minutes::TenMinutesMeter::new(ui_rx).main();
197+
let (shutdown_tx, shutdown_rx) = channel();
198+
let handle = create_log_watch_thread(ui_tx, shutdown_rx);
199+
ten_minutes::TenMinutesMeter::new(ui_rx, shutdown_tx).main();
200+
handle.join().ok();
192201
}
193202

194203
#[cfg(test)]

src/ten_minutes.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
extern crate systray;
22

3-
use std::sync::mpsc::Receiver;
3+
use std::sync::mpsc::{Receiver,Sender};
44

55
use std::time::{Duration, SystemTime, UNIX_EPOCH};
6-
use std::process;
6+
//use std::process;
77
use std::fmt;
88

99
pub enum MeterControlMessage {
@@ -25,6 +25,7 @@ impl fmt::Display for Icon {
2525
pub struct TenMinutesMeter {
2626
app : systray::Application,
2727
rx : Receiver<MeterControlMessage>,
28+
// shutdown_tx : Sender<()>,
2829
last_reminder : u16,
2930
photo_done : bool,
3031
screenshot_done : bool,
@@ -34,14 +35,15 @@ pub struct TenMinutesMeter {
3435
const TEN_MINUTES : u16 = 600;
3536

3637
impl TenMinutesMeter {
37-
pub fn new(rx : Receiver<MeterControlMessage>) -> TenMinutesMeter {
38+
pub fn new(rx : Receiver<MeterControlMessage>, shutdown_tx : Sender<()>) -> TenMinutesMeter {
3839
match systray::Application::new() {
3940
Ok(w) => {
4041
let mut app = w;
41-
TenMinutesMeter::init(&mut app);
42+
TenMinutesMeter::init(&mut app, shutdown_tx);
4243
TenMinutesMeter {
4344
app,
4445
rx,
46+
// shutdown_tx,
4547
last_reminder: 0,
4648
photo_done: false,
4749
screenshot_done: false,
@@ -52,12 +54,12 @@ impl TenMinutesMeter {
5254
}
5355
}
5456

55-
fn init(app : &mut systray::Application) -> () {
57+
fn init(app : &mut systray::Application, shutdown_tx : Sender<()>) -> () {
5658
app.set_icon_from_resource(&"Stop".to_string()).ok();
57-
app.add_menu_item(&"Quit".to_string(), |window| {
59+
app.add_menu_item(&"Quit".to_string(), move |window| {
5860
window.shutdown().ok();
5961
window.quit();
60-
process::exit(0);
62+
shutdown_tx.send(()).ok();
6163
}).ok();
6264
}
6365

@@ -86,7 +88,7 @@ impl TenMinutesMeter {
8688
self.screenshot_done = false;
8789
}
8890

89-
let icon : Icon;
91+
let icon;
9092
if TEN_MINUTES - reminder <= 5 { // Warn 5s before end of timecard
9193
icon = Icon::Warning;
9294
} else if self.photo_done && self.screenshot_done {

0 commit comments

Comments
 (0)