Skip to content
Snippets Groups Projects
Commit a23eaec2 authored by Floreal Cabanettes's avatar Floreal Cabanettes
Browse files

Add mail send at the end of a job

parent 81a66247
No related branches found
No related tags found
No related merge requests found
......@@ -4,9 +4,16 @@ data_folder = ###PROGRAM###/data
# batch system type: local, sge, slurm
batch_system_type = local
threads = 4
web_url = http://localhost:5000
[softwares]
minimap2 = ###DEFAULT###
[database]
sqlite_file = ###PROGRAM###/srv/database.sqlite
[mail]
status = mail@dgenies
reply = mail@dgenies
org = "Dgenies team"
send_mail_status = True
\ No newline at end of file
......@@ -12,11 +12,13 @@ from lib.Fasta import Fasta
from lib.functions import Functions
import requests
import wget
from jinja2 import Template
from flask_mail import Message
class JobManager:
def __init__(self, id_job: str, email: str=None, query: Fasta=None, target: Fasta=None):
def __init__(self, id_job: str, email: str=None, query: Fasta=None, target: Fasta=None, mailer: "Mail"=None):
self.id_job = id_job
self.email = email
self.query = query
......@@ -27,6 +29,11 @@ class JobManager:
self.minimap2 = config_reader.get_minimap2_exec()
self.threads = config_reader.get_nb_threads()
self.app_data = config_reader.get_app_data()
self.web_url = config_reader.get_web_url()
self.mail_status = config_reader.get_mail_status_sender()
self.mail_reply = config_reader.get_mail_reply()
self.mail_org = config_reader.get_mail_org()
self.do_send = config_reader.get_send_mail_status()
# Outputs:
self.output_dir = os.path.join(self.app_data, id_job)
self.paf = os.path.join(self.output_dir, "map.paf")
......@@ -34,6 +41,7 @@ class JobManager:
self.idx_q = os.path.join(self.output_dir, "query.idx")
self.idx_t = os.path.join(self.output_dir, "target.idx")
self.logs = os.path.join(self.output_dir, "logs.txt")
self.mailer = mailer
def __check_job_success_local(self):
if os.path.exists(self.paf):
......@@ -45,6 +53,50 @@ class JobManager:
if self.batch_system_type == "local":
return self.__check_job_success_local()
def get_mail_content(self):
message = "D-Genies\n\n"
if self.status == "success":
message += "Your job %s has successfully ended!\n\n" % self.id_job
message += str("Your job {0} is finished. You can see the results by clicking on the link below:\n"
"{1}/results/{0}\n\n").format(self.id_job, self.web_url)
else:
message += "Your job %s has failed!\n\n" % self.id_job
message += "Your job %s has failed. If the problem persists, please contact the support.\n\n" % self.id_job
message += "See you soon on D-Genies,\n"
message += "The team"
def get_mail_content_html(self):
template_str = """<h1>D-Genies</h1>
<h3>{% if status == "success" %}Your job {{ job_name }} has successfully ended!{% else %}Your job {{ job_name }} has failed{% endif %}</h3>
<p>Hi,</p>
{% if status == "success" %}
<p>Your job {{ job_name }} is finished. You can <a href="{{ url_base }}/results/{{ job_name }}">click here</a> to see results.</p>
{% else %}
<p>Your job {{ job_name }} has failed. If the problem persists, please contact the support.</p>
{% endif %}
<p>See you soon on D-Genies,</p>
<p>The team</p>
"""
template = Template(template_str)
return template.render(job_name=self.id_job, status=self.status, url_base=self.web_url)
def get_mail_subject(self):
if self.status == "success":
return "DGenies - Job %s finished" % self.id_job
else:
return "DGenies - Job %s failed" % self.id_job
def send_mail(self):
msg = Message(
subject=self.get_mail_subject(),
recipients=self.email,
html=self.get_mail_content_html(),
body=self.get_mail_content(),
sender=(self.mail_org, self.mail_status) if self.mail_org is not None else self.mail_status,
reply_to=self.mail_reply
)
self.mailer.send(msg)
@db_session
def __launch_local(self):
cmd = ["run_minimap2.sh", self.minimap2, self.threads,
......@@ -150,6 +202,8 @@ class JobManager:
job = Job.get(id_job=self.id_job)
job.status = "success"
db.commit()
if self.do_send:
self.send_mail()
@staticmethod
def index_file(fasta: Fasta, out):
......
flask==0.12.*
Flask-Mail==0.9.*
Jinja2==2.9.*
pony==0.7.*
docopt==0.6.*
......
......@@ -48,6 +48,12 @@ class AppConfigReader(object):
except NoOptionError:
return "4"
def get_web_url(self):
try:
return self.replace_vars(self.reader.get("global", "web_url"))
except NoOptionError:
return "http://localhost:5000"
def get_minimap2_exec(self):
try:
entry = self.reader.get("softwares", "minimap2")
......@@ -60,3 +66,27 @@ class AppConfigReader(object):
return self.replace_vars(self.reader.get("database", "sqlite_file"))
except NoOptionError:
return ":memory:"
def get_mail_status_sender(self):
try:
return self.replace_vars(self.reader.get("mail", "status"))
except NoOptionError:
return "status@dgenies"
def get_mail_reply(self):
try:
return self.replace_vars(self.reader.get("mail", "reply"))
except NoOptionError:
return "status@dgenies"
def get_mail_org(self):
try:
return self.replace_vars(self.reader.get("mail", "org"))
except NoOptionError:
return None
def get_send_mail_status(self):
try:
return self.replace_vars(self.reader.get("mail", "send_mail_status"))
except NoOptionError:
return False
......@@ -5,6 +5,7 @@ import time
import datetime
import shutil
from flask import Flask, render_template, request, url_for, jsonify, session
from flask_mail import Mail
from lib.paf import Paf
from config_reader import AppConfigReader
from lib.job_manager import JobManager
......@@ -33,6 +34,9 @@ app = Flask(__name__, static_url_path='/static')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['SECRET_KEY'] = 'dsqdsq-255sdA-fHfg52-25Asd5'
# Init mail:
mail = Mail(app)
# Folder containing data:
app_data = config_reader.get_app_data()
......@@ -110,7 +114,7 @@ def launch_analysis():
target = Fasta(name=target_name, path=target_path, type_f=file_target_type)
# Launch job:
job = JobManager(id_job, email, query, target)
job = JobManager(id_job, email, query, target, mail)
job.launch()
return jsonify({"success": True, "redirect": url_for(".status", id_job=id_job)})
else:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment