-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSC_engine.py
executable file
·138 lines (96 loc) · 4.44 KB
/
SSC_engine.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
#!/usr/bin/env python
'''----------------------------------------------------------------------------
License:
This program is free software: you can redistribute it and/or modify
it. This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
If you use this software (or a part of it) in your research, please cite
the authors. For more information how to cite this work properly please
visit the official webside. THANKS!
http://ssc.shef.ac.uk
cron_engine.py
Advanced Python Scheduler (APScheduler) is a Python library that lets you
schedule your Python code to be executed later, either just once or
periodically. You can add new jobs or remove old ones on the fly as you
please.
----------------------------------------------------------------------------'''
from apscheduler.schedulers.blocking import BlockingScheduler
from datetime import datetime
import engine.running as run
import logging
import smtplib
import multiprocessing
import sys
__author__ = ["Gyenge, Norbert"]
__email__ = ["n.g.gyenge@sheffield.ac.uk"]
# SETUP -------------------------------------------------------------------'''
# STEP 1: Interval-based scheduling. This method schedules jobs to be run on
# selected intervals (in minutes).
interval = 10
# STEP 2: Lag-time. The downloaded observations cannot be real-time because
# the JSOC and the ShARC services need time for publising data The lag-time
# defines a period of time between the present and the observations.
lag = 2070
# STEP 2: Email address. The JSOC service requires a registred email address
# for downloading the observations. Please do not use this email address if
# you are not involved developing this software. Instead, use the official
# JSOC for validating your own email address.
# http://jsoc.stanford.edu/ajax/register_email.html
email = 'ssc@sheffield.ac.uk'
# SETUP -------------------------------------------------------------------'''
def get_log(LOG_FORMAT='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
LOG_NAME='',
LOG_FILE_INFO='engine.log',
LOG_FILE_ERROR='engine.err'):
log = logging.getLogger(LOG_NAME)
log_formatter = logging.Formatter(LOG_FORMAT)
# comment this to suppress console output
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(log_formatter)
log.addHandler(stream_handler)
file_handler_info = logging.FileHandler(LOG_FILE_INFO, mode='w')
file_handler_info.setFormatter(log_formatter)
file_handler_info.setLevel(logging.INFO)
log.addHandler(file_handler_info)
file_handler_error = logging.FileHandler(LOG_FILE_ERROR, mode='w')
file_handler_error.setFormatter(log_formatter)
file_handler_error.setLevel(logging.ERROR)
log.addHandler(file_handler_error)
log.setLevel(logging.INFO)
return log
def run_process(logger):
if (datetime.today() - start_iteration_date).days >= 3:
logger = get_log()
p = multiprocessing.Process(target=run.start, args=(datetime.now(), logger, lag, email,))
p.start()
p.join()
# Start the engine
try:
global start_iteration_date
start_iteration_date = datetime.today()
logger = get_log()
# Scheduler definiton
sched = BlockingScheduler(misfire_grace_time = 20)
# Scheduler setup
sched.add_job(run_process, 'interval', [logger],minutes=interval, next_run_time=datetime.now())
# Scheduler start
sched.start()
except:
# Do not send email if KeyboardInterrupt()
if "KeyboardInterrupt()" not in str(sys.exc_info()):
# Send an email if engine fails
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("sheffield.solar.catalogue@gmail.com", "sscerror1234")
# Compose the email
SUBJECT = "SSC Engine failed at " + str(datetime.now())
FROM = "sheffield.solar.catalogue@gmail.com"
text1 = "Dear SSC Admins,\n\n"
text2 = "The SSC engine is being restarted due an unexpected error:\n\n"
text3 = str(sys.exc_info()) + "\n" + str(datetime.now())
text4 = "\n\n SSC Web Service"
BODY = "\r\n".join(["From: %s" % FROM, "To: %s" % __email__[0],
"Subject: %s" % SUBJECT , "", text1+text2+text3+text4])
server.sendmail(FROM, __email__[0], BODY)
server.quit()