diff --git a/youtube_dl_webui/core.py b/youtube_dl_webui/core.py index f544b1a..5544947 100644 --- a/youtube_dl_webui/core.py +++ b/youtube_dl_webui/core.py @@ -186,6 +186,7 @@ def event_batch(cls, svr, event, data, arg): class WorkMsgDispatcher(object): _task_mgr = None + logger = logging.getLogger('ydl_webui') @classmethod def init(cls, task_mgr): @@ -212,10 +213,13 @@ def event_fatal(cls, svr, event, data, arg): @classmethod def event_progress(cls, svr, event, data, arg): tid, data = data['tid'], data['data'] - cls._task_mgr.progress_update(tid, data) - - if data['status'] == 'finished': - cls._task_mgr.finish_task(tid) + try: + cls._task_mgr.progress_update(tid, data) + except TaskInexistenceError: + cls.logger.error('Cannot update progress, task does not exist') + else: + if data['status'] == 'finished': + cls._task_mgr.finish_task(tid) def load_conf_from_file(cmd_args): diff --git a/youtube_dl_webui/msg.py b/youtube_dl_webui/msg.py index caba0ba..2ca3650 100644 --- a/youtube_dl_webui/msg.py +++ b/youtube_dl_webui/msg.py @@ -50,9 +50,11 @@ def __init__(self): def new_cli(self, cli_name=None): uuid = None if cli_name is not None: + # For named client, we create unique queue for communicating with server uuid = cli_name cli = CliMsg(cli_name, Queue(), self._svrQ) else: + # Anonymous client is a client who needn't to talk to the server. uuid = new_uuid() cli = CliMsg(uuid, None, self._svrQ) @@ -61,6 +63,10 @@ def new_cli(self, cli_name=None): return cli def reg_event(self, event, cb_func, arg=None): + # callback functions should have the signature of callback(svr, event, data, arg) + # + # svr is an instance of SrvMsg class, so the callback can directly send + # mssages via svr to its corresponding client. self._evnt_cb_dict[event] = (cb_func, arg) def run(self): diff --git a/youtube_dl_webui/task.py b/youtube_dl_webui/task.py index b9e0a3c..218bb97 100644 --- a/youtube_dl_webui/task.py +++ b/youtube_dl_webui/task.py @@ -149,7 +149,7 @@ def start_task(self, tid, ignore_state=False): if status['state'] == state_index['finished']: raise TaskError('Task is finished') - task = Task(tid, self._msg_cli, ydl_opts=ydl_opts, info=info, + task = Task(tid, self._msg_cli, ydl_opts=ydl_opts, info=info, status=status, log_size=self._conf['general']['log_size']) self._tasks_dict[tid] = task @@ -279,5 +279,10 @@ def launch_unfinished(self): tid_list = self._db.launch_unfinished() for t in tid_list: - self.start_task(t) + try: + self.start_task(t) + except TaskError as e: + self.logger.warn("Task %s is in downloading or finished state", tid) + except TaskInexistenceError: + self.logger.error('Task does not exist')