start to run the task :param running_tasks: running task list :param lock_source_dir: lock to manipulate source dir
(self)
| 149 | return self._done |
| 150 | |
| 151 | def start(self): |
| 152 | """start to run the task |
| 153 | |
| 154 | :param running_tasks: running task list |
| 155 | :param lock_source_dir: lock to manipulate source dir |
| 156 | """ |
| 157 | try: |
| 158 | with open(self.request_file, 'r', encoding='utf-8') as rf: |
| 159 | task_request = json.load(rf) |
| 160 | task_display_name = ToolDisplay.get_tool_display_name(task_request) |
| 161 | |
| 162 | if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'): |
| 163 | # LogPrinter.info('running in a PyInstaller bundle') |
| 164 | puppy_task_name = "scantask" |
| 165 | if settings.PLATFORMS[sys.platform] == 'windows': |
| 166 | puppy_task_name = '%s.exe' % puppy_task_name |
| 167 | |
| 168 | puppy_task_path = os.path.join(settings.BASE_DIR, puppy_task_name) |
| 169 | # 以可执行程序的方式调用 |
| 170 | cmd_args = [puppy_task_path, self.request_file, self.response_file] |
| 171 | else: |
| 172 | python_cmd = "python3" # python3命令 |
| 173 | # LogPrinter.info('running in python code') |
| 174 | if not PythonTool.is_local_python_command_available(python_cmd, python_version="3"): |
| 175 | raise ConfigError("python3 command is not available, please install Python3.") |
| 176 | |
| 177 | cmd_args = [python_cmd, 'task/puppytask.py', self.request_file, self.response_file] |
| 178 | |
| 179 | LogPrinter.info(f'Task_{task_request["id"]} ({task_display_name}) starts ...') |
| 180 | |
| 181 | # 命令行参数加上引号,避免路径包含空格触发命令异常 |
| 182 | cmd_args = PathMgr().format_cmd_arg_list(cmd_args) |
| 183 | |
| 184 | self._task_proc = SubProcController(cmd_args, |
| 185 | stdout_filepath=self.task_log, |
| 186 | stderr_filepath=self.task_log, |
| 187 | env=EnvSet().get_origin_env(self.env)) |
| 188 | |
| 189 | self._task_expired_time = time.time() + app.settings.TASK_EXPIRED.total_seconds() |
| 190 | |
| 191 | except Exception as err: |
| 192 | logger.exception('encounter error when starting task process') |
| 193 | self.code = errcode.E_NODE_TASK |
| 194 | self.msg = u"%s: %s" % (type(err).__name__, err) |
| 195 | self.data = traceback.format_exc() |
| 196 | self._done = True |