-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_background.py
69 lines (56 loc) · 2.05 KB
/
main_background.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from traceback import format_exc
from schedule import Scheduler
import setting, search_doc, corpcode
import datetime
import os
import time
import logging
'''
실행파일
'''
setting_cls = setting.Setting()
corpcode_cls = corpcode.Corpcode()
search_doc_cls = search_doc.SearchDoc()
def run():
print("", end="") #exe파일에서 print가 없으면 시작을 안하는 경우 때문에 넣음
corpcode_cls.download_corpcode()
search_doc_cls.save_file(search_doc_cls.add_data())
logger = logging.getLogger('schedule')
# 에러뜨면 로깅하고 스케쥴대로 진행할 수 있게 함
class SafeScheduler(Scheduler):
"""
An implementation of Scheduler that catches jobs that fail, logs their
exception tracebacks as errors, optionally reschedules the jobs for their
next run time, and keeps going.
Use this to run jobs that may or may not crash without worrying about
whether other jobs will run or if they'll crash the entire script.
"""
def __init__(self, reschedule_on_failure=True):
"""
If reschedule_on_failure is True, jobs will be rescheduled for their
next run as if they had completed successfully. If False, they'll run
on the next run_pending() tick.
"""
self.reschedule_on_failure = reschedule_on_failure
super().__init__()
def _run_job(self, job):
try:
super()._run_job(job)
except Exception:
logger.error(format_exc())
if not os.path.isdir("log"):
os.mkdir("log")
today_date = datetime.date.today().isoformat()
with open(f"log/back_{today_date}.txt", 'w') as file:
file.write(format_exc())
job.last_run = datetime.datetime.now()
job._schedule_next_run()
# 수정한 스케줄러 적용하기
scheduler = SafeScheduler()
# 지정한 시간 가져오기
search_time = setting_cls.get_search_time()
scheduler.every().day.at(search_time).do(run)
# scheduler.every(5).seconds.do(run)
while True:
scheduler.run_pending()
time.sleep(1)