Beispiel #1
0
def wait_and_notify(app, email, tag, sid, url):
    with app.app_context():
        print(sf.func_name())
        status = {'status': 'xxx'}
        while status['status'] != "finished":
            time.sleep(30)
            print(sf.func_name() + " update")
            status = update_status(sid)
        if status['error'] == '0':
            mess = "completed successfully"
        else:
            mess = "failed"
        send_email(email, tag, mess, url)
Beispiel #2
0
def send_email_upon_termination(email, tag, sid):
    print(sf.func_name())
    # create new thread to send to background
    url = url_for('results', user=email, job_id=tag, _external=True)
    thr = Thread(target=wait_and_notify, args=[app, email, tag, sid, url])
    thr.start()
    return thr
Beispiel #3
0
def download(job_id, user, file_name):

    data = app.config['USER_DATA_DIR']
    feil = data + "/" + user + "/" + job_id + "/protein_prompt.txt"
    print(sf.func_name() + " " + feil)
    try:
        return send_file(feil, attachment_filename='results.txt')
    except Exception as e:
        return str(e)
Beispiel #4
0
def results(job_id, user="******"):
    user = sf.QuickFix(user)
    data = app.config['USER_DATA_DIR']
    print(sf.func_name() + " datadir: " + str(data))
    feil = data + "/" + user + "/" + job_id + "/protein_prompt.txt"
    all_lines = sf.WriteLines(feil)
    feil = "../../downloads/" + user + "/" + job_id + "/result.txt"
    return render_template('results.html',
                           lines=all_lines,
                           user=user,
                           job_id=job_id,
                           result=feil)
Beispiel #5
0
def update_status(jiddle):
    print(sf.func_name())
    connector = sql.connect("jobs.db")
    cursor = connector.cursor()
    status = subprocess.check_output(["tsp", "-s", str(jiddle)]).strip()
    error = ""
    if status == "finished":
        error = subprocess.check_output(["tsp", "-i", str(jiddle)])
        print(sf.func_name() + " " + error)
        id1 = error.find("exit code") + 10
        id2 = error.find('\n')
        error = error[id1:id2].strip()
        print(sf.func_name() + " error-code: <" + error + ">")

    cmd = "UPDATE jobs SET status = '" + status + "' WHERE id = '" + str(
        jiddle) + "'"
    print(cmd)
    cursor.execute(cmd)
    connector.commit()
    connector.close()
    return {'status': status, 'error': error}
Beispiel #6
0
def send_email(email, tag, mess, url):
    with app.app_context():

        print(sf.func_name())
        subject = tag + " " + mess
        template = 'mail/link'
        msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + ' ' + subject,
                      sender=app.config['FLASKY_MAIL_SENDER'],
                      recipients=[email])
        msg.body = render_template(template + '.txt',
                                   email=email,
                                   tag=tag,
                                   mess=mess,
                                   url=url)
        msg.html = render_template(template + '.html',
                                   email=email,
                                   tag=tag,
                                   mess=mess,
                                   url=url)
        mail.send(msg)
Beispiel #7
0
def update_all():
    print(sf.func_name())
    connector = sql.connect("jobs.db")
    cursor = connector.cursor()
    status = {}
    lines = subprocess.check_output(["tsp"]).split('\n')
    print(str(len(lines)) + " entries")
    for l in lines:
        print(l)
        l = l.strip()
        if len(l) < 5 or "ID" in l: continue
        c = l.split()
        status[c[0]] = c[1]

    for k, v in status.iteritems():
        cmd = "UPDATE jobs SET status = '" + v + "' WHERE id = '" + k + "'"
        print(cmd)
        cursor.execute(cmd)
    connector.commit()

    connector.close()
    return status
