Skip to content

Commit

Permalink
Merge pull request #16 from skysea04/dev
Browse files Browse the repository at this point in the history
Add health check and change README
  • Loading branch information
skysea04 authored Apr 20, 2024
2 parents 521a2f5 + 93d0b02 commit 635fe64
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ ADD requirements /requirements

RUN mkdir -p logs/ && \
apt update && \
apt install -y libpq5 procps && \
pip install -r /requirements/live.txt
apt install -y libpq5 procps cron && \
pip install -r /requirements/live.txt && \
echo "30 */8 * * * python manage.py snap_up_health_check" >> /var/spool/cron/crontabs/root

CMD nohup python manage.py continue_snap_up & gunicorn --bind=0.0.0.0:8000 --timeout=300 --keep-alive=60 src.wsgi:application
CMD nohup python manage.py continue_snap_up & cron && gunicorn --bind=0.0.0.0:8000 --timeout=300 --keep-alive=60 src.wsgi:application
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Snap Up THSR (Work in progress)
# Snap Up THSR

__!!此專案為純研究用途,請勿用於不當交易行為!!__

這是一份自動化高鐵購票系統的後端程式碼
這是一份自動化高鐵購票系統,使用 Django 後台進行使用者管理與訂票提單系統,協助完成訂票的最後一哩路。

使用技術:
- Django
- beautifulsoup4

本系統已實踐會員系統與基礎購票功能,未來將會陸續新增以下功能:
1. 多種類的自動購票策略
2. 疏運期間售票時間調整
2. 疏運期間售票提醒
3 changes: 1 addition & 2 deletions src/booking/admin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from django.contrib import admin

from basis.admin import ForeignKeyLinksMixin
from user.admin import UserAdminMixin, UserFilter
from user.models import User
from user.admin import UserAdminMixin

# Register your models here.
from .models import THSRTicket, BookingRequest
Expand Down
36 changes: 36 additions & 0 deletions src/booking/management/commands/snap_up_health_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django.conf import settings
from django.core.management.base import BaseCommand
from django.core.mail import send_mail
from basis.logger import log


def count_lines(filename: str) -> int:
with open(filename, 'r') as file:
lines = file.readlines()
return len(lines)


def clear_log(filename: str):
with open(filename, 'w') as file:
file.write('')


LOG_PATH = '/src/logs/django.log'


class Command(BaseCommand):
help = 'Check the health of the application'

def handle(self, *args, **options):
lines = count_lines(LOG_PATH)
if lines:
clear_log(LOG_PATH)
log.info('Snap up health check: OK')
return

send_mail(
subject='Snap Up THSR 工作排程警告',
message='Snap Up THSR 的工作排程已久無運作,記得去檢查一下!',
from_email=settings.EMAIL_HOST_USER,
recipient_list=['[email protected]'],
)
15 changes: 0 additions & 15 deletions src/user/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,6 @@ def save_model(self, request, obj, form, change):
super().save_model(request, obj, form, change)


class UserFilter(admin.SimpleListFilter):
title = ('使用者帳號')
parameter_name = 'user'

def lookups(self, request, model_admin):
if request.user.is_superuser:
return [(user.id, user.email) for user in User.objects.all()]
return [(request.user.id, request.user.email)]

def queryset(self, request, queryset):
if self.value():
return queryset.filter(user__id=self.value())
return queryset


class CustomUserAdmin(UserAdmin):
# Update the list of fields to display in the admin interface.
list_display = ('email', 'personal_id', 'phone', 'buy_discount_ticket', 'use_tgo_account')
Expand Down

0 comments on commit 635fe64

Please sign in to comment.