-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactsync.py
executable file
·92 lines (71 loc) · 2.81 KB
/
actsync.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
#!/usr/bin/python2
from __future__ import print_function
import time, sys, yaml, os
from daemon import runner
import datetime as dt
import pytz
from dateutil import parser
import getpass
import calendar
import numpy as np
import traceback
import subprocess
def check_if_time(frequency,trigger_day,time_zone_string,trigger_time,tolerance):
today = dt.date.today()
if frequency=='weekly':
assert trigger_day in [calendar.day_name[x] for x in xrange(7)], "Unrecognized day name."
isDay = (calendar.day_name[today.weekday()]==trigger_day)
if not(isDay): return False
elif frequency=='nightly' or frequency=='daily':
pass
else:
raise NotImplementedError
timezone=pytz.timezone(time_zone_string)
time_now = dt.datetime.now(tz=timezone)
datestring = dt.datetime.strftime(dt.datetime.today().date(),format='%b %d %Y')
passtime = trigger_time
dtin = parser.parse(passtime)
dtin_aware = timezone.localize(dtin)
if abs((dtin_aware-time_now).total_seconds())<tolerance:
return True
else:
return False
class App():
def __init__(self,daemon_command,yaml_file,time_interval_sec=60,tolerance_seconds=240):
self.dir = os.path.dirname(os.path.abspath(__file__))
self.stdin_path = '/dev/null'
self.stdout_path = self.dir+'/syncact_out_'+str(time.time())+".log"
self.stderr_path = self.dir+'/syncact_err_'+str(time.time())+".log"
self.pidfile_path = '/tmp/syncact_daemon.pid'
self.pidfile_timeout = 5
self.interval = time_interval_sec
self.tolerance = tolerance_seconds
assert self.tolerance>self.interval
self.last_day = -1
if daemon_command!="stop":
with open('settings.yaml') as f:
self.settings = yaml.safe_load(f)
print("syncact: Daemon is running.")
def run(self):
while True:
now_day = dt.datetime.today().day
if check_if_time(self.settings['frequency'],
None,
self.settings['time_zone'],
self.settings['trigger_time'],
tolerance=self.tolerance) and (now_day!=self.last_day):
print("Starting sync...")
self.last_day = dt.datetime.today().day
subprocess.call("."+self.dir+"/localScript.sh")
time.sleep(self.interval)
def main(argv):
try:
yamlFile = sys.argv[2]
except:
assert sys.argv[1]=="stop", "No settings yaml file specified."
yamlFile = None
app = App(sys.argv[1],yamlFile)
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()
if (__name__ == "__main__"):
main(sys.argv)