Skip to content

Commit

Permalink
Merge pull request #20 from skysea04/dev
Browse files Browse the repository at this point in the history
ADD:  Handle system maintenance
  • Loading branch information
skysea04 authored May 13, 2024
2 parents 8f133d5 + cebeee4 commit d825bc5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/booking/constants/page_htmls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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() == '網路訂票系統暫停服務中'
26 changes: 13 additions & 13 deletions src/booking/management/commands/continue_snap_up.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/booking/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions src/booking/tasks.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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

0 comments on commit d825bc5

Please sign in to comment.