function to send emails

This commit is contained in:
Davide Alberani 2018-01-23 09:06:41 +01:00
parent df45cd24c3
commit f6dd0f5393

View file

@ -65,10 +65,10 @@ def next_id(schedules):
return str(max([int(i) for i in ids]) + 1) return str(max([int(i) for i in ids]) + 1)
def get_schedule(id_, addID=True): def get_schedule(id_, add_id=True):
schedules = read_schedules() schedules = read_schedules()
data = schedules.get('schedules', {}).get(id_, {}) data = schedules.get('schedules', {}).get(id_, {})
if addID: if add_id:
data['id'] = str(id_) data['id'] = str(id_)
return data return data
@ -81,14 +81,14 @@ def select_xpath(content, xpath):
return content return content
selected_content = [] selected_content = []
for elem in elems: for elem in elems:
selected_content.append(''.join([elem.text] + [ElementTree.tostring(e).decode('utf8', 'replace') selected_content.append(''.join([elem.text] + [ElementTree.tostring(e).decode('utf-8', 'replace')
for e in elem.getchildren()])) for e in elem.getchildren()]))
content = ''.join(selected_content) content = ''.join(selected_content)
return content return content
def run_job(id_=None, *args, **kwargs): def run_job(id_=None, *args, **kwargs):
schedule = get_schedule(id_, addID=False) schedule = get_schedule(id_, add_id=False)
url = schedule.get('url') url = schedule.get('url')
if not url: if not url:
return return
@ -147,10 +147,22 @@ def run_job(id_=None, *args, **kwargs):
return return
# send notification # send notification
diff = get_diff(id_) diff = get_diff(id_)
msg = MIMEText('changes:\n\n%s' % diff.get('diff')) send_email(to=email, subject='%s page changed' % schedule.get('title'),
msg['Subject'] = '%s page changed' % schedule.get('title') body='changes:\n\n%s' % diff.get('diff'))
msg['From'] = 'Diffido <da@mimante.net>'
msg['To'] = email
def safe_run_job(id_=None, *args, **kwargs):
try:
run_job(id_, *args, **kwargs)
except Exception as e:
send_email('error executing job %s: %s' % (id_, e))
def send_email(to, subject='diffido', body='', from_=None):
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = from_ or 'Diffido <da@mimante.net>'
msg['To'] = to
s = smtplib.SMTP('localhost') s = smtplib.SMTP('localhost')
s.send_message(msg) s.send_message(msg)
s.quit() s.quit()
@ -197,14 +209,15 @@ def get_history(id_):
return {'history': history, 'lastid': lastid} return {'history': history, 'lastid': lastid}
def get_diff(id_, diff='HEAD', oldid=None): def get_diff(id_, commit_id='HEAD', old_commit_id=None):
def _history(id_, diff, oldid, queue): def _history(id_, commit_id, old_commit_id, queue):
os.chdir('storage/%s' % id_) os.chdir('storage/%s' % id_)
p = subprocess.Popen([GIT_CMD, 'diff', diff, oldid or '%s~' % diff], stdout=subprocess.PIPE) p = subprocess.Popen([GIT_CMD, 'commit_id', old_commit_id or '%s~' % commit_id, commit_id],
stdout=subprocess.PIPE)
stdout, _ = p.communicate() stdout, _ = p.communicate()
queue.put(stdout) queue.put(stdout)
queue = multiprocessing.Queue() queue = multiprocessing.Queue()
p = multiprocessing.Process(target=_history, args=(id_, diff, oldid, queue)) p = multiprocessing.Process(target=_history, args=(id_, commit_id, old_commit_id, queue))
p.start() p.start()
res = queue.get().decode('utf-8') res = queue.get().decode('utf-8')
p.join() p.join()
@ -212,7 +225,7 @@ def get_diff(id_, diff='HEAD', oldid=None):
def scheduler_update(scheduler, id_): def scheduler_update(scheduler, id_):
schedule = get_schedule(id_, addID=False) schedule = get_schedule(id_, add_id=False)
if not schedule: if not schedule:
return return
trigger = schedule.get('trigger') trigger = schedule.get('trigger')
@ -229,7 +242,7 @@ def scheduler_update(scheduler, id_):
cron_trigger = CronTrigger.from_crontab(schedule.get('cron_crontab')) cron_trigger = CronTrigger.from_crontab(schedule.get('cron_crontab'))
args['trigger'] = cron_trigger args['trigger'] = cron_trigger
git_create_repo(id_) git_create_repo(id_)
scheduler.add_job(run_job, id=id_, replace_existing=True, kwargs={'id_': id_}, **args) scheduler.add_job(safe_run_job, id=id_, replace_existing=True, kwargs={'id_': id_}, **args)
def scheduler_delete(scheduler, id_): def scheduler_delete(scheduler, id_):
@ -392,8 +405,8 @@ class HistoryHandler(BaseHandler):
class DiffHandler(BaseHandler): class DiffHandler(BaseHandler):
@gen.coroutine @gen.coroutine
def get(self, id_, diff, oldid=None, *args, **kwargs): def get(self, id_, commit_id, old_commit_id=None, *args, **kwargs):
self.write(get_diff(id_, diff, oldid)) self.write(get_diff(id_, commit_id, old_commit_id))
class TemplateHandler(BaseHandler): class TemplateHandler(BaseHandler):
@ -442,8 +455,7 @@ def serve():
_reset_schedules_path = r'schedules/reset' _reset_schedules_path = r'schedules/reset'
_schedules_path = r'schedules/?(?P<id_>\d+)?' _schedules_path = r'schedules/?(?P<id_>\d+)?'
_history_path = r'history/?(?P<id_>\d+)' _history_path = r'history/?(?P<id_>\d+)'
_diff_path = r'diff/(?P<id_>\d+)/(?P<diff>\s+)/?(?P<oldid>\s+)?' _diff_path = r'diff/(?P<id_>\d+)/(?P<commit_id>[0-9a-f]+)/?(?P<old_commit_id>[0-9a-f]+)?/?'
_diff_path = r'diff/(?P<id_>\d+)/(?P<diff>[0-9a-f]+)/?(?P<oldid>[0-9a-f]+)?/?'
application = tornado.web.Application([ application = tornado.web.Application([
(r'/api/%s' % _reset_schedules_path, ResetSchedulesHandler, init_params), (r'/api/%s' % _reset_schedules_path, ResetSchedulesHandler, init_params),
(r'/api/v%s/%s' % (API_VERSION, _reset_schedules_path), ResetSchedulesHandler, init_params), (r'/api/v%s/%s' % (API_VERSION, _reset_schedules_path), ResetSchedulesHandler, init_params),