Skip to content

Commit

Permalink
Merge pull request #10 from rohitdatta/bg-worker
Browse files Browse the repository at this point in the history
Background worker
  • Loading branch information
rohitdatta authored Oct 2, 2017
2 parents 3d2e4c4 + 039fba1 commit 759d7a4
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 142 deletions.
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
web: gunicorn pepper:hackathon_identity_app
worker: python manage.py runworker
10 changes: 10 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand
from pepper import create_app, app
from rq import Connection, Worker
import redis

hackathon_identity_app = create_app()
manager = Manager(hackathon_identity_app)
Expand All @@ -15,6 +17,14 @@
Migrate(hackathon_identity_app, app.DB)
manager.add_command('db', MigrateCommand)

@manager.command
def runworker():
redis_url = os.getenv('REDISTOGO_URL')
redis_connection = redis.from_url(redis_url)
with Connection(redis_connection):
worker = Worker(['default'])
worker.work()

@manager.command
def run(port=5000):
app.run(port=int(port))
Expand Down
5 changes: 5 additions & 0 deletions pepper/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
import sendgrid
import settings
import structlog
from rq import Queue
import redis

DB = SQLAlchemy()
redis_store = Redis()
sg = sendgrid.SendGridAPIClient(apikey=settings.SENDGRID_API_KEY)
cdn = CDN()
redis_url = settings.REDIS_URL
conn = redis.from_url(redis_url)
q = Queue(connection=conn)

import routes
from users.models import User
Expand Down
3 changes: 2 additions & 1 deletion pepper/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def configure_routes(app):
app.add_url_rule('/confirm', 'confirm-registration', view_func=users.views.confirm_registration, methods=['GET', 'POST'])
app.add_url_rule('/profile', 'update-profile', view_func=users.views.edit_resume, methods=['GET', 'POST'])
app.add_url_rule('/profile/resume', 'view-own-resume', view_func=users.views.view_own_resume, methods=['GET'])
app.add_url_rule('/refresh', 'refresh-mlh-data', view_func=users.views.refresh_from_MLH, methods=['GET'])
app.add_url_rule('/refresh', 'refresh-mlh-data', view_func=users.views.refresh_from_mlh, methods=['GET'])
app.add_url_rule('/accept', 'accept-invite', view_func=users.views.accept, methods=['GET', 'POST'])
app.add_url_rule('/accept/sign', 'sign', view_func=users.views.sign, methods=['GET', 'POST'])

Expand All @@ -36,6 +36,7 @@ def configure_routes(app):
app.add_url_rule('/admin/check-in', 'manual-check-in', view_func=users.views.check_in_manual, methods=['GET', 'POST'])
app.add_url_rule('/admin/check-in-post', 'manual-check-in-post', view_func=users.views.check_in_post, methods=['POST'])
app.add_url_rule('/admin/set-mlh-id', 'set-mlh-id', view_func=users.views.set_mlh_id, methods=['GET', 'POST'])
app.add_url_rule('/admin/job/<job_key>', view_func=users.views.job_view, methods=['GET'])

# API
app.add_url_rule('/api/announcements', 'announcements', view_func=announcements.views.announcement_list, methods=['GET'])
Expand Down
3 changes: 2 additions & 1 deletion pepper/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@
FALLBACK_LOCAL_REGISTER = os.getenv('FALLBACK_LOCAL_REGISTER')
CHECK_IN_SECRET = os.getenv('CHECK_IN_SECRET')
FIREBASE_KEY = os.getenv('FIREBASE_KEY')
RESUMES_LINK = os.getenv('RESUMES_LINK')
RESUMES_LINK = os.getenv('RESUMES_LINK')
REDIS_URL = os.getenv('REDISTOGO_URL')
6 changes: 3 additions & 3 deletions pepper/templates/emails/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
</tr>
<tr>
<td class="aligncenter content-block">
<a href="https://facebook.com/HackTX"><img src="https://d92c68liho4ps.cloudfront.net/static/img/social/fb.png"></a>
<a href="https://twitter.com/HackTX"><img src="https://d92c68liho4ps.cloudfront.net/static/img/social/twitter.png"></a>
<a href="https://github.com/HackTX"><img src="https://d92c68liho4ps.cloudfront.net/static/img/social/github.png"></a>
<a href="https://facebook.com/HackTX"><img src="{{url_for('static', filename='img/social/fb.png', _external=True)}}"></a>
<a href="https://twitter.com/HackTX"><img src="{{url_for('static', filename='img/social/twitter.png', _external=True)}}"></a>
<a href="https://github.com/HackTX"><img src="{{url_for('static', filename='img/social/github.png', _external=True)}}"></a>
</td>
</tr>
</table>
Expand Down
6 changes: 3 additions & 3 deletions pepper/templates/emails/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@
</tr>
<tr>
<td class="aligncenter content-block">
<a href="https://facebook.com/HackTX"><img src="https://d92c68liho4ps.cloudfront.net/static/img/social/fb.png"></a>
<a href="https://twitter.com/HackTX"><img src="https://d92c68liho4ps.cloudfront.net/static/img/social/twitter.png"></a>
<a href="https://github.com/HackTX"><img src="https://d92c68liho4ps.cloudfront.net/static/img/social/github.png"></a>
<a href="https://facebook.com/HackTX"><img src="{{url_for('static', filename='img/social/fb.png', _external=True)}}"></a>
<a href="https://twitter.com/HackTX"><img src="{{url_for('static', filename='img/social/twitter.png', _external=True)}}"></a>
<a href="https://github.com/HackTX"><img src="{{url_for('static', filename='img/social/github.png', _external=True)}}"></a>
</td>
</tr>
</table>
Expand Down
41 changes: 23 additions & 18 deletions pepper/templates/users/admin/accept_users.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,31 @@ <h1>Accept Users</h1>
<form action="{{ url_for('batch-modify') }}" method="POST">
<input type="number" name="num_to_accept" placeholder="Number to accept">
<div class="select">
<select name="type">
<option value="fifo">FIFO</option>
<option value="random">Random</option>
</select>
<div class="select__arrow"></div>
<select name="type">
<option value="fifo">FIFO</option>
<option value="random">Random</option>
</select>
<div class="select__arrow"></div>
</div>
<button type="submit" class="confirm">Submit <i class="fa fa-arrow-circle-o-right" aria-hidden="true"></i></button>
<label class="control control--checkbox">Include waitlisted
<input type="checkbox" name="include_waitlist" value="true"/>
<div class="control__indicator"></div>
</label>
<button type="submit" class="confirm">Submit <i class="fa fa-arrow-circle-o-right" aria-hidden="true"></i>
</button>
</form>
<br>
<p> List of pending users</p>
<p> List of pending users</p>

