|
3 | 3 | import time |
4 | 4 | import random |
5 | 5 | import argparse |
| 6 | +import subprocess |
6 | 7 | from datetime import datetime, date |
7 | 8 |
|
8 | 9 | from PyQt5.QtWidgets import QApplication |
|
11 | 12 | from src.utils.log import Log |
12 | 13 | from src.utils.utils import Utils |
13 | 14 | from src.ui.ui import ConfigWindow |
14 | | -from src.core.clock_manager import run_clock |
15 | 15 | from src.utils.const import Key, AppPath |
16 | | -from src.extend.email_server import send_email_by_result |
17 | 16 | from ui.init_ui import init_ui |
18 | 17 | import chinese_calendar as calendar |
19 | 18 |
|
@@ -114,108 +113,16 @@ def run_windows_sleep(delay=30): |
114 | 113 | Log.error("使用 --help 参数查看所有可用选项") |
115 | 114 | sys.exit(1) |
116 | 115 |
|
117 | | - config_data = Utils.read_dict_from_json(AppPath.DataJson) |
118 | | - send_email_success = False |
119 | | - send_email_failed = False |
120 | | - email = None |
121 | | - if config_data and config_data.get(Key.NotificationEmail): |
122 | | - email = config_data.get(Key.NotificationEmail) |
123 | | - send_email_success = config_data.get(Key.SendEmailWhenSuccess, False) |
124 | | - send_email_failed = config_data.get(Key.SendEmailWhenFailed, False) |
125 | | - |
126 | | - ok = False |
127 | | - error = None |
128 | | - task = None |
129 | | - try: |
130 | | - if args.task_id: |
131 | | - task = Utils.find_task(args.task_id) |
132 | | - Log.info(f"Auto Clock Get Task Id: {args.task_id}") |
133 | | - if not task: |
134 | | - raise Exception(f"Task ID: {args.task_id} not found.") |
135 | | - operation = task.get(Key.Operation) |
136 | | - day_time_type = task.get(Key.DayTimeType) |
137 | | - trigger_type = task.get(Key.TriggerType) |
138 | | - |
139 | | - # SmartHoliday: only execute on Chinese workdays within the same year |
140 | | - if trigger_type == Key.SmartHoliday: |
141 | | - try: |
142 | | - start_year = int(task.get(Key.Year)) |
143 | | - start_month = int(task.get(Key.Month)) |
144 | | - start_day = int(task.get(Key.Day)) |
145 | | - start_date = date(start_year, start_month, start_day) |
146 | | - except Exception as e: |
147 | | - Log.error(f"SmartHoliday task missing or invalid start date, skip execution. Error: {e}") |
148 | | - sys.exit(1) |
149 | | - |
150 | | - today = date.today() |
151 | | - |
152 | | - # Only valid in the creation year |
153 | | - if today.year != start_year: |
154 | | - Log.info(f"SmartHoliday task year mismatch (task: {start_year}, today: {today.year}), skip execution.") |
155 | | - sys.exit(0) |
156 | | - |
157 | | - # Only valid between start_date and end of year |
158 | | - if today < start_date or today > date(start_year, 12, 31): |
159 | | - Log.info(f"SmartHoliday: today {today} is out of valid range [{start_date}, {start_year}-12-31], skip execution.") |
160 | | - sys.exit(0) |
161 | | - |
162 | | - # Only execute on Chinese workdays (including adjusted workdays/holidays) |
163 | | - if not calendar.is_workday(today): |
164 | | - Log.info(f"SmartHoliday: today {today} is not a Chinese workday, skip execution.") |
165 | | - sys.exit(0) |
166 | | - |
167 | | - if day_time_type and day_time_type == Key.Random: |
168 | | - time_offset = task.get(Key.TimeOffset, 0) |
169 | | - random_sec = random.randint(0, time_offset) |
170 | | - Log.info(f"将等待 {random_sec} 秒...") |
171 | | - time.sleep(random_sec) |
172 | | - Log.info("等待结束!继续执行任务") |
173 | | - |
174 | | - Log.info(f"Task ID: {args.task_id} has found, Task Name: {task.get(Key.TaskName)} Operation: {operation}") |
175 | | - start_time = datetime.now() |
176 | | - |
177 | | - if operation == Key.AutoClock: |
178 | | - ok, error = run_clock() |
179 | | - elif operation == Key.ShutDownSystem: |
180 | | - ok, error = run_windows_shutdown(30) |
181 | | - elif operation == Key.SystemSleep: |
182 | | - ok, error = run_windows_sleep(30) |
183 | | - elif operation == Key.DisconnectNetwork: |
184 | | - # 对于断网操作,Windows和Linux都支持延迟参数 |
185 | | - ok, error = disconnect_network(30) |
186 | | - elif operation == Key.ConnectNetwork: |
187 | | - # 对于联网操作,通常不需要延迟 |
188 | | - ok, error = connect_network() |
189 | | - # 联网后等待10秒,确保网络已经稳定连接 |
190 | | - if ok: |
191 | | - Log.info("网络连接成功,等待10秒确保网络稳定...") |
192 | | - time.sleep(10) |
193 | | - Log.info("网络稳定等待完成") |
194 | | - else: |
195 | | - error = f"No operation specified for: {operation}" |
196 | | - |
197 | | - end_time = datetime.now() |
198 | | - elapsed_time = end_time - start_time |
199 | | - elapsed_sec = elapsed_time.total_seconds() |
200 | | - task[Key.CostTime] = int(elapsed_sec) |
201 | | - Log.info(f"Finish Task. Start at: {start_time} End at: {end_time} Cost time: {elapsed_sec} sec") |
202 | | - else: |
203 | | - exit() |
204 | | - except Exception as e: |
205 | | - error = str(e) |
206 | | - |
207 | | - config_data = Utils.read_dict_from_json(AppPath.DataJson) |
208 | | - if not config_data or not config_data.get(Key.NotificationEmail): exit() |
| 116 | + if not args.task_id: |
| 117 | + Log.error("任务模式需要提供task_id参数") |
| 118 | + sys.exit(1) |
209 | 119 |
|
210 | | - email = config_data.get(Key.NotificationEmail) |
211 | | - send_email_success = config_data.get(Key.SendEmailWhenSuccess, False) |
212 | | - send_email_failed = config_data.get(Key.SendEmailWhenFailed, False) |
| 120 | + runner_cmd = Utils.get_runner_execute_file() |
| 121 | + cmd = f"{runner_cmd} --task_id={args.task_id}" |
| 122 | + if args.headless: |
| 123 | + cmd += " --headless" |
213 | 124 |
|
214 | | - if not error: error = "Unknow Error" |
215 | | - Log.info(f"task: {task}") |
216 | | - Log.info(f"ok: {ok} error: {error}") |
217 | | - Log.info(f"email: {email} send_email_success: {send_email_success} send_email_failed: {send_email_failed}") |
218 | | - |
219 | | - send_email_by_result(task=task, email=email, send_email_success=send_email_success, |
220 | | - send_email_failed=send_email_failed, ok=ok, error=error) |
| 125 | + Log.info(f"Delegate task to runner: {cmd}") |
| 126 | + result = subprocess.run(cmd, shell=True) |
| 127 | + sys.exit(result.returncode) |
221 | 128 | Log.close() |
0 commit comments