-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.py
52 lines (46 loc) · 1.86 KB
/
worker.py
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
# worker.py
import threading
import logging
class EzShareWorker(threading.Thread):
def __init__(self, ezshare, queue, name="EzShareWorkerThread", app=None):
super().__init__(name=name)
self.ezshare = ezshare
self.queue = queue
self.app = app
self._is_running = True
def run(self):
logging.info(f"{self.name} started.")
try:
self.ezshare.set_progress_callback(self.update_progress)
self.ezshare.set_status_callback(self.update_status)
self.ezshare.run()
except Exception as e:
logging.error(f"{self.name} encountered an error: {str(e)}")
self.update_status(f'Error: {e}', 'error')
finally:
self._cleanup()
def update_progress(self, value):
logging.debug(f"{self.name} updating progress: {value}")
if value == 'no_files':
self.queue.put(('no_files',))
else:
self.queue.put(('progress', min(max(0, value), 100)))
def update_status(self, message, message_type='info'):
logging.debug(f"{self.name} updating status: {message} (type: {message_type})")
if self.app:
self.queue.put(('status', message, message_type))
else:
logging.error("Cannot update status: app object is None.")
def stop(self):
logging.info(f"{self.name} stop requested.")
self._is_running = False
self.ezshare.stop()
self.queue.put(('stop',))
def _cleanup(self):
logging.info(f"Cleaning up {self.name}.")
if self.ezshare.connected:
self.ezshare.connection_manager.disconnect(self.ezshare.ssid)
self.ezshare.connected = False
success = (self.ezshare.processed_files == self.ezshare.total_files)
self.queue.put(('finished', success))
logging.info(f"{self.name} cleanup completed.")