Skip to content

Commit

Permalink
feat(cohort): Implement appscheduler
Browse files Browse the repository at this point in the history
- Implement background sheduler
- implement shedular to be running every 30 days
- Implement schedular to invoke the link_society_cohort_csv_data
- Include a decorator for aadding app context

[Maintains #164797807]
  • Loading branch information
frankip committed Mar 26, 2019
1 parent 611bcfe commit 436bbb1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
15 changes: 14 additions & 1 deletion src/api/utils/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Contain utility functions and constants."""

import requests, os
import functools
from collections import namedtuple
from flask import current_app, jsonify, request, url_for

Expand Down Expand Up @@ -134,4 +135,16 @@ def find_d_level(token, user_id):
response.status_code = 503
return response
except Exception:
return None
return None


# App context decorator
def with_application_context(app):
""" Instantiate the app context as a decorator """
def inner(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
with app.app_context():
return func(*args, **kwargs)
return wrapper
return inner
2 changes: 1 addition & 1 deletion src/data/cohort_data.csv
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ class 4 - kla,class 32 - los,class 23 - nbo,class 5 - kla
class 33 - los,class 24 - nbo,class 34 - los,class 25 - nbo
class 35 - los,class 6 - kla,class 26 - nbo,class 36 - los
class 27 - nbo,class 37 - los,class 7 - kla,class 28 - nbo
class 8 - kla,class 38 - los,,
class 8 - kla,class 38 - los
18 changes: 9 additions & 9 deletions src/manage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python

"""Entry point for app, contain commands to configure and run the app."""

import csv
Expand All @@ -16,6 +15,8 @@
from api.models.base import db
from run_tests import test

from apscheduler.schedulers.background import BackgroundScheduler
from api.utils.helpers import with_application_context

app = create_app()
cli = FlaskGroup(app)
Expand All @@ -28,7 +29,7 @@ def drop_database():
db.drop_all()
print("Dropped all tables successfully.")
except Exception:
print("Failed, make sure your database server is running!")
print("Failed, make sure your database server is running!")


@cli.command()
Expand Down Expand Up @@ -94,16 +95,12 @@ def linker(cohort_name, society_name):
if not cohort:
return print(
f'Error cohort by name: {cohort_name} does not exist in DB.')
if (cohort.society and
not prompt_bool(f'Cohort:{cohort_name} has society:{cohort.society} '
' already! \n Do you want to change to '
f'{society_name}?')):
if cohort.society:
return 0

society = Society.query.filter_by(name=society_name).first()
if not society:
return print(
f'Error society with name:{society_name} does not exist.')
return print(f'Error society with name:{society_name} does not exist.')

society.cohorts.append(cohort)
if society.save():
Expand All @@ -114,7 +111,7 @@ def linker(cohort_name, society_name):
print('Error something went wrong when saving to DB. :-)')


@cli.command()
@with_application_context(app)
def link_society_cohort_csv_data(path='data/cohort_data.csv'):
"""CLI tool, link cohort with society."""
with open(path) as raw_data:
Expand All @@ -139,4 +136,7 @@ def tests():
migrate = Migrate(app, db)

if __name__ == "__main__":
scheduler = BackgroundScheduler()
scheduler.add_job(link_society_cohort_csv_data, 'interval', days=30)
scheduler.start()
cli()
1 change: 1 addition & 0 deletions src/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ virtualenv-clone==0.3.0
wcwidth==0.1.7
Werkzeug==0.12.2
wrapt==1.10.11
apscheduler==3.6.0

0 comments on commit 436bbb1

Please sign in to comment.