<ul>
</ul>
<ul>
{% for user in users %}
<div class="row">
<div class="col-md-6">
{{ user.fname }} {{ user.lname }}
</div>
</div>
{% endfor %}
</ul>
<ul>
</ul>
<ul>
{% for user in users %}
<div class="row">
<div class="col-md-6">
{{ user.fname }} {{ user.lname }}
</div>
</div>
{% endfor %}
</ul>
{% endblock content %}
44 changes: 20 additions & 24 deletions pepper/templates/users/confirm.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,38 @@ <h2>Almost There!</h2>
<td>Email</td>
<td>{{ user.email }}</td>
</tr>
{# <tr>#}
{# <td>Class Standing</td>#}
{# <td>{{ user.class_standing }}</td>#}
{# </tr>#}
{# <tr>#}
{# <td>Major</td>#}
{# <td>{{ user.major }}</td>#}
{# </tr>#}
<tr>
<td>Major</td>
<td>{{ user.major }}</td>
</tr>
<tr>
<td>Shirt Size</td>
<td>{{ user.shirt_size }}</td>
</tr>
{# <tr>#}
{# <td>Dietary Restrictions</td>#}
{# <td>{{ user.dietary_restrictions }}</td>#}
{# </tr>#}
<tr>
<td>Dietary Restrictions</td>
<td>{{ user.dietary_restrictions }}</td>
</tr>
<tr>
<td>Birthday</td>
<td>{{ user.birthday.strftime('%B %e, %Y') }}</td>
</tr>
{# <tr>#}
{# <td>Gender</td>#}
{# <td>{{ user.gender }}</td>#}
{# </tr>#}
{# <tr>#}
{# <td>Phone Number</td>#}
{# <td>{{ user.phone_number }}</td>#}
{# </tr>#}
<tr>
<td>Gender</td>
<td>{{ user.gender }}</td>
</tr>
<tr>
<td>Phone Number</td>
<td>{{ user.phone_number }}</td>
</tr>
<tr>
<td>School</td>
<td>{{ user.school_name }}</td>
</tr>
{# <tr>#}
{# <td class="no-border">Special Needs</td>#}
{# <td class="no-border">{{ user.special_needs }}</td>#}
{# </tr>#}
<tr>
<td class="no-border">Special Needs</td>
<td class="no-border">{{ user.special_needs }}</td>
</tr>
</table>
</div>
</div>
Expand Down
67 changes: 67 additions & 0 deletions pepper/users/batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from flask import render_template, render_template_string
from helpers import send_email
from pepper import settings
from models import User
from pepper.app import DB

from flask import g
from sqlalchemy import or_

import random

def send_batch_email(content, subject, users):
lines = content.split('\r\n')
msg_body = u""
for line in lines:
msg_body += u'<tr><td class="content-block">{}</td></tr>\n'.format(line)
for user in users:
html = render_template('emails/generic_message.html', content=msg_body)
html = render_template_string(html, user=user)
send_email(settings.GENERAL_INFO_EMAIL, subject, user.email, html_content=html)
print 'Sent email'

def accept_fifo(num_to_accept, include_waitlist):
if include_waitlist:
potential_attendees = User.query.filter(or_(User.status == 'WAITLISTED', User.status == 'PENDING'))
else:
potential_attendees = User.query.filter_by(status='PENDING')
ordered_attendees = potential_attendees.order_by(User.time_applied.asc()).limit(
num_to_accept).all()

for attendee in ordered_attendees:
if attendee.status == 'WAITLISTED':
html = render_template('emails/application_decisions/accept_from_waitlist.html', user=attendee)
else: # User should be in pending state, but catch all just in case
html = render_template('emails/application_decisions/accepted.html', user=attendee)
attendee.status = 'ACCEPTED'
DB.session.commit()
send_email(settings.GENERAL_INFO_EMAIL, "Congrats! {} Invitation"
.format(settings.HACKATHON_NAME),
attendee.email, html_content=html)
g.log = g.log.bind(email=attendee.email)
g.log.info('Sent email to')


def random_accept(num_to_accept, include_waitlist):
if include_waitlist:
pool = User.query.filter(or_(User.status == 'PENDING', User.status == 'WAITLISTED')).all()
else:
pool = User.query.filter_by(status='PENDING').all()

accepted = random.sample(set(pool), num_to_accept)
for attendee in accepted:
if attendee.status == 'PENDING':
html = render_template('emails/application_decisions/accepted.html', user=attendee)
else: # they got off waitlist
html = render_template('emails/application_decisions/accept_from_waitlist.html', user=attendee)
attendee.status = 'ACCEPTED'
DB.session.commit()
send_email(settings.GENERAL_INFO_EMAIL, "You're In! {} Invitation".format(settings.HACKATHON_NAME), attendee.email, html_content=html)

# set everyone else to go from pending to waitlisted
pending_attendees = User.query.filter_by(status='PENDING').all()
for pending_attendee in pending_attendees:
pending_attendee.status = 'WAITLISTED'
html = render_template('emails/application_decisions/waitlisted.html', user=pending_attendee)
DB.session.commit()
send_email(settings.GENERAL_INFO_EMAIL, "You're {} Application Status".format(settings.HACKATHON_NAME), pending_attendee.email, html_content=html)
6 changes: 5 additions & 1 deletion pepper/users/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pepper.utils import send_email, s
from pepper import settings
from flask import render_template, url_for, flash
import time

def hash_pwd(password):
return generate_password_hash(password)
Expand Down Expand Up @@ -36,4 +37,7 @@ def send_recruiter_invite(user):
to_email=user.email, txt_content=txt, html_content=html)
except Exception as e:
g.log = g.log.bind(error=e)
g.log.error('Unable to send recruiter email: ')
g.log.error('Unable to send recruiter email: ')

def sleep():
time.sleep(5)
Loading

1 comment on commit 759d7a4

@rainiera
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs more 🐮🔔!

Please sign in to comment.