Beispiel #8
0
def submit(email, tag, sequence, db):
    print("now entered submit:" + current_app.name)
    #    os.chdir( app.config['APP_PATH'] )
    pwd = os.getcwd()
    print(pwd)
    if email == '':
        email = "anonymous"
    else:
        email = sf.QuickFix(email)
    print(email)
    sequence = sf.CleanSequence(sequence)
    print(sequence)
    tag = sf.QuickFix(tag)
    print(tag)
    user_dir = app.config['USER_DATA_DIR']

    connector = sql.connect("jobs.db")
    cursor = connector.cursor()

    #    os.chdir( user_dir )
    job_dir = user_dir + "/" + email + "/" + tag

    print(job_dir)
    if os.path.isdir(job_dir):
        print("dir exists")
        return {'status': 'exists', 'id': '-99999', 'error': '-99999'}
    else:
        os.makedirs(job_dir)
        print(job_dir + " created")


#    os.chdir( job_dir )
    with open(job_dir + '/protein_prompt.txt', 'w') as f:
        f.write("fake.fa 15 AABCDEABCDEBCDE 0.4 \n")
        f.write(
            "cake.fa 105 ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE 0.5 \n"
        )
        f.write("bake.fa 5 ABCDE 1.5 \n")

    print(sf.func_name() + " send to queue")
    #cmd = "tsp rf -seq " + sequence + " -db " + db + " -out " + job_dir + "/protein_prompt.txt"
    cmd = ["tsp", "sleep", "10"]
    jiddle = subprocess.check_output(cmd).strip()
    print("job id: " + jiddle)
    status = subprocess.check_output(["tsp", "-s", str(jiddle)]).strip()
    print("status " + status)
    date = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
    print(date)

    print("INSERT INTO jobs (user,tag,id,date,status) VALUES (?,?,?,?,?)",
          (email, tag, jiddle, date, status))
    cursor.execute(
        "INSERT INTO jobs (user,tag,id,date,status) VALUES (?,?,?,?,?)",
        (email, tag, jiddle, date, status))
    connector.commit()

    #    os.chdir( pwd)
    print("now: " + os.getcwd() + " should have retured to base dir")
    connector.close()

    return {'status': status, 'id': jiddle}
Beispiel #9
0
def taskstatus(task_id):
    print(sf.func_name() + " id: " + task_id)
    status = update_status(task_id)
    return jsonify(status)
Beispiel #10
0
def index():
    print(sf.func_name() + " go")

    form = RequestForm()
    #    form = RequestForm(db='1')

    if request.method == 'GET':

        form.email.data = session.get('email', '')
        form.sequence.data = session.get('sequence', '')
        form.tag.data = session.get('tag', '')
        form.db.data = session.get('db', '1')

        print(sf.func_name() + " get")
        print(sf.func_name() + " " + form.email.data)
        print(sf.func_name() + " " + form.tag.data)
        print(sf.func_name() + " " + form.sequence.data)
        print(sf.func_name() + " " + form.db.data)

        return render_template('form.html', form=form)

    print(sf.func_name() + " post ")
    if form.validate() == False:
        print(sf.func_name() + " errors ")
        flash('errors in input (see detailed information above)')
        return render_template('form.html', form=form)

    #    email = request.form['email']
    session['email'] = form.email.data
    session['tag'] = form.tag.data
    session['sequence'] = form.sequence.data
    session['db'] = form.db.data

    print(sf.func_name() + " redirect")
    print(sf.func_name() + " " + session.get('email'))
    print(sf.func_name() + " " + session.get('tag'))
    print(sf.func_name() + " " + session.get('sequence'))
    print(sf.func_name() + " " + session.get('db'))
    #    return render_template( 'wait.html', form = form, serialize=Serialize)

    status = submit(form.email.data, form.tag.data, form.sequence.data,
                    form.db.data)
    print(sf.func_name() + " submit send to background, id: " +
          str(status['id']))
    print(sf.func_name() + " forward status location: " +
          url_for('taskstatus', task_id=status['id']))
    joburl = url_for('taskstatus', task_id=status['id'])
    if int(status['id']) < 0:
        flash("submission failed")
        return render_template('form.html', form=form)

    # start background process waiting for termination of calculation to send email
    if form.email.data != "anonymous":
        send_email_upon_termination(form.email.data, form.tag.data,
                                    status['id'])

    return render_template('wait.html', form=form, joburl=joburl)