-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathscraper_sched.py
42 lines (30 loc) · 985 Bytes
/
scraper_sched.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
from functools import wraps
import errno
import os
import signal
from scraper import run_scraper
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger
class TimeoutError(Exception):
pass
def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):
def decorator(func):
def _handle_timeout(signum, frame):
raise TimeoutError(error_message)
def wrapper(*args, **kwargs):
#signal.signal(signal.SIGALRM, _handle_timeout)
signal.alarm(seconds)
try:
result = func(*args, **kwargs)
finally:
signal.alarm(0)
return result
return wraps(func)(wrapper)
return decorator
@timeout(seconds=90 * 60)
def timeout_runner():
run_scraper()
if __name__ == '__main__':
scheduler = BlockingScheduler()
scheduler.add_job(timeout_runner, CronTrigger(minute=0))
scheduler.start()