|
@@ -0,0 +1,35 @@
|
|
1
|
+from apscheduler.jobstores.base import JobLookupError
|
|
2
|
+from apscheduler.schedulers.background import BackgroundScheduler
|
|
3
|
+import time
|
|
4
|
+
|
|
5
|
+class SchedulerTest:
|
|
6
|
+ def __init__(self):
|
|
7
|
+ self.sched = BackgroundScheduler()
|
|
8
|
+ self.sched.start()
|
|
9
|
+ self.job_id = ''
|
|
10
|
+
|
|
11
|
+ def __del__(self):
|
|
12
|
+ print("__del__ call")
|
|
13
|
+ self.shutdown()
|
|
14
|
+
|
|
15
|
+ def shutdown(self):
|
|
16
|
+ self.sched.shutdown()
|
|
17
|
+
|
|
18
|
+ def kill_scheduler(self, job_id):
|
|
19
|
+ try:
|
|
20
|
+ self.sched.remove_job(job_id)
|
|
21
|
+ except JobLookupError as err:
|
|
22
|
+ print("fail to stop Scheduler: {err}".format(err=err))
|
|
23
|
+ return
|
|
24
|
+
|
|
25
|
+ def hello(self, type, job_id):
|
|
26
|
+ print("%s Scheduler process_id[%s] : %d" % (type, job_id, time.localtime().tm_sec))
|
|
27
|
+
|
|
28
|
+ def scheduler(self, type, job_id):
|
|
29
|
+ print("{type} Scheduler Start".format(type=type))
|
|
30
|
+ if type == 'interval':
|
|
31
|
+ self.sched.add_job(self.hello, type, seconds=10, id=job_id, args=(type, job_id))
|
|
32
|
+ elif type == 'cron':
|
|
33
|
+ self.sched.add_job(self.hello, type, day_of_week='mon-sun',
|
|
34
|
+ hour='0-23', second='*/1',
|
|
35
|
+ id=job_id, args=(type, job_id))
|