-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·91 lines (77 loc) · 2.8 KB
/
Copy pathmain.py
File metadata and controls
executable file
·91 lines (77 loc) · 2.8 KB
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
#!/home/rtem/.conda/envs/alarms/bin/python
# -*- coding: utf-8 -*-
# alarm_codes is a directory
# utils is file. Importing it allows for error email delivery & importing environment variables
from alarm_codes import utils
from obspy import UTCDateTime
import os
import sys
import traceback
import warnings
import time
start=time.time()
# don't write .pyc files (probably slightly faster without this, but more cluttered)
sys.dont_write_bytecode = True
# if run from a cron, write output to 4-hourly file in the logs directory
if os.getenv('FROMCRON') == 'yep':
T0=UTCDateTime.now()
d_hour=int(T0.strftime('%H'))%4
f_time=UTCDateTime(T0.strftime('%Y%m%d'))+(int(T0.strftime('%H'))-d_hour)*3600
file=os.environ['LOGS_DIR']+'/'+sys.argv[1]+'-'+f_time.strftime('%Y%m%d-%H')+'.out'
os.system('touch {}'.format(file))
f=open(file,'a')
sys.stdout=sys.stderr=f
# keep .keep file from getting pruned by other cron deleting old log-files
os.system('touch {}'.format(os.environ['LOGS_DIR']+'/.keep'))
print('')
print('-----------------------------------------')
# check input arguments. 1st argument is config file, 2nd is time (optional, otherwise right now)
if len(sys.argv) == 1:
warnings.warn('Wrong input arguments. eg: main.py Pavlof_RSAM 201701020205')
sys.exit()
# no time given, use current time
if len(sys.argv) == 2:
# get current timestamp
T0=UTCDateTime.utcnow()
# round down to the nearest minute
T0=UTCDateTime(T0.strftime('%Y-%m-%d %H:%M'))
# time given, use it
else:
# time given as single string (eg. 201705130301)
if len(sys.argv)==3:
T0 = sys.argv[2]
# time given as 2 strings (eg. 20170513 03:01)
elif len(sys.argv)==4:
T0='{}{}'.format(sys.argv[2],sys.argv[3])
else:
warnings.warn('Too many input arguments. eg: main.py Pavlof_RSAM 201701020205')
sys.exit()
try:
T0 = UTCDateTime(T0)
except:
warnings.warn('Needs end-time argument. eg: main.py Pavlof_RSAM 201701020205')
sys.exit()
try:
# import the config file for the alarm you're running
exec('import alarm_configs.{} as config'.format(sys.argv[1]))
# import alarm module specified in config file
ALARM=__import__('alarm_codes.'+config.alarm_type)
# run the alarm
eval('ALARM.{}.run_alarm(config,T0)'.format(config.alarm_type))
# if error, send message to designated recipients
except:
print('Error...')
b=traceback.format_exc()
message = ''.join('{}\n'.format(a) for a in b.splitlines())
message = '{}\n\n{}'.format(T0.strftime('%Y-%m-%d %H:%M'),message)
subject=config.alarm_name+' error'
attachment='alarm_aux_files/oops.jpg'
utils.send_alert('Error',subject,message,attachment)
print(UTCDateTime.utcnow().strftime('%Y.%m.%d %H:%M:%S'))
end = time.time()
print('[{:.2f} seconds to complete alarm]'.format(end - start))
print('-----------------------------------------')
print('')
if os.getenv('FROMCRON') == 'yep':
f.close()
sys.exit()