-
Notifications
You must be signed in to change notification settings - Fork 6
/
Mirrors-AutoSync.py
154 lines (118 loc) · 3.71 KB
/
Mirrors-AutoSync.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Mirrors-AutoSync
# A tool to set schedules to rsync from remote server.
# developed by eastpiger from Geek Pie @ ShanghaiTech
# - http://www.geekpie.org/
# - http://www.eastpiger.com/
# - https://github.com/ShanghaitechGeekPie/Mirrors-AutoSync
# ===========================================
config_file_dir = "/Mirrors-AutoSync/Mirrors-AutoSync.conf"
script_file_dir = "/Mirrors-AutoSync/script/"
# ===========================================
from apscheduler.schedulers.background import BackgroundScheduler,BlockingScheduler
import threading
import json
import datetime, time
import os
class task(object):
"""scheduler task"""
name = 'default'
schedule = {}
path = {}
def __init__(self, name, schedule, exec, argument):
super(task, self).__init__()
self.name = name
self.schedule = schedule
self.exec = exec
self.argument = argument
self.schedule.setdefault('year', '*')
self.schedule.setdefault('month', '*')
self.schedule.setdefault('day', '*')
self.schedule.setdefault('week', '*')
self.schedule.setdefault('day_of_week', '*')
self.schedule.setdefault('hour', '*')
self.schedule.setdefault('minute', '*')
self.schedule.setdefault('second', '*')
print(' Load scheduler [' + self.name + '].')
def runner(self):
global method_lock
global scheduler
global mutex
if mutex.acquire():
method_lock += 1
mutex.release()
try:
print(" [{}] running with [{}].".format(self.name, self.exec))
statuscode = os.system("( python3 {} {} {} {} {} 2>&1 ) > {}{}"
.format(
script_file_dir + self.exec,
self.name,
config_file_dir,
status_file_dir,
' '.join(self.argument),
log_file_dir,
self.name)) >> 8
print(" [{}] fired.".format(self.name, statuscode))
if statuscode != 0:
print(" [{}] script running with [{}] failed with error code {}."
.format(self.name, self.exec, statuscode))
if statuscode == 233:
print(" [{}] script ask to retry after 60 minutes."
.format(self.name))
scheduler.modify_job(
self.name,
next_run_time = datetime.datetime.now() + datetime.timedelta(seconds = 60*60),)
finally:
if mutex.acquire():
method_lock -= 1
mutex.release()
def setup(self, scheduler):
scheduler.add_job(
self.runner,
'cron',
id = self.name,
name = self.name,
coalesce = True,
misfire_grace_time = 86400,
max_instances = 1,
next_run_time = datetime.datetime.now(),
year = self.schedule['year'],
month = self.schedule['month'],
day = self.schedule['day'],
week = self.schedule['week'],
day_of_week = self.schedule['day_of_week'],
hour = self.schedule['hour'],
minute = self.schedule['minute'],
second = self.schedule['second'],
)
print(' Set scheduler ' + self.name + '.')
print('''
===========================================================
Mirrors-AutoSync
A tool to set schedules to rsync from remote server.
developed by eastpiger from Geek Pie @ ShanghaiTech
- http://www.geekpie.org/
- http://www.eastpiger.com/
- https://github.com/ShanghaitechGeekPie/Mirrors-AutoSync
===========================================================
''')
print('[Loading config file]\n')
scheduler = BlockingScheduler(
timezone = 'Asia/Shanghai',
)
config_file = open(config_file_dir, 'r')
content = json.loads(config_file.read())
log_file_dir = content['log_file_dir']
status_file_dir = content['status_file_dir']
method_lock = 0
mutex = threading.Lock()
for i in content['schedules']:
t = task(i['name'], i['schedule'], i['exec'], i['argument'])
t.setup(scheduler)
config_file.close()
print(' Finished.\n')
print('[Starting multithreading]\n')
try:
scheduler.start()
except KeyboardInterrupt:
pass
print(' Finished.\n')