diff --git a/src/booking/constants/page_htmls.py b/src/booking/constants/page_htmls.py
index bd2a789..61e6691 100644
--- a/src/booking/constants/page_htmls.py
+++ b/src/booking/constants/page_htmls.py
@@ -88,3 +88,6 @@ class ErrorPage:
def server_busy(tag):
return tag.name == 'p' and '系統忙碌中,請耐心等候,並請勿使用「重新整理」鍵或離開本頁面。' in tag.text
+
+ def in_maintenance(tag):
+ return tag.name == 'h1' and tag.text.strip() == '網路訂票系統暫停服務中'
diff --git a/src/booking/management/commands/continue_snap_up.py b/src/booking/management/commands/continue_snap_up.py
index 907e3d6..49b9f46 100644
--- a/src/booking/management/commands/continue_snap_up.py
+++ b/src/booking/management/commands/continue_snap_up.py
@@ -1,10 +1,9 @@
import logging
import signal
import traceback
-from datetime import time
+from datetime import time, timedelta as td
from time import sleep
-from basis.conf import CURRENT_TZ
from basis.constants import Time
from basis.logger import log
from booking import tasks
@@ -30,22 +29,23 @@ def sigterm_handler(*args): # ignore signum and frame
def switch_requests_status():
- now = tz.now().astimezone(CURRENT_TZ)
+ now = tz.localtime()
if now.time() < time(0, 3):
tasks.update_not_yet_requests_to_pending()
-def take_a_break(all_success: bool):
- now = tz.now().astimezone(CURRENT_TZ)
- now_time = now.time()
- if now_time < time(23, 59):
+def take_a_break(all_success: bool, in_maintenance: bool):
+ now = tz.localtime()
+ next_day = now.replace(hour=0, minute=0, second=0) + td(days=1)
+ time_diff = (next_day - now).seconds
+
+ if in_maintenance:
+ sleep(min(time_diff, Time.TEN_MINUTES))
+ else:
if all_success:
- sleep(Time.ONE_MINUTE)
+ sleep(min(Time.ONE_MINUTE, time_diff))
else:
sleep(Time.FIFTEEN_SECONDS)
- else:
- time_diff = Time.ONE_MINUTE - now_time.second
- sleep(time_diff)
class Command(BaseCommand):
@@ -54,10 +54,10 @@ def handle(self, *args, **options):
try:
log.info('Start snap up')
switch_requests_status()
- all_success = tasks.book_all_pending_reqests()
+ all_success, in_maintenance = tasks.book_all_pending_reqests()
tasks.expire_pending_requests()
log.info('Snap up fininshed, take a break')
- take_a_break(all_success)
+ take_a_break(all_success, in_maintenance)
except Exception as e:
log.error(e)
diff --git a/src/booking/services.py b/src/booking/services.py
index 01e5de9..97573b8 100644
--- a/src/booking/services.py
+++ b/src/booking/services.py
@@ -205,6 +205,10 @@ def get_holidays_reservation_start_date():
def check_resp_ok(page: Tag) -> Tuple[bool, Optional[str]]:
+ maintenance_tag = page.find(ErrorPage.in_maintenance)
+ if maintenance_tag:
+ return False, '高鐵系統維護中'
+
feedback_tag = page.find(**ErrorPage.ERROR_FEEDBACK)
if feedback_tag:
return False, feedback_tag.find('span').text
diff --git a/src/booking/tasks.py b/src/booking/tasks.py
index b2c6b0d..7708be8 100644
--- a/src/booking/tasks.py
+++ b/src/booking/tasks.py
@@ -1,3 +1,5 @@
+import traceback
+from typing import Tuple
from datetime import datetime as dt, timedelta as td
from basis.conf import CURRENT_TZ
@@ -130,9 +132,10 @@ def expire_pending_requests():
request.save()
-def book_all_pending_reqests() -> bool:
+def book_all_pending_reqests() -> Tuple[bool, bool]:
pending_requests = BookingRequest.get_all_by_status(BookingRequest.Status.PENDING)
counter = 0
+ in_maintenance = False
for request in pending_requests:
try:
booking_task(request)
@@ -141,9 +144,13 @@ def book_all_pending_reqests() -> bool:
log.error(e)
request.error_msg = str(e)
request.save()
+ if e.__str__() == '高鐵系統維護中':
+ in_maintenance = True
+
except Exception as e:
log.error(e)
+ log.error(traceback.format_exc())
request.error_msg = str(e)
request.save()
- return counter == len(pending_requests)
+ return counter == len(pending_requests), in_maintenance