Skip to content

Commit

Permalink
add sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
dwreeves committed Jun 19, 2024
1 parent b2910c9 commit e04b87e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 36 deletions.
2 changes: 2 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ def SQLALCHEMY_DATABASE_URI(self) -> str:
"pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw",
) # noqa

SENTRY_DSN: str | None = os.getenv("SENTRY_DSN")


class ProductionConfig(Config):
"""The Production Config is used for deployment of the website to the
Expand Down
15 changes: 4 additions & 11 deletions app/data/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,14 @@ def predict_v3_task(*args, **kwargs) -> RecordsType:


@celery_app.task
def update_db_task() -> None:
from app.data.processing.core import update_db

update_db()


@celery_app.task
def update_website_task() -> None:
def update_db_task(tweet_status: bool = False) -> None:
from app.data.globals import website_options
from app.data.processing.core import update_db
from app.twitter import tweet_current_status

update_db()
if website_options.boating_season:
if tweet_status and website_options.boating_season:
from app.twitter import tweet_current_status

tweet_current_status()


Expand All @@ -161,5 +155,4 @@ def send_database_exports_task() -> None:
predict_v2_task: WithAppContextTask
predict_v3_task: WithAppContextTask
update_db_task: WithAppContextTask
update_website_task: WithAppContextTask
send_database_exports_task: WithAppContextTask
48 changes: 23 additions & 25 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ def register_extensions(app: Flask):
cors = CORS(resources={"/api/*": {"origins": "*"}, "/flags": {"origins": "*"}})
cors.init_app(app)

if app.config.get("SENTRY_DSN"):
import sentry_sdk

sentry_sdk.init(
dsn=app.config["SENTRY_DSN"],
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for tracing.
traces_sample_rate=1.0,
# Set profiles_sample_rate to 1.0 to profile 100%
# of sampled transactions.
# We recommend adjusting this value in production.
profiles_sample_rate=1.0,
)


def register_blueprints(app: Flask):
"""Register the "blueprints." Blueprints are basically like mini web apps
Expand Down Expand Up @@ -262,42 +276,26 @@ def _wrap(*args, **kwargs):
help="If set, then run this command in Celery."
" This can help save a bit of money on Heroku compute.",
)
@click.option(
"--tweet-status/--dont-tweet-status",
is_flag=True,
default=False,
help="If set, then send a tweet indicating the status of the flags.",
)
@with_appcontext
@mail_on_fail
def update_db_command(async_: bool = False):
def update_db_command(async_: bool = False, tweet_status: bool = False):
"""Update the database with the latest live data."""
from app.data.celery import update_db_task

if async_:
res = update_db_task.delay()
res = update_db_task.delay(tweet_status=tweet_status)
click.echo(f"Started update database task ({res.id!r}).")
else:
click.echo("Updating the database...")
update_db_task.run()
update_db_task.run(tweet_status=tweet_status)
click.echo("Updated the database successfully.")

@app.cli.command("update-website")
@click.option(
"--async",
"async_",
is_flag=True,
default=False,
help="If set, then run this command in Celery."
" This can help save a bit of money on Heroku compute.",
)
@mail_on_fail
def update_website_command(async_: bool = False):
"""Updates the database, then Tweets a message."""
from app.data.celery import update_website_task

if async_:
res = update_website_task.delay()
click.echo(f"Started update website task ({res.id!r}).")
else:
click.echo("Updating the website...")
update_website_task.run()
click.echo("Updated the website successfully.")

@app.cli.command("gen-mock-data")
@dev_only
def generate_mock_data():
Expand Down
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ redis
requests
rich-click
SQLAlchemy
sentry-sdk[flask]
tenacity
tweepy

Expand Down
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ blinker==1.8.2
# via
# flask
# flask-mail
# sentry-sdk
cachelib==0.9.0
# via flask-caching
celery==5.4.0
Expand All @@ -33,6 +34,7 @@ certifi==2024.2.2
# httpcore
# httpx
# requests
# sentry-sdk
cfgv==3.4.0
# via pre-commit
charset-normalizer==3.3.2
Expand Down Expand Up @@ -84,6 +86,7 @@ flask==3.0.3
# flask-mail
# flask-postgres
# flask-sqlalchemy
# sentry-sdk
flask-admin==1.6.1
# via -r requirements.in
flask-basicauth==0.2.0
Expand Down Expand Up @@ -169,6 +172,7 @@ markupsafe==2.1.5
# via
# jinja2
# mako
# sentry-sdk
# werkzeug
# wtforms
mdurl==0.1.2
Expand Down Expand Up @@ -288,6 +292,8 @@ ruff==0.4.5
# via -r requirements.in
schemathesis==3.28.1
# via -r requirements.in
sentry-sdk==2.6.0
# via -r requirements.in
setuptools==70.0.0
# via
# flask-db
Expand Down Expand Up @@ -343,6 +349,7 @@ urllib3==2.2.1
# via
# docker
# requests
# sentry-sdk
uv==0.2.3
# via -r requirements.in
vine==5.1.0
Expand Down

0 comments on commit e04b87e

Please sign in to comment.