-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_util.py
executable file
·45 lines (32 loc) · 1.11 KB
/
task_util.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
#!/usr/bin/python
# -*- coding:utf-8 -*-
# vim: set number tw=0 shiftwidth=4 tabstop=4 expandtab:
import os
import signal
import logging
import threading
from apscheduler.schedulers.blocking import BlockingScheduler
logger = logging.getLogger(__file__)
class TaskManager(threading.Thread):
def __init__(self, **kwargs):
super(TaskManager, self).__init__()
self.scheduler = BlockingScheduler({'max_instances': 1})
def add_interval_task(self, task, **kwargs):
self.scheduler.add_job(task, "interval", **kwargs)
def add_cron_task(self, task, **kwargs):
self.scheduler.add_job(task, "cron", **kwargs)
def run(self):
try:
self.scheduler.start()
except Exception as e:
logger.exception(e)
def stop(self):
self.scheduler.shutdown()
task_manager = TaskManager()
def signal_handler(sig, frame):
task_manager.stop()
pid = os.getpid()
os.kill(pid, signal.SIGQUIT)
def init_signals():
signals = [signal.SIGILL, signal.SIGSEGV, signal.SIGTERM, signal.SIGINT]
map(lambda x: signal.signal(x, signal_handler), signals)