-
Notifications
You must be signed in to change notification settings - Fork 4
/
timed_task_run.py
executable file
·79 lines (68 loc) · 2.15 KB
/
timed_task_run.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from subprocess import *
from timed_task_config import CONFIG_JSON
from datetime import datetime
import time
import os
CURRENT_DIR = os.path.split(os.path.abspath(__file__))[0]
RUN_CMD = 'python ' + os.path.join(CURRENT_DIR, 'run.py')
CONFIG_DAT_PATH = os.path.join(CURRENT_DIR, 'config.dat')
task = None
def do_task():
global task
task = None
task = Popen(RUN_CMD, shell=True, stdout=PIPE, stdin=PIPE, stderr=PIPE)
out, err = task.communicate()
task = None
if err:
print 'Error: ' + err
else:
print "Executed '" + RUN_CMD + "'"
print out
def change_config(local_path, bucket, remote_dir):
j = {
'Operation': 'upload',
'IgnoreExist': 'false',
'LocalPath': local_path,
'BucketNameFixed': bucket,
'RemoteDir': remote_dir
}
new_lines = []
with open(CONFIG_DAT_PATH, 'r') as f:
lines = f.readlines()
for line in lines:
for k, v in j.iteritems():
if line.strip().startswith(k):
line = ' ' + k + ' = ' + v + '\n'
new_lines.append(line.rstrip() + '\n')
with open(CONFIG_DAT_PATH, 'w') as f:
f.writelines(new_lines)
f.flush()
def run():
try:
while True:
now = datetime.now().strftime('%H:%M')
if now in CONFIG_JSON['run_moments_everyday']:
print datetime.now().strftime('%Y-%m-%d %H:%M:%S')
for upload_config in CONFIG_JSON['upload_configs']:
change_config(upload_config[0], upload_config[1], upload_config[2])
do_task()
print 'Waiting for next run moment...'
while True:
if datetime.now().strftime('%H:%M') == now:
time.sleep(0.1)
else:
break
else:
time.sleep(0.1)
except KeyboardInterrupt:
print 'exit...'
if task:
try:
task.terminate()
except OSError:
pass
exit()
if __name__ == '__main__':
run()